Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Sending html with mail() - certain servers receive as text

Tags:

php

email

I've searched a lot but could not find any solutions for this. after changing my code for hours i tried the example from php.net and it acts the same.

if i send it to my gmail address everything look fine, if i send it to another server it does not render it as html. i tried about 8 servers and 4 suffer from this issue

this is the code (php example):

// subject
$subject = 'Birthday Reminders for August';

// message
$message = '
<html>
<head>
  <title>Birthday Reminders for August</title>
</head>
<body>
  <p>Here are the birthdays upcoming in August!</p>
  <table>
    <tr>
      <th>Person</th><th>Day</th><th>Month</th><th>Year</th>
    </tr>
    <tr>
      <td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
    </tr>
    <tr>
      <td>Sally</td><td>17th</td><td>August</td><td>1973</td>
    </tr>
  </table>
</body>
</html>
';

// 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";

// Additional headers
$headers .= 'To: Mary <[email protected]>, Kelly <[email protected]>' . "\r\n";
$headers .= 'From: Birthday Reminder <[email protected]>' . "\r\n";
$headers .= 'Cc: [email protected]' . "\r\n";
$headers .= 'Bcc: [email protected]' . "\r\n";

// Mail it
mail($to, $subject, $message, $headers);
?>

this is the "show original" from gmail (gmail is only the client, mail comes from php server), message is not displayed correctly - html is raw:

Delivered-To: XXX
Received: by 10.220.35.74 with SMTP id o10csp285028vcd;
        Sun, 9 Dec 2012 03:18:35 -0800 (PST)
Received: by 10.220.238.139 with SMTP id ks11mr6865773vcb.49.1355051914996;
        Sun, 09 Dec 2012 03:18:34 -0800 (PST)
Received-SPF: softfail (google.com: best guess record for domain of transitioning             unknown does not designate 82.80.232.249 as permitted sender) client-ip=82.80.232.249;
Received: by 10.230.67.134 with POP3 id r6mf4415353vbi.4;
            Sun, 09 Dec 2012 03:18:34 -0800 (PST)
X-Gmail-Fetch-Info: [email protected] 5 mail.example.com 110 [email protected]
Received: from [82.80.232.249] by mail.example.com (ArGoSoft Mail Server .NET v.1.0.8.4)         with ESMTP (EHLO web9.wishosting.net)
    for <[email protected]>; Sun, 09 Dec 2012 13:18:02 +0200
Received: by web9.wishosting.net (Postfix, from userid 1168)
    id C82D03EE2EC; Sun,  9 Dec 2012 13:18:23 +0200 (IST)
        To: [email protected], [email protected]
Subject: Birthday Reminders for August
    X-PHP-Script: example.com/lp/mailtest.php for 89.139.28.204
MIME-Version: 1.0
Date: Sun, 09 Dec 2012 13:18:02 +0200
Message-ID: <x0otq4xc1dcboohp09122012011802@EAWEB1>
SPF-Received: none
X-FromIP: 82.80.232.249
From: [email protected]

Content-type: text/html; charset=iso-8859-1

To: Mary <[email protected]>, Kelly <[email protected]>

From: Birthday Reminder <[email protected]>

Cc: [email protected]

Message-Id: <[email protected]>
Date: Sun,  9 Dec 2012 13:18:23 +0200 (IST)



    <html>
    <head>
      <title>Birthday Reminders for August</title>
</head>
<body>
  <p>Here are the birthdays upcoming in August!</p>
  <table>
    <tr>
      <th>Person</th><th>Day</th><th>Month</th><th>Year</th>
    </tr>
    <tr>
              <td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
            </tr>
    <tr>
          <td>Sally</td><td>17th</td><td>August</td><td>1973</td>
    </tr>
      </table>
</body>
</html>
like image 533
gidim Avatar asked Dec 09 '12 11:12

gidim


People also ask

Why my mail function is not working in PHP?

Make sure the localhost mail server is configuredWithout one, PHP cannot send mail by default. You can overcome this by installing a basic mail server. For Windows you can use the free Mercury Mail. You can also use SMTP to send your emails.

How can I add HTML email in PHP?

Use the following script. <? php $to = '[email protected]'; $subject = "Send HTML Email Using PHP"; $htmlContent = ' <html> <body> <h1>Send HTML Email Using PHP</h1> <p>This is a HTMl email using PHP by CodexWorld</p> </body> </html>'; // Set content-type header for sending HTML email $headers = "MIME-Version: 1.0" .

When we want to incorporate HTML tags in the email that we are sending through PHP Which of the following is to be set for MIME content?

= 'Content-type: text/html; charset=iso-8859-1' .

How do you send and receive emails using PHP?

PHP makes use of mail() function to send an email. This function requires three mandatory arguments that specify the recipient's email address, the subject of the the message and the actual message additionally there are other two optional parameters. mail( to, subject, message, headers, parameters );


2 Answers

Ivan's comment is correct!

Try below example which uses phpmailer lib.

<?php
//phpmailer lib
require_once 'class.phpmailer.php';

$mail = new PHPMailer(true); 

try {
    $mail->AddAddress('[email protected]', 'Mary');
    $mail->AddAddress('[email protected]', 'Kelly');
    $mail->SetFrom('[email protected]', 'Birthday Reminder');
    $mail->AddReplyTo('[email protected]', 'No-reply');
    $mail->Subject = 'Birthday Reminders for August';
    $mail->AltBody = 'Here are the birthdays upcoming in August!\nPerson:Joe BirthDay:3rd Aug 1970\nPerson:Sally BirthDay:17th Aug 1973\n'; 
    $mail->MsgHTML('
        <p>Here are the birthdays upcoming in August!</p>
        <table>
            <tr>
              <th>Person</th><th>Day</th><th>Month</th><th>Year</th>
            </tr>
            <tr>
              <td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
            </tr>
            <tr>
              <td>Sally</td><td>17th</td><td>August</td><td>1973</td>
            </tr>
        </table>'
    );

    $mail->Send();

    echo "Message Sent OK<p></p>\n";
} catch (phpmailerException $e) {
    echo $e->errorMessage(); 
} catch (Exception $e) {
    echo $e->getMessage(); 
}
?>

PHPMailer can be downloaded from https://code.google.com/a/apache-extras.org/p/phpmailer/downloads/list

Samples located in https://code.google.com/a/apache-extras.org/p/phpmailer/wiki/ExamplesPage

like image 50
Kasun Avatar answered Nov 03 '22 03:11

Kasun


This depends of email reader that will display the message.

Gmail and most modern email clients (like MS Outlook, Thunderbird, Evolution...) will render html if it has html content, some older readers will render it as a plain text since they are not capable of rendering html (like squirrel or horde).

In order for email to be properly rendered in any reader you have to put in email body both plain text and html version of email.

Best way to do it is to use PHPmailer, SwiftMail and other wrappers for email in php, but if you really wish to do it yourself try this.

Also, note that some readers will have problems in displaying various html elements like divs, and css rules. It is best to stick with tags and css what MS Outlook can render.

like image 41
Ivan Hušnjak Avatar answered Nov 03 '22 03:11

Ivan Hušnjak