我想使用PHP在带有图片和CSS样式表的电子邮件中发送HTML页面。如何在服务器中上传图像时添加我的图像和我的CSS样式表?
以下是sendind电子邮件的PHP代码:
<?php
$to = "my.account@gmail.com";
// subject
$subject = "Test mail";
// message
$message = file_get_contents("index.html"); // index.html contains images and css stylesheet which are not displayin in the email
// from
$from = "my.account@gmail.com";
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= "From:" . $from;
// Mail it
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>
非常感谢你的帮助。提前谢谢。
答案 0 :(得分:0)
对于复杂的电子邮件,您应该使用Mailer类,如精彩的PHPMailer类。
使用此类,可以轻松发送复杂的邮件(如HTML邮件)。您可以在PHPMailer的examples文件夹中找到示例。
答案 1 :(得分:0)
您可以像这样添加send html文件:
$file_name = basename($file); // Get file name
$data = file_get_contents($file); // Read file contents
$file_contents = chunk_split(base64_encode($data)); // Encode file data into base64
$uid = md5(time()); // Create unique boundary from timestamps
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "From: {$from_name}<{$mail_from}>";
$headers[] = "Reply-To: {$mail_from}";
$headers[] = "Content-Type: multipart/mixed; boundary=\"{$uid}\"";
$headers[] = "This is a multi-part message in MIME format.";
$headers[] = "--{$uid}";
$headers[] = "Content-type:text/plain; charset=iso-8859-1"; // Set message content type
$headers[] = "Content-Transfer-Encoding: 7bit";
$headers[] = $message; // Dump message
$headers[] = "--{$uid}";
$headers[] = "Content-Type: application/octet-stream; name=\"{$file_name}\""; // Set content type and file name
$headers[] = "Content-Transfer-Encoding: base64"; // Set file encoding base
$headers[] = "Content-Disposition: attachment; filename=\"{$file_name}\""; // Set file Disposition
$headers[] = $file_contents; // Dump file
// Send mail with header information
if (mail($mail_to, $subject, '', implode("\r\n", $headers) ))
return true;