通过PHP将图像发送到电子邮件

时间:2012-01-25 10:15:07

标签: php

你能帮我解决一下这段代码吗?我试图在我的邮件中嵌入一个图像,并将其显示为字母数字字符串..

function send_mail_pear()
{

    $crlf = "\n";

    $to = "mail@mail.in";
    $head = ("From: mail@mail.in". "\r\n");
    $head .= "Content-type: text/html\r\n";
    $head .= "Content-Type: image/jpeg"; 

    $mime = new Mail_mime($crlf);
    echo $head. "<br/>";
    $mime->setHTMLBody('<html><body><img src="map_6.gif"> <p>You see this image.</p></body></html>');
    $mime->addHTMLImage('map_6.gif', 'image/gif');  

    $subject = "Test mail";
    $mailBody = $mime->get();
    $mail =& Mail::factory('mail');
    echo $mailBody ;

    /*if(mail($to, $subject, $mailBody, $head)){
        echo "successful";
    } else {
        echo "Mail Not sent";
    }
}

3 个答案:

答案 0 :(得分:0)

我相信你必须将图像链接为“cid:yourcontentid”,并指定“yourcontentid”作为addHTMLImage的参数。

您可以参考the documentation了解更多信息。

答案 1 :(得分:0)

你可以使用它来附加它。或者我建议最不痛苦的选择是在一些服务器上托管图像并发送HTML电子邮件。

身体看起来像

<html>
<p>
   <img src='http://yourserver.com/YOURIMAGE.gif' />
</p>
</html>

编辑:

将图片或文件附加到电子邮件中尝试类似这样的内容

$to = "someome@anadress.com";
$from = "SOME NAME ";
$subject = "SUBJECT";
$fileatt = "./FILENAME.gif";
$fileatttype = "image/gif";
$fileattname = "ATTACHMENTNAME.gif";
$headers = "From: $from";

$file = fopen($fileatt, 'rb');
$data = fread($file, filesize($fileatt));
fclose($file);
$data = chunk_split(base64_encode($data));

$rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$rand}x";
$headers .= <<<HEADER
MIME-Version: 1.0\n
Content-Type: multipart/mixed;\n
boundary="{$mime_boundary}"
HEADER;

$message = <<<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
\n\n
–{$mime_boundary}\n
Content-Type: {$fileatttype};\n
name="{$fileattname}"\n
Content-Disposition: attachment;\n
filename="{$fileattname}"\n
Content-Transfer-Encoding: base64\n\n
{$data}\n\n
-{$mime_boundary}-\n
MESSAGE;

if(mail($to, $subject, $message, $headers)) {
  echo "The email was sent.";
}
else {
  echo "There was an error sending the mail.";
}

答案 2 :(得分:0)

Matthias Vance的回答是故事的一部分。

如果你想发送一个自包含的HTML邮件,那么它需要以mhtml类型发送(注意,不同实现的内容类型有所不同 - 它应该是多部分/相关的,但经常是消息/ RFC822)。然而,在RFC 2557中有一个正式的结构定义。每个组件应该是单独的附件。这也意味着必须重写每个URL以使用本地引用和cid方案 - IIRC,PEAR类不会为您执行此操作。

网上有一些文件,例如this one - 但不要陷入相信除了在本地发送电子邮件/保存网页之外的其他任何事情的陷阱 - MSIE无法正确处理通过HTTP传送的mhtml文件。

我不知道有任何现成的包用于生成mhtml文件/电子邮件 - 但有一些用于parsing such files。编写一个工具来做到这一点很简单,但不是一件容易的事。

相关问题