PHP 发送带有图像附件的电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17368770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP send an email with image attachment
提问by Thomas Spade
I'm trying to create a script that will send an image uploaded from an iphone app I'm developing to me in an email.
我正在尝试创建一个脚本,该脚本将从我正在开发的 iphone 应用程序上传的图像通过电子邮件发送给我。
I've been able to get the script to send me an email with an attachment. However, the attachment is always 0kb.
我已经能够让脚本给我发送一封带有附件的电子邮件。但是,附件始终为 0kb。
Here is my code:
这是我的代码:
// Standard email info
$to = '[email protected]';
$subject = 'Email with an image attached';
// This variable will be used when declaring the "boundaries"
// for the different sections of the email
$boundary = md5(date('r', time()));
//Initial Headers
$headers = "MIME-Version: 1.0\r\n"; // <- the "\r\n" indicate a carriage return and newline, respectively
$headers .= "From: <[email protected]>\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=" . $boundary . "\r\n"; // <- This is
// saying the there will be more than one (a "mix") of Content Types in this email.
// The "boundary" value will indicate when each content type will start
//First Content Type
$message = "\r\n\r\n--" . $boundary . "\r\n"; // <- This indicates that I'm going to start
// declaring headers specific to this section of the email.
// MAKE SURE there's only ONE(1) "\r\n" between the above boundry and the first header below (Content-Type)
$message .= "Content-type: text/plain; charset=\"iso-8859-1\"\r\n"; // <- Here I'm saying this content should be plain text
$message .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
// Body of the email for the headers I just declared
$message .= "Someone filled out the form. See their info below:\r\n";
$message .= "Name:";
//Second Content Type
$message .= "\r\n\r\n--" . $boundary . "\r\n"; // <- This idicates that I'm going to start
// declaring some more headers for the content below
// MAKE SURE there's only ONE(1) "\r\n" between the above boundry and the first header below (Content-Type)
$message .= "Content-type: image/jpeg\r\n"; // <- Here I'm saying that this Content Type is for a JPEG image
$message .= "Content-Transfer-Encoding: base64\r\n"; // <- this is saying that this section's content will be base64 Encoded
$message .= "Content-Disposition: attachment; filename=\"Image.jpg\"\r\n"; // <- This is saying the content below should be an attachment and gives it a file name
// The base64_encode below is necessary because this is a file.
$message .= base64_encode(file_get_contents($_FILES["userfile"]["name"]));
$message .= "\r\n\r\n--" . $boundary . "--"; // <- This indicates the end of the boundries. Notice the additional "--" after the boundry's value.
// Send the email using "mail()".
// Adding the "$mail_sent = " before "mail()" will store TRUE in $mail_sent if the email is sent successfully
// Adding the "@" sign before "mail()" will disable error display so users
// won't see the actual error info if it fails, just "Mail failed".
$mail_sent = @mail($to, $subject, $message, $headers);
//Check to see if the email was sent successfully ($mail_sent = true).
//If so, display "Mail Sent" to the screen, else display "Mail Failed".
echo $mail_sent ? "Mail sent" : "Mail failed";
Everything works fine except the attachment is 0kb.
一切正常,除了附件是 0kb。
Any help is appreciated.
任何帮助表示赞赏。
Thanks.
谢谢。
回答by mti2935
// to, from, subject, message body, attachment filename, etc.
$to = "[email protected]";
$from = "[email protected]";
$subject = "subject";
$message = "this is the message body";
$filename = "/home/user/file.jpeg";
$fname = "file.jpeg";
$headers = "From: $from";
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// multipart boundary
$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
$message .= "--{$mime_boundary}\n";
// preparing attachments
$file = fopen($filename,"rb");
$data = fread($file,filesize($filename));
fclose($file);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"".$fname."\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"$fname\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$message .= "--{$mime_boundary}--\n";
// send
//print $message;
$ok = @mail($to, $subject, $message, $headers, "-f " . $from);
回答by Dan Ross
While there are canned functions for this, what a fantastic exercise for a junior programmer!
虽然有针对此的预制函数,但对于初级程序员来说,这是一个多么棒的练习啊!
Very well written mti2935. It would do folks good to actually read and not just cut-and-paste. If you're sending email with php, you should understand these fundamental concepts.
mti2935写得很好。真正阅读而不只是剪切和粘贴对人们有好处。如果您使用 php 发送电子邮件,您应该了解这些基本概念。
Probably some oversights from masking your real code:
掩饰您的真实代码可能存在一些疏忽:
Line 23 should be:
第 23 行应该是:
$data = fread($file,filesize($filename));
That is, $fname
should be $filename
.
也就是说,$fname
应该是$filename
。
Line 26 should be:
第 26 行应该是:
$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"".$fname."\"\n" .
Neither $x
nor $files[$x]
are defined.
既不定义$x
也不$files[$x]
定义。
@Thomas Spade: I'd like to remind that you should always sanitize input (email address).
@ Thomas Spade:我想提醒您应该始终清理输入(电子邮件地址)。
回答by Siebren
And the closing mime boundary should end in --
, so line 29should read:
并且结束 mime 边界应以 结尾--
,因此第 29 行应为:
$message .= "--{$mime_boundary}--\n";