如何发送HTML电子邮件php

时间:2012-11-12 11:17:53

标签: php mass-emails

我正在使用以下代码,以便通过PHP发送HTML电子邮件,请检查我的下面的代码,我在做错了。它发送电子邮件但不是html格式,我已尝试过很多网上可用的例子但无法以html格式收到电子邮件。请帮助我。

 require_once "Mail.php";
 $name="Me";
 $mail_from="info@domain.com";
 $subject=$mail_from." Emails";

 $message = " 
 <html> 
  <body bgcolor=#DDDDDD> 
   YOUR HTML EMAIL
  </body> 
 </html> 
 "; 


 $From="from: $name <$mail_from>"; 

  $headers  = "From: $From\r\n"; 
  $headers  = 'MIME-Version: 1.0' . "\r\n";
  $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";


  $to = "Shop <info@domain.com>";

  $from = "Contact $name <info@domain.co.uk>";

  $host = "mail.exclusivehosting.net";
  $username = "info@domain.co.uk";
  $password = "(hello)";

  $headers = array ('From' => $from,
  'To' => $to,
  'Subject' => $subject);
  $smtp = Mail::factory('smtp',
  array ('host' => $host,
  'auth' => true,
  'username' => $username,
  'password' => $password));

  $mail = $smtp->send($to, $headers, $message);


  if (PEAR::isError($mail)) {
  echo("<p>" . $mail->getMessage() . "</p>");
  } else {
  //echo("<p>Message successfully sent!</p>");
  echo'<table width="800" border="0" align="center" >
  <td>';
  echo "We've recived your contact information, we will get back soon!";
echo'</td>
</table>';
  }

因为我正在接收电子邮件

  <html> 
  <body bgcolor=#DDDDDD> 
  YOUR HTML EMAIL
  </body> 
  </html> 

3 个答案:

答案 0 :(得分:2)

您可以使用以下方法。

function sendMail($email, $subject, $message)
{
    $supportEmail = 'info@abc.com';
    $from = 'Abc';
    $msg  = $message;
    $from = str_replace(' ', '-', $from);
    $frm  = $from.' <'.$supportEmail.'>';
    preg_match("<(.*)@(.*\..*)>", $frm, $match);

    ///////////////////Headers/////////////////
    $hdr='';
    $hdr.='MIME-Version: 1.0'."\n";
    $hdr.='content-type: text/html; charset=iso-8859-1'."\n";
    $hdr.="From: {$frm}\n";
    $hdr.="Reply-To: {$frm}\n";
    $hdr.="Message-ID: <".time()."@{$match[2]}>\n";
    $hdr.='X-Mailer: PHP v'.phpversion();
    $x=@mail($email, $subject, $msg, $hdr);
    if($x==0)
    {
        $email=str_replace('@','\@', $email);
        $hdr=str_replace('@','\@',$hdr);
        $x=@mail($email, $subject, $msg, $hdr);
    }
    return $x;
}

答案 1 :(得分:0)

您正在重写我脚本底部的标题。

将内容类型标题添加到数组中。

$headers = array (
    'From' => $from,
    'To' => $to,
    'Subject' => $subject,
    'Content-Type' => 'text/html; charset=iso-8859-1',
    'MIME-Version' => '1.0',

);

答案 2 :(得分:0)

你可以使用:

$ message_new = html_entity_decode($ mesage);

你可以将$ message_new变量传递给mail funciton,这样你就可以获得正确的输出。 或者参考以下网址:

http://php.net/manual/en/function.html-entity-decode.php