2017-02-22 21 views
0

我試圖通過PHP發送HTML郵件,但沒有任何工作。我嘗試了兩種選擇。PHP郵件程序無法發送HTML郵件

一個與file_get_contents

<?php 
$to = '[email protected]'; 
$subject = 'Marriage Proposal'; 
$from = '[email protected]'; 

// 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.'; 
} 
?> 

而且一個與HTML在PHP字符串:

<?php 
$to = '[email protected]'; 
$subject = 'Marriage Proposal'; 
$from = '[email protected]'; 

// 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.'; 
} 
?> 

響應兩種功能是:

無法發送電子郵件。請再試一次。無法發送電子郵件。請 再試一次。

任何人都可以告訴我什麼是錯的?

+0

你在'php.ini'正確設置您的SMTP服務器? – TFennis

+0

一切工作正常。我一直以純文本的方式發送了總共10k封電子郵件。但是當我實現HTML時,它會出錯。 如果這些普通電子郵件正在交付,那麼它意味着一切都很好.idk仍然 – DrNawaf

+0

爲了澄清標題,這是使用php中的mail()函數,而不是「PHPMailer」。巨大差距。 – mickmackusa

回答

-1

只啓用在PHP郵件的HTML

$mail->isHTML(true); 

做參考,請參閱here the example in github

+0

好吧等待讓我試試這個 – DrNawaf

+0

好吧試試這種方式 –

+0

他沒有使用PHPMailer庫。 – TFennis