2013-12-17 106 views
0

我想用PHP发送带有图片和CSS样式表的电子邮件中的HTML页面。我怎样才能添加我的图像和我的CSS样式表上传图片在服务器?通过PHP在电子邮件中发送HTML页面

下面是sendind电子邮件中的PHP代码:

<?php 
$to = "[email protected]"; 

// 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 = "[email protected]"; 

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

我不认为它真的会去你想要的。大多数电子邮件客户端将不会显示您想要的电子邮件。 – putvande

+0

如果图像不在服务器上,您如何引用图像?图片必须位于服务器上。 –

+0

对图像使用绝对路径,您将需要放入内联样式,因为在电子邮件中不支持应用CSS。 – Anup

回答

0

对于复杂的电子邮件,您应该使用类似美妙的PHPMailer类的梅勒类。

这个类很容易发送复杂的邮件(如HTML邮件)。您可以在PHPMailer的示例文件夹中找到示例。

+0

OP是要求图像引用,而不是如何发布HTML电子邮件。 –

+0

'我想发送一个HTML页面' - 听起来像是他想发送一封带有图片的HTML电子邮件。 PHPMailer是一个很好的方法来做到这一点。 –

+0

“我怎样才能添加我的图像和我的CSS样式表上传图片在服务器?” –

0

您可以添加发送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; 
相关问题