发送带有Excel附件的PHP电子邮件时遇到问题。我可以看到邮件发送邮件,邮件即将到来,但没有附件。我知道真正的问题。这是我的代码。我已将User.xlsx文件放在我的根目录中。请帮我解决这个问题。谢谢
$to = "dibeesh@amt.in";
$subject="attachement";
$mail_msg="message with attach";
$filename="User.xlsx"; // Attachement file in root directory
$contentType="application/zip"; // Not sure about what to put here
$pathToFilename="./";
$random_hash = md5(date('r', time()));
$headers = "From: webmaster@mysite.com\r\nReply-To: ".$to;
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
$attachment = chunk_split(base64_encode(file_get_contents($pathToFilename)));
ob_start();
echo "
--PHP-mixed-$random_hash
Content-Type: multipart/alternative; boundary=\"PHP-alt-$random_hash\"
--PHP-alt-$random_hash
Content-Type: text/plain; charset=\"utf-8\"
Content-Transfer-Encoding: 7bit
$mail_msg
--PHP-alt-$random_hash--
--PHP-mixed-$random_hash
Content-Type: $contentType; name=\"$filename\"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
$attachment
--PHP-mixed-$random_hash--
";
$message = ob_get_clean();
// $fh=fopen('log.txt','w');
// fwrite($fh,$message);
$mail_sent = @mail( $to, $subject, $message, $headers );
echo $mail_sent ? "Mail sent" : "Mail failed";
答案 0 :(得分:0)
我已设法调试我的代码,抱歉重复发布。它可以帮助别人谢谢
// Email to Client with attachement
//define the receiver of the email
$to = 'dibeesh@amt.in';
//define the subject of the email
$subject = 'Bayern3 Notification';
$filename = "User.xlsx";
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: lal@amt.in\r\nReply-To: dibeesh@amt.in";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents($filename)));
//define the body of the message.
ob_start(); //Turn on output buffering
echo "--PHP-mixed-$random_hash
Content-Type: multipart/alternative; boundary=\"PHP-alt-$random_hash\"
--PHP-alt-$random_hash
Content-Type: text/plain; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit
--PHP-alt-$random_hash
Content-Type: text/html; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit
<h4>Hi,</h4>
<p><b>$userfullname</b> and his 10 friends qualified for <b>$venuename</b>. Please find the Excel sheet attached. You can download the detailed report form Bayern3 Admin Panel</p>
<h4>Regards,</h4>
<h4>Bayern3 Team</h4>
--PHP-alt-$random_hash--
--PHP-mixed-$random_hash
Content-Type: application/zip; name=$filename
Content-Transfer-Encoding: base64
Content-Disposition: attachment
$attachment
--PHP-mixed-$random_hash--";
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
//echo $mail_sent ? "Mail sent" : "Mail failed";
if($mail_sent === True)
{
//echo "Mail Sent";
}
else{
//echo "Mail Failed";
}