发送电子邮件附件php无法正常工作

时间:2015-04-02 05:12:50

标签: php html-email email-attachments

我想用PHP电子邮件发送附件但不能正常工作。 我将图片上传到我的服务器文件夹,我想通过电子邮件在gmail上发送2.doc。 任何解决方案。 我收到消息"消息已发送! "但未收到电子邮件或附件文件。

enter image description here

<?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));
$uid = md5(uniqid(time()));
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n";
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--".$uid."--";
// Messages for testing only, nobody will see them unless this script URL is visited manually
if (mail($mailto, $subject, "", $header)) {
echo "Message sent!";
} else {
echo "ERROR sending message.";
}
}
// Only accept POSTs from authenticated source
// EDIT FROM HERE DOWN TO
// CUSTOMIZE EMAIL
// File to attach
$my_file = "2.doc";
$my_path = ''; // $_SERVER['DOCUMENT_ROOT']."/your_path_here/";
// Who email is FROM
$my_name = "Your Name (or) Your Business";
$my_mail = "myemail@yahoo.com";
$my_replyto = "myemail@yahoo.com";
// Whe email is going TO
$to_email = "toemail@gmail..com"; // Comes from Wufoo WebHook
// Subject line of email
$my_subject = "Your file has arrived!";
// Content of email message (Text only)
$requester = $_POST['Field101']; // Comes from Wufoo WebHook
$message = "Hey $requester, Your custom email message goes here";
// Call function to send email
mail_attachment($my_file, $my_path, $to_email, $my_mail, $my_name, $my_replyto, $my_subject, $message);
?> 

2 个答案:

答案 0 :(得分:1)

你在寻找这样的东西:

这适用于这种情况:

<?php 
function mail_attachment($filename, $mailto, $from_mail, $from_name, $replyto, $subject, $message) { 
$body = ''; 
$headers = ''; 

$file = $filename; 
$separator = md5(time()); 
$attachment = chunk_split(base64_encode(file_get_contents($file))); 
$headers = "From: ".$from_name."<".$from_mail.">" . PHP_EOL; 
$headers .= "Reply-To: ".$replyto . PHP_EOL; 
$headers .= "MIME-Version: 1.0".PHP_EOL; 
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . PHP_EOL . PHP_EOL; 
$body .= "Content-Transfer-Encoding: 7bit" . PHP_EOL; 
$body .= "This is a MIME encoded message." . PHP_EOL; 
$body .= "--" . $separator . PHP_EOL; 
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"" . PHP_EOL; 
$body .= "Content-Transfer-Encoding: 8bit" . PHP_EOL . PHP_EOL; 
$body .= $message . PHP_EOL; 
$body .= "--" . $separator . PHP_EOL; 
$body .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"" . PHP_EOL; 
$body .= "Content-Transfer-Encoding: base64" . PHP_EOL; 
$body .= "Content-Disposition: attachment" . PHP_EOL . PHP_EOL; 
$body .= $attachment . PHP_EOL; 
$body .= "--" . $separator . "--"; 
if (mail($mailto, $subject, $body, $headers)) { 
echo "Message sent!"; 
} else { 
echo "ERROR sending message."; 
    } 
} 

$my_file = "2.doc"; 
$my_name = "Your Name (or) Your Business"; 
$my_mail = "myemail@yahoo.com"; 
$my_replyto = "replyemail@gmail.com"; 
$to_email = "replyemail@gmail.com"; 
$my_subject = "Your file has arrived!"; 
$requester = $_POST['Field101']; 
$message = "Hey $requester, Your custom email message goes here"; 
mail_attachment($my_file, $to_email, $my_mail, $my_name, $my_replyto, $my_subject, $message); 
?>

答案 1 :(得分:0)

我建议您下载PHPMailer(https://github.com/PHPMailer/PHPMailer)并尝试如下所述: https://stackoverflow.com/a/17818607/982075