以html格式发送邮件正文

时间:2013-12-13 02:04:27

标签: php html email smtp

$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中。我已经尝试了很长一段时间,但我没有到达任何地方。如果有人能帮我这个。那会很棒。

1 个答案:

答案 0 :(得分:0)

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

<?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);

?>