将附件添加到邮件
Posted
技术标签:
【中文标题】将附件添加到邮件【英文标题】:Adding attachments to mail 【发布时间】:2012-05-28 04:04:19 【问题描述】:我有这个完美运行的脚本,但附件为空(我的意思是我可以看到附件..但我加载的每个文件的大小都是 0 kb) 这是代码:
allegato 表示依附
$path = $allegato['tmp_name'];
$fp = fopen($path, 'r');
do //we loop until there is no data left
$data = fread($fp, 8192);
if (strlen($data) == 0) break;
$content .= $data;
while (true);
$content_encode = chunk_split(base64_encode($content));
$mime_boundary = "<<<--==+X[".md5(time())."]";
$headers .= "From:".$email."\r\n";
$headers .= "To: me <mail@gmail.com>\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed;\r\n";
$headers .= " boundary=\"".$mime_boundary."\"";
$message .= "This is a multi-part message in MIME format.\r\n";
$message .= "\r\n";
$message .= "--".$mime_boundary."\r\n";
$message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
$message .= "Content-Transfer-Encoding: 7bit\r\n";
$message .= "\r\n";
$message .= "Email sent from:".$nome." \r\n";
$message .= $messaggio."\r\n";
$message .= "--".$mime_boundary."\r\n";
$message .= "Content-Type: ".$allegato_type."\"\r\n";
$message .= " name=\ ".$allegato_name."\"\r\n";
$message .= "Content-Transfer-Encoding: base64\r\n";
$message .= "Content-Disposition: attachment;\r\n";
$message .= " filename=\ ".$allegato_name."\"\r\n";
$message .= "\r\n";
$message .= $content_encode;
$message .= "\r\n";
$message .= "--".$mime_boundary."\r\n";
$ok = mail("mail@gmail.com", $object, $message, $headers);
if($ok)
echo'<script type="text/javascript">alert("mail sent successfully ! ");</script>';
else
echo'<script type="text/javascript">alert("mail failed! ");</script>';
有人可以帮忙处理附件吗?(发送的每个文件都是 0 kb,我如何修复它?)
【问题讨论】:
我也是这样开始的,然后我意识到 php PEAR 框架的Mail_Mime package 更容易,而且我遇到的问题也少了很多。我建议你试一试。 我不知道它是否相关,但似乎" filename=\ ".$allegato_name."\"\r\n";
上缺少引用
Travesty3 问题是不知道生产服务器是否安装了Pear。 sofl 也许你是对的,应该怎么做?
我的意思是这样 .$allegato_name.\"\"\r\n"; ?
【参考方案1】:
我建议使用PHPMailer!
可从http://sourceforge.net/projects/phpmailer/免费获得
只要把它放在服务器上的一个目录中,包括它的主要php,当你从php发送电子邮件时,你的生活就会变得愉快。
我有一个简单的开始功能,我可以提供给您以获得快速的结果:
require("/path/to/class.phpmailer.php");
function mail_sender($from_name,$from_email,$to_email,$subject,$mailtext,$att = false)
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "localhost"; // specify main and backup server
$mail->SMTPAuth = false; // turn on SMTP authentication
$mail->From = $from_email;
$mail->FromName = $from_name;
$mail->WordWrap = 50; // set word wrap to 50 characters
$mail->Ishtml(true); // set email format to HTML
$mail->CharSet="utf-8";
$mail->Subject = $subject;
$mail->Body = $mailtext;
$mail->AltBody = "";
$mail->AddAddress($to_email);
if ($att)
$mail->AddAttachment("include/attachment.doc");
if(!$mail->Send())
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
【讨论】:
以上是关于将附件添加到邮件的主要内容,如果未能解决你的问题,请参考以下文章