PHP邮件程序无法发送HTML消息

时间:2017-02-22 11:48:40

标签: php html

我尝试通过PHP发送HTML邮件,但没有任何工作。我尝试了两种选择。

一个file_get_contents

<?php
$to = 'teas@gmail.com';
$subject = 'Marriage Proposal';
$from = 'support@blabla.com';

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Create email headers
$headers .= 'From: '.$from."\r\n".
    'Reply-To: '.$from."\r\n" .
    'X-Mailer: PHP/' . phpversion();

$message = file_get_contents("email_template.html");

// Sending email
if(mail($to, $subject, $message, $headers)){
    echo 'Your mail has been sent successfully.';
} else{
    echo 'Unable to send email. Please try again.';
}
?>

在PHP字符串中使用HTML:

<?php
$to = 'anthony@gmail.com';
$subject = 'Marriage Proposal';
$from = 'peterparker@email.com';

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Create email headers
$headers .= 'From: '.$from."\r\n".
    'Reply-To: '.$from."\r\n" .
    'X-Mailer: PHP/' . phpversion();

// Compose a simple HTML email message
$message = '<html><body>';
$message .= '<h1 style="color:#f40;">Hi Jane!</h1>';
$message .= '<p style="color:#080;font-size:18px;">Will you marry me?</p>';
$message .= '</body></html>';

// Sending email
if(mail($to, $subject, $message, $headers)){
    echo 'Your mail has been sent successfully.';
} else{
    echo 'Unable to send email. Please try again.';
}
?>

两种功能的响应是:

  

无法发送电子邮件。请再试一次。无法发送电子邮件。请   再试一次。

谁能告诉我什么错了?

1 个答案:

答案 0 :(得分:-2)

只需在php邮件程序中启用HTML

$mail->isHTML(true);

如需参考,请参阅here the example in github