2012-06-28 47 views
34

如何使用PHP发送带格式图片的HTML格式电子邮件? 我想要一个页面,其中包含一些设置和通过电子邮件发送到地址的HTML输出。我该怎么办? 主要问题是附上files.how我可以做到这一点?通过PHP在电子邮件中发送HTML?

+0

是你的问题发送'HTML'代码?或一般使用PHP发送电子邮件? – Adi

回答

80

这是很简单的,在服务器上保留的图像和发送PHP + CSS来他们...

$to = '[email protected]'; 

$subject = 'Website Change Reqest'; 

$headers = "From: " . strip_tags($_POST['req-email']) . "\r\n"; 
$headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n"; 
$headers .= "CC: [email protected]\r\n"; 
$headers .= "MIME-Version: 1.0\r\n"; 
$headers .= "Content-Type: text/html; charset=UTF-8\r\n"; 

$message = '<p><strong>This is strong text</strong> while this is not.</p>'; 


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

正是这一行告诉邮件程序和电子邮件包含收件人(希望)以及形成的HTML,这将需要解释:

$headers .= "Content-Type: text/html; charset=UTF-8\r\n"; 

这是我得到的信息。(link...

您需要安全但链接...

+0

怎么样的CSS格式,图片和其他文件,如js,一个HTML文件需要? – Abadis

+3

@UmerHayat同意了,但我们都必须从某个地方开始,即使它只是在学习如何Google的东西... – Chris

+0

@Aadadis - CSS可以以任何方式完成,嵌入到文件中或按照惯例与外部HTML文件。图像可以打包与我认为(我从来没有做过)或留在服务器上并链接到。如果您不确定它们是如何工作的,请查看HTML图像:P – Chris

2

最简单的方法可能就是使用Zend Framework或任何其他框架(如CakePHP或Symphony)。

您也可以使用标准的mail函数,但您需要更多的关于如何附加图片的知识。

或者,只将图像托管在服务器上而不是附加它们。发送HTML邮件记录在mail功能文档中。

+1

添加框架是过度杀伤IMO :) – gonatee

+0

@gonatee取决于什么级别你想要的质量。接受的答案不适用于所有人,但最基本的情况。如果你需要基本的例子,那么使用'mail()'就可以了。如果您需要更可靠的解决方案或者具有复杂的情况或需求,最好依赖已经通过几十个RFC文档和多年修复bug的框架或库。如果你能找到一个专用的库或框架,让你选择你想要的功能,没有什么理由依靠自己的想法。 – Martijn

15

您需要使用图像的绝对路径编码您的html。通过绝对路径意味着你必须上传图像在服务器和图像的src属性,你必须给这个直接路径<img src="http://yourdomain.com/images/example.jpg">

以下是为您REFFERENCE PHP代码: - 它从http://www.php.net/manual/en/function.mail.php

<?php 
// multiple recipients 
$to = '[email protected]' . ', '; // note the comma 
$to .= '[email protected]'; 

// subject 
$subject = 'Birthday Reminders for August'; 

// message 
$message = ' 
    <p>Here are the birthdays upcoming in August!</p> 
'; 

// To send HTML mail, the Content-type header must be set 
$headers = 'MIME-Version: 1.0' . "\r\n"; 
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n"; 

// Additional headers 
$headers .= 'To: Mary <[email protected]>, Kelly <[email protected]>' . "\r\n"; 
$headers .= 'From: Birthday Reminder <[email protected]>' . "\r\n"; 


// Mail it 
mail($to, $subject, $message, $headers); 
?> 
7

采取我有这个代码,它将完美运行我的网站

public function forgotpassword($pass,$name,$to) 
    { 
     $body ="<table width=100% border=0><tr><td>"; 
     $body .= "<img width=200 src='"; 
     $body .= $this->imageUrl(); 
     $body .="'></img></td><td style=position:absolute;left:350;top:60;><h2><font color = #346699>PMS Pvt Ltd.</font><h2></td></tr>"; 
     $body .='<tr><td colspan=2><br/><br/><br/><strong>Dear '.$name.',</strong></td></tr>'; 
     $body .= '<tr><td colspan=2><br/><font size=3>As per Your request we send Your Password.</font><br/><br/>Password is : <b>'.$pass.'</b></td></tr>'; 
     $body .= '<tr><td colspan=2><br/>If you have any questions, please feel free to contact us at:<br/><a href="mailto:[email protected]" target="_blank">[email protected]</a></td></tr>'; 
     $body .= '<tr><td colspan=2><br/><br/>Best regards,<br>The PMS Team.</td></tr></table>'; 
     $subject = "Forgot Password"; 
     $this->sendmail($body,$to,$subject); 
    } 

邮件功能

function sendmail($body,$to,$subject) 
     { 
      //require_once 'init.php'; 


      $from='[email protected]';  
      $headersfrom=''; 
      $headersfrom .= 'MIME-Version: 1.0' . "\r\n"; 
      $headersfrom .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 
      $headersfrom .= 'From: '.$from.' '. "\r\n"; 
      mail($to,$subject,$body,$headersfrom); 
     } 

图片url功能用于如果你想改变你有图片的图片ge只有一个功能我有很多邮件功能,比如忘记密码创建用户那里我使用图片url功能可以直接设置路径。

function imageUrl() 
    { 
     return "http://".$_SERVER['SERVER_NAME'].substr($_SERVER['SCRIPT_NAME'], 0, strrpos($_SERVER['SCRIPT_NAME'], "/")+1)."images/capacity.jpg"; 
    } 
+1

谢谢,这就是我需要的 – Abadis

+0

@Jalpesh因此,对于CSS文件,你只需要做同样的事情?引用绝对路径,它应该工作,对吧? –

+1

@FernandoSilva你必须设置绝对路径,并设置类或ID为元素,所以根据你的CSS将工作 – Jalpesh

2

您可以通过PHP轻松发送带HTML内容的电子邮件。使用以下脚本。

<?php 
$to = '[email protected]'; 
$subject = "Send HTML Email Using PHP"; 

$htmlContent = ' 
<html> 
<body> 
    <h1>Send HTML Email Using PHP</h1> 
    <p>This is a HTMl email using PHP by CodexWorld</p> 
</body> 
</html>'; 

// Set content-type header for sending HTML email 
$headers = "MIME-Version: 1.0" . "\r\n"; 
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n"; 

// Additional headers 
$headers .= 'From: CodexWorld<[email protected]>' . "\r\n"; 
$headers .= 'Cc: [email protected]' . "\r\n"; 
$headers .= 'Bcc: [email protected]' . "\r\n"; 

// Send email 
if(mail($to,$subject,$htmlContent,$headers)): 
    $successMsg = 'Email has sent successfully.'; 
else: 
    $errorMsg = 'Email sending fail.'; 
endif; 
?> 

的源代码,并现场演示可以从这里找到 - Send Beautiful HTML Email using PHP

4

发送HTML电子邮件不是来自发送使用PHP正常邮件太大的不同。需要添加的是php mail()函数头部参数的内容类型。这是一个例子。

<?php 
    $to = "[email protected]"; 
    $subject = "HTML email"; 
    $message = " 
    <html> 
    <head> 
    <title>HTML email</title> 
    </head> 
    <body> 
    <p>A table as email</p> 
    <table> 
    <tr> 
    <th>Firstname</th> 
    <th>Lastname</th> 
    </tr> 
    <tr> 
    <td>Fname</td> 
    <td>Sname</td> 
    </tr> 
    </table> 
    </body> 
    </html> 
    "; 
    // Always set content-type when sending HTML email 
    $headers = "MIME-Version: 1.0" . "\r\n"; 
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\b"; 
    $headers .= 'From: name' . "\r\n"; 
    mail($to,$subject,$message,$headers); 
?> 

您还可以检查here通过W3Schools的更详细的解释