2015-08-26 70 views
0

我能够发送仅包含文本的电子邮件,但没有一个脚本正在发送HTML。我有认证关闭,谷歌应用程序是基于服务器的IPPHP Pear Mail HTML版本

$message = 'Blah blah blah'; 


require_once "Mail.php"; 
$from = "mycompany <[email protected]>"; 
$to = $fName.$lName." <".$email.">"; 
$subject = "Hello from mycompany!"; 
$body = 

'Dear '.$fName.', 

Thank you for your interest in mycompany. We have received your inquiry and will contact you within 24 hours. 

Thanks, 

Timothy Elliott - Owner/CEO mycompany'; 

         $host = "tls://smtp-relay.gmail.com"; 
         $port = "465"; 
         $username = ""; 
         $password = ""; 
         $headers = array (
         'From' => $from, 
         'To' => $to, 
         'Subject' => $subject); 
         $smtp = Mail::factory('smtp', 
         array ('host' => $host, 
          'port' => $port, 
          'auth' => false, 
          'username' => $username, 
          'password' => $password)); 
         $mail = $smtp->send($to, $headers, $body); 

if (PEAR::isError($mail)) { 
echo("There was an error, please try again."); 
} 

此代码工作得很好验证,但我不能将它作为发送HTML邮件。

+0

有没有简单的方法来与梨邮件发送HTML格式的邮件。你可能会考虑使用类似http://swiftmailer.org/或http://framework.zend.com/manual/current/en/modules/zend.mail.introduction.html – bspellmeyer

+0

哦,很高兴知道!我在共享服务器上,但无法安装任何脚本:(Dang fatcow !! :( –

+0

)无需安装。您可以将Zend Mail和Swift Mailer与您自己的PHP脚本并行上传,不需要任何特殊设置 – bspellmeyer

回答

2

使用PEAR Mail很容易 - 只需使用PEAR Mail_Mime编码外发电子邮件的HTML部分即可。

Here's an example of how.

扩大你的代码这一点,它应该是这个样子:

<?php 
require_once "Mail.php"; 
require_once "Mail/mime.php"; 

$from = "mycompany <[email protected]>"; 
$to = $fName.$lName." <".$email.">"; 
$subject = "Hello from mycompany!"; 
$html = <<< HTML 
<b>Dear $fName</b>, 
<p> 
Thank you for your interest in mycompany. We have received your inquiry and will contact you within 24 hours. 
</p> 
Thanks,<br/> 

<i>Timothy Elliott - Owner/CEO mycompany</i> 
HTML; 

$host = "tls://smtp-relay.gmail.com"; 
$port = "465"; 
$username = ""; 
$password = ""; 
$headers = [ 
    'From' => $from, 
    'To' => $to, 
    'Subject' => $subject 
]; 

$crlf = "\n"; 
$mime = new Mail_mime(['eol' => $crlf]); 
$mime->setHTMLBody($html); 
$body = $mime->get(); 
$headers = $mime->headers($headers); 


$smtp = Mail::factory('smtp', 
    [ 
    'host' => $host, 
    'port' => $port, 
    'auth' => false, 
    'username' => $username, 
    'password' => $password 
    ] 
); 
$mail = $smtp->send($to, $headers, $body); 

if (PEAR::isError($mail)) { 
    echo("There was an error, please try again."); 
} 
+0

html部分中的某些内容会给我PHP错误...我的脚本结尾处出现意外结束 –

+0

如果您'使用heredoc符号来设置HTML,您需要确保结束标记“HTML;”位于行首的起始位置,并且之前没有空格/制表符。 – kguest