Car*_*ima 73
我强烈建议使用像PHPMailer这样的库来发送电子邮件.
它更容易,并自动处理大多数问题.
关于显示嵌入式(内联)图像,这里是他们的文档:
内联附件
还有一种添加附件的方法.如果要制作带有图像并入桌面的HTML电子邮件,则需要附加图像,然后将标记链接到该图像.例如,如果您使用CID my-photo将图像添加为内联附件,则可以使用HTML电子邮件访问该图像
<img src="cid:my-photo" alt="my-photo" />
.具体来说,这是添加内联附件的功能:
$mail->AddEmbeddedImage(filename, cid, name);
//By using this function with this example's value above, results in this code:
$mail->AddEmbeddedImage('my-photo.jpg', 'my-photo', 'my-photo.jpg ');
Run Code Online (Sandbox Code Playgroud)
为了给你一个更完整的例子,说明它是如何工作的:
<?php
require_once('../class.phpmailer.php');
$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
$mail->IsSMTP(); // telling the class to use SMTP
try {
$mail->Host = "mail.yourdomain.com"; // SMTP server
$mail->Port = 25; // set the SMTP port
$mail->SetFrom('name@yourdomain.com', 'First Last');
$mail->AddAddress('whoto@otherdomain.com', 'John Doe');
$mail->Subject = 'PHPMailer Test';
$mail->AddEmbeddedImage("rocks.png", "my-attach", "rocks.png");
$mail->Body = 'Your <b>HTML</b> with an embedded Image: <img src="cid:my-attach"> Here is an image!';
$mail->AddAttachment('something.zip'); // this is a regular attachment (Not inline)
$mail->Send();
echo "Message Sent OK<p></p>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
?>
Run Code Online (Sandbox Code Playgroud)
关于你的评论,你问过如何发送带有嵌入式图像的HTML 电子邮件,所以我给你举了一个如何做到这一点的例子.
我告诉你的图书馆可以使用除SMTP之外的许多方法发送电子邮件.有关其他示例,
请查看PHPMailer示例页面.
不管怎样,如果你不想以库支持的方式发送电子邮件,你可以(应该)仍然使用库来构建消息,然后按照你想要的方式发送它.
例如:
您可以替换发送电子邮件的行:
$mail->Send();
Run Code Online (Sandbox Code Playgroud)
有了这个:
$mime_message = $mail->CreateBody(); //Retrieve the message content
echo $mime_message; // Echo it to the screen or send it using whatever method you want
Run Code Online (Sandbox Code Playgroud)
希望有所帮助.如果您在使用它时遇到麻烦,请告诉我.
Art*_*lma 30
我正在使用此功能查找我信中的所有图像并将其附加到消息中.
参数:获取您的HTML(您要发送的);
返回:必要的HTML和标题,您可以使用mail()
;
用法示例:
define("DEFCALLBACKMAIL", "yourmail@yourdomain.com"); // WIll be shown as "from".
$final_msg = preparehtmlmail($html); // give a function your html*
mail('your@mail.com', 'your subject', $final_msg['multipart'], $final_msg['headers']);
// send email with all images from html attached to letter
function preparehtmlmail($html) {
preg_match_all('~<img.*?src=.([\/.a-z0-9:_-]+).*?>~si',$html,$matches);
$i = 0;
$paths = array();
foreach ($matches[1] as $img) {
$img_old = $img;
if(strpos($img, "http://") == false) {
$uri = parse_url($img);
$paths[$i]['path'] = $_SERVER['DOCUMENT_ROOT'].$uri['path'];
$content_id = md5($img);
$html = str_replace($img_old,'cid:'.$content_id,$html);
$paths[$i++]['cid'] = $content_id;
}
}
$boundary = "--".md5(uniqid(time()));
$headers .= "MIME-Version: 1.0\n";
$headers .="Content-Type: multipart/mixed; boundary=\"$boundary\"\n";
$headers .= "From: ".DEFCALLBACKMAIL."\r\n";
$multipart = '';
$multipart .= "--$boundary\n";
$kod = 'utf-8';
$multipart .= "Content-Type: text/html; charset=$kod\n";
$multipart .= "Content-Transfer-Encoding: Quot-Printed\n\n";
$multipart .= "$html\n\n";
foreach ($paths as $path) {
if(file_exists($path['path']))
$fp = fopen($path['path'],"r");
if (!$fp) {
return false;
}
$imagetype = substr(strrchr($path['path'], '.' ),1);
$file = fread($fp, filesize($path['path']));
fclose($fp);
$message_part = "";
switch ($imagetype) {
case 'png':
case 'PNG':
$message_part .= "Content-Type: image/png";
break;
case 'jpg':
case 'jpeg':
case 'JPG':
case 'JPEG':
$message_part .= "Content-Type: image/jpeg";
break;
case 'gif':
case 'GIF':
$message_part .= "Content-Type: image/gif";
break;
}
$message_part .= "; file_name = \"$path\"\n";
$message_part .= 'Content-ID: <'.$path['cid'].">\n";
$message_part .= "Content-Transfer-Encoding: base64\n";
$message_part .= "Content-Disposition: inline; filename = \"".basename($path['path'])."\"\n\n";
$message_part .= chunk_split(base64_encode($file))."\n";
$multipart .= "--$boundary\n".$message_part."\n";
}
$multipart .= "--$boundary--\n";
return array('multipart' => $multipart, 'headers' => $headers);
}
Run Code Online (Sandbox Code Playgroud)
don*_*nis 15
PHPMailer能够自动嵌入HTML电子邮件中的图像.在编写HTML时,您必须在文件系统中提供完整路径:
<img src="/var/www/host/images/photo.png" alt="my photo" />
Run Code Online (Sandbox Code Playgroud)
它将自动转换为:
<img src="cid:photo.png" alt="my photo" />
Run Code Online (Sandbox Code Playgroud)