PHP发送带有图像附件的电子邮件 [英] PHP send an email with image attachment

查看:145
本文介绍了PHP发送带有图像附件的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个脚本,它会发送一个从我正在开发的iphone应用程序上传的图像到电子邮件。



我已经能够让脚本向我发送附有电子邮件的附件。但是,附件总是0kb。



这是我的代码:

  //标准电子邮件信息


$ to ='example@example.com';
$ subject ='附带图片的电子邮件';

//当电子邮件的不同部分声明边界
//时,将使用此变量
$ boundary = md5(date('r',time ()));

//初始头文件
$ headers =MIME-Version:1.0\r\\\
; //< - \r\\\
分别表示回车符和换行符
$ headers。=From:< example@example.com> \r\\\
;
$ headers。=Content-Type:multipart / mixed; boundary =。 $边界。 \r\\\
; //< - 这是
//说这个电子邮件中会有多个(混合)的内容类型。
//边界值将指示何时启动每个内容类型

//第一个内容类型
$ message =\r\\\
\r\\ \\ n--。 $边界。 \r\\\
; //< - 这表示我要开始
//声明特定于电子邮件这一部分的标题。
//确认上面的边界和下面的第一个标题之间只有一个(1)\r\\\
(Content-Type)
$ message。=内容类型:文本/ plain; charset = \iso-8859-1\\r\\\
; //< - - 这里我说这个内容应该是纯文本
$ message。=Content-Transfer-Encoding:7bit\r\\\
\r\\\
;

//标题的电子邮件正文我刚刚声明
$ message。=有人填写了表单,请看下面的信息:\r\\\
;
$ message。=Name:;

//第二个内容类型
$ message。=\r\\\
\r\\\
--。 $边界。 \r\\\
; //< - 这表明我要开始
//声明一些更多的标题为
下面的内容//确认只有一个(1)\r\\\
在上面的边界和下面的第一个标题之间(Content-Type)
$ message。=Content-type:image / jpeg\r\\\
; //< - - 这里我说这个内容类型是用于JPEG图像
$ message。=Content-Transfer-Encoding:base64\r\\\
; //< - 这就是说这部分的内容将是base64 Encoded
$ message。=Content-Disposition:attachment; filename = \Image.jpg\\r\\\
; //< - 这是说下面的内容应该是附件,并给它一个文件名

//下面的base64_encode是必需的,因为这是一个文件。
$ message。= base64_encode(file_get_contents($ _ FILES [userfile] [name]));

$ message。=\r\\\
\r\\\
--。 $边界。 - ; //< - 这表示边界的结束。注意边界值之后的附加 - 。

//使用mail()发送电子邮件。
//如果电子邮件成功发送,则在mail()之前添加$ mail_sent =将在$ mail_sent中存储TRUE
//在mail()之前添加@符号将禁用错误显示,所以用户
//如果失败,将看不到实际的错误信息,只是邮件失败。
$ mail_sent = @mail($ to,$ subject,$ message,$ headers);

//检查邮件是否已成功发送($ mail_sent = true)。
//如果是这样,显示发送邮件到屏幕,否则显示邮件失败。
echo $ mail_sent? 邮件发送:邮件失败;

一切正常,除了附件是0kb。



谢谢。

解决方案

  // to,from,subject,message body,attachment filename等。
$ to =to@to.com;
$ from =from@from.com;
$ subject =subject;
$ message =这是消息体;
$ 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。=\\\
MIME-Version:1.0\\\
。 Content-Type:multipart / mixed; \\\
。 boundary = \{$ mime_boundary} \;

// multipart boundary
$ message =这是MIME格式的多部分消息.\\\
\\\
。 - {$ mime_boundary} \\\
。 Content-Type:text / plain; charset = \iso-8859-1\\\\
。 Content-Transfer-Encoding:7bit\\\
\\\
。 $消息。 \\\
\\\
;
$ message。= - {$ mime_boundary} \\\
;

//准备附件
$ file = fopen($ filename,rb);
$ data = fread($ file,filesize($ filename));
fclose($ file);
$ data = chunk_split(base64_encode($ data));
$ message。=Content-Type:{\application / octet-stream\}; \\\
。 name = \$ fname。\\\\

Content-Disposition:attachment; \\\
。 filename = \$ fname\\\\

Content-Transfer-Encoding:base64\\\
\\\
。 $数据。 \\\
\\\
;
$ message。= - {$ mime_boundary} - \\\
;


//发送
//打印$ message;

$ ok = @mail($ to,$ subject,$ message,$ headers,-f。$ from);


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.

I've been able to get the script to send me an email with an attachment. However, the attachment is always 0kb.

Here is my code:

// 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"; 

Everything works fine except the attachment is 0kb.

Any help is appreciated.

Thanks.

解决方案

        // to, from, subject, message body, attachment filename, etc.
        $to = "to@to.com";
        $from = "from@from.com";
        $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);          

这篇关于PHP发送带有图像附件的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆