Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP发送带有图像附件的电子邮件_Php_Email_Attachment - Fatal编程技术网

PHP发送带有图像附件的电子邮件

PHP发送带有图像附件的电子邮件,php,email,attachment,Php,Email,Attachment,我正在尝试创建一个脚本,将从我正在开发的iphone应用程序上传的图像通过电子邮件发送给我 我已经能够得到脚本发送给我一封带有附件的电子邮件。但是,附件始终为0kb 这是我的密码: // Standard email info $to = 'example@example.com'; $subject = 'Email with an image attached'; // This variable will be used when declaring the "boundar

我正在尝试创建一个脚本,将从我正在开发的iphone应用程序上传的图像通过电子邮件发送给我

我已经能够得到脚本发送给我一封带有附件的电子邮件。但是,附件始终为0kb

这是我的密码:

// Standard email info


$to = 'example@example.com'; 
 $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: <example@example.com>\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"; 
//标准电子邮件信息
$to$example@example.com'; 
$subject='附有图像的电子邮件';
//声明“边界”时将使用此变量
//对于电子邮件的不同部分
$boundary=md5(日期('r',时间());
//初始标题

$headers=“MIME版本:1.0\r\n”;// 虽然有一些函数可以实现这一点,但对于初级程序员来说,这是一个多么棒的练习啊

写得很好。真正阅读,而不仅仅是剪切和粘贴,对人们会有好处。如果您使用php发送电子邮件,您应该了解这些基本概念

可能是掩盖真实代码的一些疏忽:

第23行应为:

也就是说,
$fname
应该是
$filename

第26行应为:

未定义
$x
$files[$x]


@:我想提醒您,您应该始终清理输入(电子邮件地址)。

并且结束mime边界应以
--
结尾,因此第29行应为:

$message .= "--{$mime_boundary}--\n";

不要建立自己的mime电子邮件。使用PHPMailer或Swiftmailer。它们都使代码变得简单,并将代码缩减为几行。如何附加多个图像文件@mti2935
$data = fread($file,filesize($filename));
$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"".$fname."\"\n" .
$message .= "--{$mime_boundary}--\n";