使用内嵌图像发送电子邮件

时间:2015-05-18 05:18:31

标签: php image email

我发送电子邮件的代码

<?php
$to = 'piyu.mulay@gmail.com';

$subject = 'Website Change Request';

$headers = "From: chintamani.mulay@gmail.com \r\n";
$headers .= "CC: chintamani.mulay@gmail.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

$message = '<html><body>';
$message .= '<h1>Hello, World!</h1>';
$message .= '<img src="../1.jpg">';
$message .= '</body></html>';

echo mail($to, $subject, $message, $headers);
?> 

当我在<img src="http://weber-steinach.de/files/339349">中添加链接时效果很好但是当我放置在 localserver 中的文件路径时,我将不会在电子邮件中显示和图像。

1 个答案:

答案 0 :(得分:5)

最后我才知道如何做到这一点。以下是我所知道的来源link        

$bound_text = "----*%$!$%*";
$bound = "--".$bound_text."\r\n";
$bound_last = "--".$bound_text."--\r\n";

$headers = "From: youremail@host.com\r\n";
$headers .= "MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed; boundary=\"$bound_text\""."\r\n" ;

$message = " you may wish to enable your email program to accept HTML \r\n".
$bound;

$message .=
'Content-Type: text/html; charset=UTF-8'."\r\n".
'Content-Transfer-Encoding: 7bit'."\r\n\r\n".
'

<html>

<head>
<style> p {color:green} </style>
</head>
<body>

A line above
<br>
<img src="cid:http://localhost/a/img/1.jpg">
<br>
a line below
</body>

</html>'."\n\n".
$bound;

$file = file_get_contents("http://localhost/a/img/1.jpg");

$message .= "Content-Type: image/jpeg; name=\"http://localhost/a/img/1.jpg\"\r\n"
."Content-Transfer-Encoding: base64\r\n"
."Content-ID: <http://localhost/a/img/1.jpg>\r\n"
."\r\n"
.chunk_split(base64_encode($file))
.$bound_last;

echo mail($to, $subject, $message, $headers) ;

?>