1

I've been working through this thread here: Sending HTML email from PHP

For the last 5 hours and for some reason cannot get my PHP to generate HTML mail.

Here's what I have:

// Construct Mail:
$message = "<html><body>Some text<br />New line<p>New para</p></body></html>";
// Send Mail:
$to         = "$UserMail";
$subject        = "[Skills Exchange Network] Event Created";
$header     = 'MIME-Version: 1.0' . "\r\n";
$header     .= 'Content-Type: text/html; charset=ISO-8859-1' . "\r\n";
$headers    .= 'From: Skills Exchange Network <Mail Address is here>' . "\r\n";
$headers    .= 'Cc: Skills Exchange Network <Mail Address is here>' . "\r\n";
$SendMail   = mail($to,$subject,$message,$headers);

Here's what I receive:

From Skills Exchange Network rnCc: Skills Exchange Network rn

I'm truly at a loss...

Here's what I have now:

// Construct Mail:
$message = "<html><body>Some text<br />New line<p>New para</p></body></html>";

// Send Mail:
$to         = "$UserMail";
$subject        = "[Skills Exchange Network] Event Created";

// 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    .= "From: Skills Exchange Network <[email protected]>" . "\r\n";
$headers    .= "Cc: Skills Exchange Network <[email protected]>" . "\r\n";
$SendMail   = mail($to, $subject, $message, $headers);

And here's what I receive:

Subject: [Skills Exchange Network] Event Created
MIME-Version: 1.0rnContent-Type: text/html; charset=ISO-8859-1rnFrom: Skills Exchange     Network <[email protected]>rnCc: Skills Exchange Network <[email protected]>rn
Message-Id: <[email protected]>
From: [email protected]
Date: Thu, 18 Jul 2013 10:13:08 -0500
X-Antivirus: AVG for E-mail 2013.0.3349 [3204/6500]
X-AVG-ID: ID58DD9571-36429B43

<html><body>Some text<br />New line<p>New para</p></body></html>
5
  • Best is to use tables for email markup Commented Jul 18, 2013 at 13:36
  • Is this the e-mail body you are receiving or is this the subject? Commented Jul 18, 2013 at 13:43
  • There are 2 things at fault: The headers and the body. I get the mail but with malformed headers and the HTML comes as plain text. Probably because the headers are wrong. Commented Jul 18, 2013 at 15:16
  • Use a proper library like SwiftMailer. There's no good reason to do it manually with mail(). Commented Jul 18, 2013 at 15:24
  • I've been doing it this way as it's code running on the server. Are you suggesting SwiftMailer for a specific reason...? Also, as much as I'd like to use SwiftMailer it won't run within my system. Commented Jul 18, 2013 at 15:46

3 Answers 3

1

You are naming $headers as $header and because of that your content-type is not embedding.

$headers     = 'MIME-Version: 1.0' . "\r\n";
$headers    .= 'Content-Type: text/html; charset=ISO-8859-1' . "\r\n";
$headers    .= 'From: Skills Exchange Network <Mail Address is here>' . "\r\n";
$headers    .= 'Cc: Skills Exchange Network <Mail Address is here>' . "\r\n";
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Yogesh I implemented what you suggested: I now still get an error in the headers. MIME-Version: 1.0rnContent-Type: text/html; charset=ISO-8859-1rnFrom: Skills Exchange Network <[email protected]>rnCc: Skills Exchange Network <[email protected]>rn Clearly I still have an issue.
In fact, it's not doing two things: Breaking the headers apart and formatting the HTML. I get the mail OK but not formatted correctly or with the headers loading correctly.
Here's what I have now // Construct Mail: $message = "HTML code here"; // Send Mail: $to = "$UserMail"; $subject = "[Skills Exchange Network] Event Created"; // 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 .= "From: Skills Exchange Network <[email protected]>" . "\r\n"; $headers .= "Cc: Skills Exchange Network <[email protected]>" . "\r\n"; $SendMail = mail($to, $subject, $message, $headers);
0

Maybe if you put the 's' consequently at the end of $header?

1 Comment

That's now corrected byt still get mal formed headers and HTML.
0

PHPMailer could be a useful solution.

1 Comment

I've been using that and have even used code from a previous page I wrote that works perfectly but get these errors.

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.