2

我正在使用以下代码通过 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> 
4

3 回答 3

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;
}
于 2012-11-12T11:23:06.567 回答
0

根据我所见,您正在重写脚本底部的标题。

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

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

);

于 2012-11-12T11:20:27.013 回答
0

您可以使用:

$message_new=html_entity_decode($mesage);

并且您可以将 $message_new 变量传递给邮件功能,这样您就可以获得正确的输出。或者参考这个网址:

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

于 2012-11-12T11:22:42.173 回答