我正试图发送一封带有附件的电子邮件,我的代码如下,让我知道我是否正在做遗漏的东西或做错了什么
<?php
function mail_attachment( $filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message ) {
$file = $path. "/" .$filename;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$separator = md5(uniqid(time()));
$eol = PHP_EOL;
$header = "From: ".$from_name." <usman_86@rocketmail.com>".$from_mail. $eol;
$header .= "Reply-To: ".$replyto.$eol;
$header .= "MIME-Version: 1.0".$eol;
$header .= "Content-Type: multipart/mixed; boundary="".$separator. """ . $eol . $eol;
$header .= "Content-Transfer-Encoding: 7bit" . $eol;
$header .= "--".$separator. $eol;
$header .= "Content-Type: text/plain; charset="iso-8859-1"" . $eol;
$header .= "Content-Transfer-Encoding: 8bit" . $eol . $eol;
$header .= $message. $eol . $eol;
$header .= "--".$separator. $eol;
$header .= "Content-Type: application/octet-stream; name="".$filename. $eol; // use different content types here
$header .= "Content-Transfer-Encoding: base64".$eol;
$header .= "Content-Disposition: attachment; filename="".$filename. $eol;
$header .= $content. $eol;
$header .= "--".$separator."--";
ob_start(); //Turn on output buffering
if( mail( $mailto, $subject, "", $header ) ) {
echo "mail send ... OK"; // or use booleans here
} else {
echo "mail send ... ERROR!";
}
}
if( isset($_REQUEST['FindDealer']) ){
$my_file = "pdf.pdf";
$my_path = "/";
$my_name = " Find Dealer @ flowsleeve";
$my_mail = "stifstone@gmail.com";
$my_replyto = "my_reply_to@flowsleeve.com";
$my_subject = "Find Dealer @ flowsleeve";
$my_message = "Hallo,rndoPlease find Attached PDF For Dealers";
mail_attachment( $my_file, $my_path, $my_mail, $my_name, $my_replyto, $my_subject, $my_message );
header("Location: index.php#section8");
exit();
}
?>
use $mail-> addatattachment ($path);用这个比较好。
您可以从https://github.com/PHPMailer/PHPMailer获取phpmailer文件
<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user@example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('joe@example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen@example.com'); // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
?>
这里有很多东西需要消化,但下面这行看起来不对。
$header = "From: ".$from_name." <usman_86@rocketmail.com>".$from_mail. $eol;
解析后,将显示为:
"From: my_reply_to@flowsleeve.com <usman_86@rocketmail.com>Find Dealer @ flowsleevern"
回复地址也是错误的,请参阅标题的完整打印输出,以了解我的意思。
提示:为了方便起见,我建议使用命名为与提供给函数mail_attachment
~ I的参数相同继续计算哪个参数引用了哪个变量
整个头部看起来像这样:
From: my_reply_to@flowsleeve.com <usman_86@rocketmail.com>Find Dealer @ flowsleeve
Reply-To: Find Dealer @ flowsleeve
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="2642aff44ba290c53e0574e15444e12f"
Content-Transfer-Encoding: 7bit
--2642aff44ba290c53e0574e15444e12f
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 8bit
--2642aff44ba290c53e0574e15444e12f
Content-Type: application/octet-stream; name="pdf.pdf
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="pdf.pdf
--2642aff44ba290c53e0574e15444e12f--
从以前处理附件的经验来看,格式是至关重要的——它必须在行结尾方面完全正确,否则就会失败。一些可能有帮助的指针:
- 边界前应该只有一行。
- 最后一个边界后面应该有2个破折号。
- 有两个地方的Content-Transfer-Encoding需要有一个新的行。
另外,我忘了提到你调用函数mail_attachment
有7个参数,但函数有8个参数。在函数调用中似乎没有提供的是$from_mail