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 <[email protected]>' . "\r\n";
    $headers .= 'From: Pynix <[email protected]>' . "\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

4
  • Take a look here and other answers related to HTML email stackoverflow.com/questions/3058897/sending-html-email-from-php Commented Jul 27, 2011 at 8:38
  • is sendmail or something installed? Commented Jul 27, 2011 at 8: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. Commented Jul 27, 2011 at 8:46
  • I took it from the PHP.net site and numerous tutorials, but it still sends in plain-text. Commented Jul 27, 2011 at 8:53

3 Answers 3

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;
Sign up to request clarification or add additional context in comments.

Comments

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 => "[email protected]"))
    ->setTo(array($email => "$fname $lname"))
    ->setBody($body);

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

You can send both plaintext and html email with ease.

1 Comment

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?
0

Fixed.

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

// Set The Headers:
$headers        =   'From: "Me" <[email protected]>'.PHP_EOL.
'Reply-to: <[email protected]>'.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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.