Solved

PHP Mail() Headers - Problems sending valid data.

Posted on 2006-12-01
22
232 Views
Last Modified: 2006-12-01
Hi guys,

I have recently been looking at the mail() function for a new project on a new dedicated server we have. I have copied "Example 4. Sending HTML email" from the PHP manual (http://uk.php.net/manual/en/function.mail.php) exactly and tried sending the mail but all the headers get put into the email body and dont seem to want to stay in the header section of the email.

Is there a setting in Apache/PHP.ini that might be wrong as this is a server from an ISP? Maybe I haven't set something up properly?

Any ideas?

Thanks,
MOZ
0
Comment
Question by:nikez2k4
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 8
  • 4
  • +1
22 Comments
 
LVL 13

Expert Comment

by:rhickmott
ID: 18052495
What is the code your using to send?

Normally headers int he body are caused by erroneous /r/n's
0
 
LVL 1

Author Comment

by:nikez2k4
ID: 18052501
Sorry... just FYI:

The code I am using:
************************************************************************************************
            // multiple recipients
            $to  = 'marshall@landau-forte.org.uk';
            
            // 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 .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
            
            // Mail it
            mail($to, $subject, $message, $headers);
************************************************************************************************

The email output:
************************************************************************************************
Content-type: text/html; charset=iso-8859-1

From: Birthday Reminder <birthday@example.com>



            <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>
************************************************************************************************
0
 
LVL 1

Author Comment

by:nikez2k4
ID: 18052505
It is literally copied from PHP's website with a couple of headers (like CC & BCC) removed as I don't need them.

MOZ
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Expert Comment

by:rhickmott
ID: 18052521
The content type normally has to be the last line in the header

Try

==========================================
$headers = "From: \"You\" <mail@you.com>\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html";
=====================================
0
 
LVL 13

Expert Comment

by:rhickmott
ID: 18052524
Failing that

Try

=======================================
$headers = "From: \"You\" <mail@you.com>\r\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: text/html";
=======================================
0
 
LVL 1

Author Comment

by:nikez2k4
ID: 18052541
Tried both of those and neither of them send out an email :|

MOZ
0
 
LVL 48

Expert Comment

by:hernst42
ID: 18052546
for sending HTML-mails I recommend not using the php-mail-function, as there are mayor problems if you do one little mistake in your code. Better use http://phpmailer.sf.net. Makes sending mails a lot easyer and you don't need bugfixing for that.
0
 
LVL 13

Expert Comment

by:rhickmott
ID: 18052558
I'm assuming you added the mail() function after it?

ie

// To send HTML mail, the Content-type header must be set
$headers = "From: \"Birthday Reminder\" <birthday@example.com>\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html";
 
// Mail it
mail($to, $subject, $message, $headers);



0
 
LVL 1

Author Comment

by:nikez2k4
ID: 18052560
Which is why I have come here to get some help on how to get the syntax right :)

I have looked at PHPMailer before but I want to do it on my own as it's interesting to know how it all works...

MOZ
0
 
LVL 1

Author Comment

by:nikez2k4
ID: 18052578
rhickmott - yes i did ;)

I have now got the "From" bit working but it's still not recognising the HTML header. I'm still getting "Content-Type: text/html" in the email body rather than as a header... looks like the - "MIME-Version: 1.0\r\n"; - bit is being sent though.

MOZ
0
 
LVL 13

Expert Comment

by:rhickmott
ID: 18052584
PHPMailer is a Class so you basically use it as such

require("class.phpmailer.php");

$mail = new PHPMailer();

$mail->From              = "birthday@example.com";
$mail->FromName     = "Birthday Reminder";
$mail->Host               = "your.smtp.server";
$mail->Mailer             = "smtp";

$mail->body                =    $message;
$mail->AddAddress      =   $to;

$mail->Send();

0
 
LVL 13

Accepted Solution

by:
rhickmott earned 425 total points
ID: 18052602
Which code did you use? you sometimes have to play and tweak the \n\r values on the last two lines depending on the server.

Normally viewing the e-mail source is a good way of debugging this especially if it shows you the headers.

It should look like

header
header
header
header

body

0
 
LVL 1

Author Comment

by:nikez2k4
ID: 18052604
Yeah I know :) But like I said, I want to do it without "3rd party" classes.

I appreciate the suggestion though, but I really want to give it a go without any aids like that. I know it makes it a lot harder to get right but it is something I need to learn.

Thx,
MOZ
0
 
LVL 13

Expert Comment

by:rhickmott
ID: 18052614
No problem PhPMailer is a useful class even if you don't use it its worth looking into as it handles things such as Authenticated SMTP and such. If you have ever tried to make a e-mail with attachments using mail() you will realise what a PITA it is :(

mail works with most things but everyone has problems with the header.

0
 
LVL 48

Expert Comment

by:hernst42
ID: 18052630
Tried copy&paste the example from php and it works flawless. Either your Mail-client is not able to display the HTML-mail correctly or your sendmail-program screws up the mail. On which system did you try to do this.
0
 
LVL 11

Expert Comment

by:ch2
ID: 18052659
Is not PHP. You mail client dosen't accept MIME.

Which client are you using?
0
 
LVL 1

Author Comment

by:nikez2k4
ID: 18052661
AHHHH!

Managed to fix it....thats for your suggestion to play about with the line feeds. The carriage return was messing it up. Is that a server thing? Can you explain what causes this problem?

Sorry it will annoy me no end if I don't understand what is causing the problem!! :)

Thanks,
MOZ

P.S.

The code I am using that works for me:
********************************************************************************
            // multiple recipients
            $to  = 'marshall@landau-forte.org.uk';
            
            // 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 = "From: \"Birthday Reminder\" <birthday@example.com>\n";
            $headers .= "MIME-Version: 1.0\n";
            $headers .= "Content-Type: text/html\n";
             
            // Mail it
            mail($to, $subject, $message, $headers);
********************************************************************************
0
 
LVL 48

Expert Comment

by:hernst42
ID: 18052894
What php on which platform are you using?
0
 
LVL 13

Expert Comment

by:rhickmott
ID: 18052905
Its normally caused by the outgoing mail server im not 100% sure what causes it as it varies the above code normally works fo rme in some instances but I sometime have to tweak the headers slughtly with regards to \r\n to get it to function correctly on different mail servers.

Most annoying.

Thats the bonus of phpmailer() because it acts as a outgoing server you know its consistent.
0
 
LVL 48

Assisted Solution

by:hernst42
hernst42 earned 75 total points
ID: 18052935
phpmailer uses "\n" as line-delimiter, because most mailserver accept "\n" also, but some don not accept "\r\n". It's ok to write your own code for that, but why always invent and reimplement the wheel again.
Keep your focus on putting good softwareparts together to build a application in lees time, with less errors and lower costs for your customers. This will make all people happy. So for php phpmailer is one of the best tools to send mails. Your own solution will take at least a month to be as good as phpmailer. There is no reason not to use good parts form others (if the licence permits this).
0
 
LVL 13

Expert Comment

by:rhickmott
ID: 18052951
I think what he means is that if you only sending a basic e-mail and your application is not mail-intensive then there no point in importing a class which can do x y z if you only want to do x and x is a few lines of code.

I will agree with you though in general if your doing a fair amount of e-mail work then using phpmailer is a good idea.
0
 
LVL 1

Author Comment

by:nikez2k4
ID: 18053048
Thanks for all your advice guys, really appreciate it.

I do agree with you, using ready made applications are always a good idea, however licencing issues are a pain in the neck sometimes and I really like working out how to do things my way...It is usually only for my own projects that I mess about so I have all the time I need to perfect what I am working on.

In this case I am doing some work for our Careers department and it's only going to be a temporary solution this year to see how much the online system gets used. We are planning to completely rethink how we store data in databases etc etc for next year and as I already have the groundwork on this system it will make it easier to work on the big project next year.

Again, thanks for all your advice and no doubt I will be back on EE when I have my next problem!! :)

Take care,
MOZ
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question