开发者

Php mail: how to send html?

开发者 https://www.devze.com 2023-02-08 07:13 出处:网络
The code below is sending an email correctly but for the body. I need to show html in the body of the message and I canno开发者_运维技巧t make it. The examples in the web won\'t send the email :(

The code below is sending an email correctly but for the body. I need to show html in the body of the message and I canno开发者_运维技巧t make it. The examples in the web won't send the email :(

How can I fix my code to send the email with the html in the body?

Thanks a ton!

<?php

$to = 'mymail@mail.com';

$subject = 'I need to show html'; 

$from ='example@example.com'; 

$body = '<p style=color:red;>This text should be red</p>';

ini_set("sendmail_from", $from);

$headers = "From: " . $from . "\r\nReply-To: " . $from . "";
  $headers .= "Content-type: text/html\r\n"; 
if (mail($to, $subject, $body, $headers)) {

  echo("<p>Sent</p>");
 } else {
  echo("<p>Error...</p>");
 }

?>


use this header for the mail:

 $header  = "MIME-Version: 1.0\r\n";
 $header .= "Content-type: text/html; charset: utf8\r\n";

and for the content/body:

<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
... ... ...

it's important to use inline css commands and recommanded to use tables for the interface.

...

In your Mail-Body you than have to put HTML code with head and body


Have you looked at the headers of the incoming mail? It says

Reply-To: example@example.comContent-type: text/html

Simply add another \r\n here:

Reply-To: " . $from . "\r\n";


I recommend rather than messing around with doing this yourself you use one of the many free classes available all over the web to do it.

I would recommend: PHPMailer


I found this works well!

Source

<?php
//define the receiver of the email
$to = 'youraddress@example.com';
//define the subject of the email
$subject = 'Test HTML email'; 
//create a boundary string. It must be unique 
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time())); 
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\""; 
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-alt-<?php echo $random_hash; ?>  
Content-Type: text/plain; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit

Hello World!!! 
This is simple text email message. 

--PHP-alt-<?php echo $random_hash; ?>  
Content-Type: text/html; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit

<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p> 

--PHP-alt-<?php echo $random_hash; ?>--
<?
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed";
?>


Simple answer: Don't do it. HTML emails are evil and annoying. At least if there's no PROPER plaintext version included. Proper = same information like in the HTML version, not just a stupid comment about getting another email client or a link to the html version if it's available on the web.

If you really need it: http://pear.php.net/package/Mail_Mime

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号