1

I am trying to send an HTML mail through PHP, but I can't seem to get it working.

This is my code:

    // multiple recipients
    $to  = 'mail';

    // subject
    $subject = 'Subject';

    // message
    $message = "
    <html>
    <head>
      <title>Thanks</title>
    </head>
    <body>
        <div>
            <b>Thanks for your email</b>
        </div>
    </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: info <info@example.com>' . "\r\n";
    $headers .= 'From: Pynix <info@example.com>' . "\r\n";

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

This also doesn't show any sender when I receive it in Outlook.

Anyone an idea?

Thanks

Roel
  • 1,462
  • 4
  • 19
  • 31
  • Take a look here and other answers related to HTML email http://stackoverflow.com/questions/3058897/sending-html-email-from-php – Sabeen Malik Jul 27 '11 at 08:38
  • is sendmail or something installed? – Sparky Jul 27 '11 at 08:39
  • @Sparky: sendmail is definitely installed. the OP says "..when I receive it in Outlook". So, there must be something wrong... This code seems to be directly taken from the PHP.net website though. – Jimmie Lin Jul 27 '11 at 08:46
  • I took it from the PHP.net site and numerous tutorials, but it still sends in plain-text. – Roel Jul 27 '11 at 08:53

3 Answers3

1

I don't think you have to put the To: line in the header as it is a parameter of the mail function. However some mail clients don't like light headers, here's mine which is working:

$header = 'From: "Contact" <mail>'.PHP_EOL.
                  'Reply-to: <mail>'.PHP_EOL.
                  'MIME-Version: 1.0'.PHP_EOL.
                  'Content-Type: text/plain; charset=utf-8'.PHP_EOL.
                  'Content-Transfer-Encoding: 8bit'.PHP_EOL.
                  'X-Mailer: PHP/'.PHP_VERSION.PHP_EOL;
MatTheCat
  • 18,071
  • 6
  • 54
  • 69
0

Fixed.

I found this which for some reason worked perfectly on my server:

// Set The Headers:
$headers        =   'From: "Me" <info@skillsexchangenetwork.net>'.PHP_EOL.
'Reply-to: <Me@Him.com>'.PHP_EOL.
'Cc: <Her @There.com>'.PHP_EOL.
'MIME-Version: 1.0'.PHP_EOL.
'Content-type: text/html; charset=iso-8859-1'.PHP_EOL.
'Content-Transfer-Encoding: 8bit'.PHP_EOL.
'X-Mailer: PHP/'.PHP_VERSION.PHP_EOL;
// Send:
mail($to, $subject, $message, $headers);

Thanks for the input guys.

Harlequin
  • 25
  • 5
0

I use SwiftMailer:

require_once('../lib/swiftMailer/lib/swift_required.php');
...
function sendEmail(){
  //Sendmail
  $transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs');

  //Create the Mailer using your created Transport
  $mailer = Swift_Mailer::newInstance($transport);

  $body="Dear $fname,\n\nYour job application was successful. \n\nYours,\n\nEamorr\n\n\n\n\n\n\n";


  //Create a message
  $message = Swift_Message::newInstance('Subject goes here')
    ->setFrom(array($email => "no-reply@yourdomain.com"))
    ->setTo(array($email => "$fname $lname"))
    ->setBody($body);

  //Send the message
  $result = $mailer->send($message);
}

You can send both plaintext and html email with ease.

Eamorr
  • 9,872
  • 34
  • 125
  • 209
  • Hey! I know it's a little bit off-topic and 3 years late but, while using SwiftMailer, can you combine it with CKEditor? What I mean is, can you design your mail in CKEditor, and then send the HTML outputted by CKEditor via SwiftMailer and it will show properly? – kfirba Feb 21 '14 at 09:05