以 html 格式发送邮件正文


Send body of mail in html

$site = "http://127.0.0.1/website";                                             
$webmaster = "<email@hotmail.com>";
$to = "$email";                                             
$subject = "Your new password";
$message = "<html><body><p>Hello. Your password has been reset. Your new password is bellow</p><br/><p>Password : $pass</p><br/><a href='$site/activatePass.php?user=$user&password=$pass&code=$dbcode'>Acrivate</a><br/></body></html>";
$host = "smtp.live.com";                                                
$port = "587";                                              
$eusername = "email@hotmail.com";                                               
$epassword = "password!";                                               
$headers = array ('From' => $webmaster,'To' => $to,'Subject' => $subject, 'MIME-Version: 1.0', 'Content-type: text/html; charset=iso-8859-1');                                              
$smtp =@ Mail::factory('smtp',                                                
array ('host' => $host,'port' => $port,'auth' => true,'username' => $eusername,'password' => $epassword));                                              
$mail = @$smtp->send($to, $headers, $message);                                                                                                  
if (@PEAR::isError($mail)) {
   echo("<p>" . $mail->getMessage() . "</p>");
  } else {
 echo "Your password has been reset. An email has been sent with your new password to $email";
 }

上面的代码应该向一个人发送电子邮件,但以 html 格式。我已经尝试了一段时间,但我一无所获。如果有人可以帮助我。那真是太棒了。

您还需要使用 Mail/mime 并设置邮件的 HTML 版本。Pear在他们的网站上有一个例子。

<?php
include 'Mail.php';
include 'Mail/mime.php' ;
$text = 'Text version of email';
$html = '<html><body>HTML version of email</body></html>';
$file = '/home/richard/example.php';
$crlf = "'n";
$hdrs = array(
              'From'    => 'you@yourdomain.com',
              'Subject' => 'Test mime message'
              );
$mime = new Mail_mime(array('eol' => $crlf));
$mime->setTXTBody($text);
$mime->setHTMLBody($html);
$mime->addAttachment($file, 'text/plain');
$body = $mime->get();
$hdrs = $mime->headers($hdrs);
$mail =& Mail::factory('mail');
$mail->send('postmaster@localhost', $hdrs, $body);
?>