I have the following mail function:

<?php
{
$to = "alee@####.com";
$subject = "General Contact.";
$headers  = "From: ####<noreply@####.com>\r\n";
$headers .= "Reply-To: info@####.com\r\n";
$headers .= "Return-Path: info@####.com\r\n";
$headers .= 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$name = filter_input(INPUT_POST, 'name');
$telephone = filter_input(INPUT_POST, 'phone');
$nature = filter_input(INPUT_POST, 'nature');   
$comments = filter_input(INPUT_POST, 'comments');
$message = 
"<span style='font-weight: bold;'>Name: </span>"."<br />".$name."<br />"."<br />".
"<span style='font-weight: bold;'>Telephone: </span>"."<br />".$telephone."<br />"."<br   />".
"<span style='font-weight: bold;'>Nature of enquiry: </span>"."<br />".$nature."<br />"."<br />".
"<span style='font-weight: bold;'>Comments: </span>"."<br />".$comments."<br />"."<br />";

mail($to, $subject,"We have received a message via the online form at www.####.com<br />  <br />" . $message, $headers);
echo "Your message was successfully sent. Please <a href='contact.php'>click here</a> to return to the site.";
?>
<?php  }
?>

The problem I have is that when the form is used and the message hits my inbox, the HTML is being displayed as plain text, and not being displayed properly. So I can see all the inline HTML element, i.e. the
will be shown as part of the paragraph and not display a new line. Any help would be awesome!

EDIT:

Reply-To: info@###.com
Return-Path: info@###.com
MIME-Version: 1.0
Content-type: text/html; charset: utf8

We have received a message via the online form at www.###.com<br /><br /><span    style='font-weight: bold;'>Name: </span><br />Aaron Lee<br /><br /><span style='font-weight:   bold;'>Telephone: </span><br />#####<br /><br /><span style='font-weight:   bold;'>Nature of enquiry: </span><br />###<br /><br /><span style='font-weight:   bold;'>Comments: </span><br />Testttttttttttt<br /><br />
有帮助吗?

解决方案

Try to replace the lines :

$headers .= 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

With :

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

其他提示

It looks like it is sending the headers with extra line breaks.

From your example you seem to have two line breaks after the headers. I would try removing the \r so the last line of the header only has \n

Try changing the line

$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

To

$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\n";

I know it is not proper but I have seen this same problem solved this way.

You can try changing the below line:

$headers .= 'MIME-Version: 1.0' . "\n";

to

$headers .= 'MIME-Version: 1.0' . "\r\n";

All the lines should end with \r\n and not anything else.

I'm using this method to send mail. Maybe you can see something it can help:

public static function mailSend($mail_to,$fname,$mail_sub,$mail_msg,$from){

    $To=$mail_to;

    $Body =$mail_msg."<BR>";

    $Subject=$mail_sub;

    $headers  = "MIME-Version: 1.0\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1\n";
    $headers .= "From: Robot <info@#####.com>  \n";
    $headers .= "X-Mailer: www.#####.com\n"; //mailer
    $headers .= "Return-Path: <info@#####.com>\n";
    $headers .= "X-Priority: 3\n"; //1 UrgentMessage, 3 Normal

    $mail = new PHPMailer();

    $mail->IsSMTP();            // set mailer to use SMTP
    $mail->IsHTML(true);

    $mail->From = $from;
    $mail->FromName = $fname;
    $mail->AddAddress($To);

    $mail->WordWrap = 100;      // set word wrap to 100 characters

    $mail->Subject = $Subject;
    $mail->Body    = $Body;
    $mail->AltBody    = $Body;

    $mail->Send();

}

The message

The message you're passing to the mail() function is not valid HTML. This could be a reason your client isn't showing it properly.

Try this:

$message =
    "<!DOCTYPE html>\r\n"
    . "<html lang="en">\r\n"
    . "<body>\r\n"
    . "We have received a message via the online form at my.domain.com<br><br>\r\n"
    . "<strong>Name:</strong><br>{$name}<br><br>\r\n"
    . "<strong>Telephone:</strong><br>{$telephone}<br><br>\r\n"
    . "<strong>Nature of enquiry:</strong><br>{$nature}<br><br>\r\n"
    . "<strong>Comments:</strong><br>{$comments}<br><br>\r\n"
    . "</body>\r\n"
    . "</html>\r\n";

Also make sure attribute-values in tags are wrapped in double-quotes, not single-quotes. So it's <a href="#">link</a>, and not <a href='#'>link</a>.

The headers

Some notes on the headers you're using (though I don't think this is causing your problem):

  • Headers should be separated by \r\n (not \n like your MIME-Version).
  • From should have the syntax From: <email> or From: "name" <email>.
  • Reply-To should have the syntax Reply-To: <email> or Reply-To: "name" <email>.
  • Return-Path should have the syntax Return-Path: <email>.
  • You should add a Content-Length header.

Try this:

$headers = implode(
    "\r\n",
    array(
        'From: "My Name" <noreply@my.domain.com>',
        'Reply-To: <info@my.domain.com>',
        'Return-Path: <info@my.domain.com>',
        'MIME-Version: 1.0',
        'Content-type: text/html; charset=iso-8859-1',
        'Content-Length: ' . strlen($message)
    )
);

Don't use mail()

There are lots of lots of issues with the PHP mail() function. You should really consider using a 3rd party library for sending emails.

I suggest you take a look at Swift Mailer, and don't use the "Mail Transport" (Swift_MailTransport) ;)

I am always using mixed mail, when sending HTML-formatted Mail, cause some people have html disabled. Building mail, multipart/mixed, and add some Lines to devide the parts($trenner)

The main difference from my working solution to your solution is the property Content-Transfer-Encoding, which is 7bit for text part and quoted-printable for html

$crlf = "\r\n";
$trenner = '---wbs'.md5(uniqid(time()));

if(!$this->from)throw new exception('wbs_mail:Kein Absender angegeben');
if(!$this->to)throw new exception('wbs_mail:Kein Empfänger angegeben');
if(!$this->subject)throw new exception('wbs_mail:Kein Betreff angegeben');
if(!$this->content['html'])throw new exception('wbs_mail:Kein HTML-Inhalt angegeben');
if(!$this->content['text'])throw new exception('wbs_mail:Kein Text-Inhalt angegeben');

$this->header = "From: ".$this->from.$crlf;
$this->header  .= "X-Mailer: wbs_mail php_ver". phpversion().$crlf;
$this->header  .= "MIME-Version: 1.0".$crlf;
$this->header .= 'Content-Type: multipart/mixed; boundary="'.$trenner.'"'.$crlf;

// Der Textblock
$content = $trenner.$crlf;
$content .= 'Content-Type: text/plain; charset="iso-8859-1"'.$crlf;
$content .= 'Content-Transfer-Encoding: 7bit'.$crlf;
$content .= $this->getContent('text').$crlf.$crlf;

// Der HTML Block
$content .= $trenner.$crlf.$crlf;
$content_html = 'Content-Type: text/html; charset="iso-8859-1"'.$crlf;
$content_html .= 'Content-Transfer-Encoding: quoted-printable'.$crlf;
$content_html .= $this->getContent('html').$crlf;
$content .= $content_html.$crlf;
$content .= $trenner.$crlf;

You can also use the PHP_EOL (PHP end of line) constant instead of the \n or \r\n.

PHP_EOL (string)
The correct 'End Of Line' symbol for this platform. Available since PHP 4.3.10 and PHP 5.0.2

Source: http://www.php.net/manual/en/reserved.constants.php
See this also: When do I use the PHP constant "PHP_EOL"?


In practice, you can use it as below:

$br = PHP_EOL;

$headers = 'From: Avatar <avatarparto@gmail.com>.'.$br;
$headers .= 'Reply-To: Avatar <avatarparto@gmail.com>.'.$br;
$headers .= 'Subject: Hello'.$br;
$headers .= 'MIME-Version: 1.0'.$br;
$headers .= 'X-Mailer: PHP'.phpversion();

Try using the following function, it sends the mail with multipart data,

function sendEmail($to,$from,$sub,$msg){

    $subject = $sub; 
    $headers = "From: $from";

    // boundary 
    $semi_rand = md5(time()); 
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

    // headers for attachment 
    $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 

    // multipart boundary 
    $message = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"iso-8859-1\"\n" .
    "Content-Transfer-Encoding: 7bit\n\n" .  $msg . "\n\n"; 
    $message .= "--{$mime_boundary}--";
    $returnpath = "-f" . $from;

    $ok = @mail($to, $subject, $message, $headers, $returnpath); 
    return $ok;
  }

Try changing the line

$headers  = "From: ####<noreply@####.com>\r\n";

To

$headers = 'From: <urmail@mail.com>' . "\r\n";

I have changes the double quote to single quote.It works fine for me. try your luck.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top