如何在PHP中发送电子邮件附件
发布时间:2020-12-13 17:01:52 所属栏目:PHP教程 来源:网络整理
导读:?php // array with filenames to be sent as attachment $files = array("sendFiles.php"); // email fields: to,from,subject,and so on $to = "dfjdsoj@googlemail.com"; $from = "mail@mail.com"; $subject ="My subject"; $message = "A logo has been
<?php
// array with filenames to be sent as attachment
$files = array("sendFiles.php");
// email fields: to,from,subject,and so on
$to = "dfjdsoj@googlemail.com";
$from = "mail@mail.com";
$subject ="My subject";
$message = "A logo has been sen't by". $_SESSION['loggedin_business_name'];
$headers = "From: $from";
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "nMIME-Version: 1.0n" . "Content-Type: multipart/mixed;n" . " boundary="{$mime_boundary}"";
// multipart boundary
$message = "This is a multi-part message in MIME format.nn" . "--{$mime_boundary}n" . "Content-Type: text/plain; charset="iso-8859-1"n" . "Content-Transfer-Encoding: 7bitnn" . $message . "nn";
$message .= "--{$mime_boundary}n";
// preparing attachments
for($x=0;$x<count($files);$x++){
$file = fopen($files[$x],"rb");
$data = fread($file,filesize($files[$x]));
fclose($file);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: {"application/octet-stream"};n" . " name="$files[$x]"n" .
"Content-Disposition: attachment;n" . " filename="$files[$x]"n" .
"Content-Transfer-Encoding: base64nn" . $data . "nn";
$message .= "--{$mime_boundary}n";
}
// send
echo sizeof($files);
$ok = @mail($to,$subject,$message,$headers);
if ($ok) {
echo "<p>mail sent to $to!</p>";
} else {
echo "<p>mail could not be sent!</p>";
}
?>
我收到一封包含sendfiles.php的电子邮件,然后是一个文本文件ATT00424.txt.数量每次都在变化.发送到我的Gmail,没关系!很奇怪! $files = array("sendFiles.php");
// email fields: to,and so on
$to = "hdfiuhufsadhfu@yaho.com";
$from = "mail@mail.com";
$subject ="My subject";
$message = "A logo has been sen't by". $_POST['loggedin_business_name'];
$headers = "From: $from";
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "rnMIME-Version: 1.0rn" . "Content-Type: multipart/mixed;rn" . " boundary="{$mime_boundary}"";
// multipart boundary
$message = "This is a multi-part message in MIME format.rn" . "--{$mime_boundary}rn" . "Content-Type: text/plain; charset="iso-8859-1"rn" . "Content-Transfer-Encoding: 7bitrn" . $message . "rn";
$message .= "--{$mime_boundary}rn";
// preparing attachments
for($x=0;$x<count($files);$x++){
$file = fopen($files[$x],"rb");
$data = fread($file,filesize($files[$x]));
fclose($file);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: {"application/octet-stream"};rn" . " name="$files[$x]"rn" .
"Content-Disposition: attachment;rn" . " filename="$files[$x]"rn" .
"Content-Transfer-Encoding: base64rn" . $data . "rn";
$message .= "--{$mime_boundary}rn";
}
在代码中添加CRLF修复了附件问题但是现在消息“A logo has sen’t by”消失了.为什么是这样? 解决方法
Use phpMailer()
<?php
require_once('phpmailer.php');
$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors,which we need to catch
$mail->IsSendmail(); // telling the class to use SendMail transport
try {
$mail->AddReplyTo('email@example.com','First Last');
$mail->AddAddress('John@example.com','John Doe');
$mail->SetFrom('email@example.com','First Last');
$mail->Subject = "Subject Line";
$mail->AltBody = "Alternate Text"; // optional,comment out and test
$mail->WordWrap = 50; // set word wrap
$mail->Body = "This is the body of the email";
$mail->IsHTML(true); // send as HTML
// Single or Multiple File Attachments
$mail->AddAttachment('../path-to-file.pdf','File-Name.pdf');
$mail->Send(); // Try to send email
//echo "Message Sent OK<p></p>n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
// end try
?>
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
