简体   繁体   中英

Why is the HTML not being interpreted in this e-mail?

I'm running a website loccaly useing WAMP and have installed Test Mail Server Tool to act as a mail server (all it does is saves the messages as .eml files). I've tried opening the messages with Lotus Notes and gmail (web interface) and both do not interpret the HTML, for example instead of having a clickable link they have <a href='localhost'>click here</a> Did I make a mistake with the headers?

Here is the code I am using

$to = 'bepusslai@fakeinbox.com';
$from = 'From: tester1@localhost.com';
$subject = 'this is a test';
$message = '<html><head></head><body>Hello. Please follow <a href="http://localhost/proc.php?uid=45ab3">this link</a> to activate your account.'
    ."r\n".'<a href="http://localhost/proc.php?uid=45ab3"><img src="images/ActivateButton.gif" alt="activate button" />
    </body></html>';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; utf-8' . "\r\n";
$headers .= 'From: testk1@localhost.com' . "\r\n";
mail($to, $subject, $message, $from, $headers);

By the way, I was stuck not having a mail server since WAMP doesn't come with one and I red on another question someone recommended Test Mail Server Tool. I'm open to using a different one because it doesn't seem popular.

If you refer to the docs and examples in the docs, you will see that the From information is not a separate argument to mail() but is included with the additional-headers information.

bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )

Remove your $from argument from the call to mail() .

mail($to, $subject, $message, $headers);

Look at this example , it works for me :

<? 
//change this to your email. 
$to = "m@maaking.com"; 
$from = "m2@maaking.com"; 
$subject = "Hello! This is HTML email"; 

//begin of HTML message 
$message = "<html> 
   <body bgcolor=\"#DCEEFC\"> 
<center> 
    <b>Looool!!! I am reciving HTML email......</b> <br> 
    <font color=\"red\">Thanks dud!</font> <br> 
    <a href=\"http://www.test.com/\">* test.com</a> 
</center> 
  <br><br>*** Now you Can send HTML Email <br> Regards<br>MOhammed Ahmed - Palestine 
  </body> 
   </html>"; 
//end of message 

// To send the HTML mail we need to set the Content-type header. 
$headers = "MIME-Version: 1.0rn"; 
$headers .= "Content-type: text/html; charset=iso-8859-1rn"; 
$headers  .= "From: $from\r\n"; 
//options to send to cc+bcc 
//$headers .= "Cc: [email]maa@p-i-s.cXom[/email]"; 
//$headers .= "Bcc: [email]email@example.cXom[/email]"; 

// now lets send the email. 
mail($to, $subject, $message, $headers); 

echo "Message has been sent....!"; 
 ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM