PHP 发送带有文件附件的电子邮件

编程入门 行业动态 更新时间:2024-10-28 03:27:35
本文介绍了PHP 发送带有文件附件的电子邮件 - 根本不发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在尝试阅读有关在 PHP 中发送带附件的电子邮件的各种文章后(我使用 ASP 和 VBScript),我编写了下面的代码.不幸的是,它根本不起作用.它不仅没有发送带有附件的电子邮件,而且电子邮件似乎根本没有发送,即使我的脚本说它确实发送了.我哪里错了?我没有使用表单上传文件.这是一个静态脚本.

After trying to read various articles on sending emails with attachments in PHP (I am use to ASP with VBScript), I wrote the code below. Unfortunately, it does not work at all. Not only does it not send the email with the attachment, the email doesn't seem to send at all, even though my script says that it did send. Where have I gone wrong? I'm not using a form to upload a file. This is a static script.

<?php $EmailTo = "Me@here"; $EmailFrom = "You@There"; $EmailSubject = "The Email Subject"; $MailBoundary = md5(uniqid(time())); $Headers = "To: ". $EmailTo . " "; $Headers .= "From: ". $EmailFrom . " "; $Headers = "MIME-Version: 1.0 "; $Headers .= "Content-type: multipart/mixed;boundary="$MailBoundary ""; $Headers .= " "; $Headers .= "This is a multi-part message in MIME format."; $Headers .= " "; $FileAttachment = "AttachedFile.pdf"; $File = fopen($FileAttachment, "r"); $FileData = fread($File, filesize($FileAttachment)); $FileData = chunk_split(base64_encode($FileData)); $FileName = basename($FileAttachment); $EmailBody = "--$MailBoundary "; $EmailBody .= "Content-type: text/html; charset=iso-8859-1 "; $EmailBody .= "Content-transfer-encoding: 8bit "; $EmailBody .= "<html>" . chr(13) . "<head>" . chr(13) . "<style>" . chr(13) . ".breg {font-family:arial;font-size:10pt;color:#000000;padding:5px;}" . chr(13) . "</style>" . chr(13) . "</head>" . chr(13) . "<body>" . chr(13) . "<div class=" . chr(34) . "breg" . chr(34) . ">" . chr(13) . "The message text body goes here" . chr(13) . "</div>" . chr(13) . "</body>" . chr(13) . "</html>"; $EmailBody .= "--$MailBoundary "; $EmailBody .= "Content-type: " . mime_content_type($File) . "; name=$FileName "; $EmailBody .= "Content-transfer-encoding:base64 "; $EmailBody .= $FileData. " "; $EmailBody .= " --$MailBoundary--"; if (mail($EmailTo, $EmailSubject, $EmailBody, $Headers)) { echo "Email to " . $EmailTo . " has been sent" . chr(13) . "<br />" . chr(13); } else { echo "<b>Email to " . $EmailTo . " was not sent</b>" . chr(13) . "<br />" . chr(13); } ?>

推荐答案

For sending mail with attachment using php mail().Try this code: <?php //If there is no error, send the email if(isset($_POST['ur_submit_button_name'])) { $EmailTo = "Me@here"; $EmailFrom = "You@There"; $EmailSubject = "The Email Subject"; $separator = md5(time()); // carriage return type (we use a PHP end of line constant) $eol = PHP_EOL; // attachment name $filename = "ip.zip";//store that zip file in ur root directory $attachment = chunk_split(base64_encode(file_get_contents('ip.zip'))); // main header $headers = "From: ".$from.$eol; $headers .= "MIME-Version: 1.0".$eol; $headers .= "Content-Type: multipart/mixed; boundary="".$separator."""; // no more headers after this, we start the body! // $body = "--".$separator.$eol; $body .= "Content-Transfer-Encoding: 7bit".$eol.$eol; $body .= "This is a MIME encoded message.".$eol; // message $body .= "--".$separator.$eol; $body .= "Content-Type: text/html; charset="iso-8859-1"".$eol; $body .= "Content-Transfer-Encoding: 8bit".$eol.$eol; $body .= $message.$eol; // attachment $body .= "--".$separator.$eol; $body .= "Content-Type: application/octet-stream; name="".$filename.""".$eol; $body .= "Content-Transfer-Encoding: base64".$eol; $body .= "Content-Disposition: attachment".$eol.$eol; $body .= $attachment.$eol; $body .= "--".$separator."--"; // send message if (mail($to, $subject, $body, $headers)) { $mail_sent=true; echo "mail sent"; } else { $mail_sent=false; echo "Error,Mail not sent"; } } ?>

更多推荐

PHP 发送带有文件附件的电子邮件

本文发布于:2023-05-27 19:32:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/300551.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:词库加载错误:Could not find file &#039;D:\淘小白 高铁采集器win10\Configuration\Dict_Sto

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!