Skip to content Skip to sidebar Skip to footer

Php Mail() Function With Headers

I always struggle when using headers along with the PHP mail() function. The problem is always the same, last year, this time, as long as I remember, it drives me mad. The problem

Solution 1:

It would be better to use established mailing solutions instead of PHPs mail() function if you're not experienced with that.

A few hints:

Readability and preventing errors

For readability and programming purposes - think about implementing the headers as array. Instead of adding \r\n in every line you could work like that. When building together the mail in the mail() function you can implode() it.

// Building headers.$headers = array();
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=iso-8859';
$headers[] = 'X-Mailer: PHP/'. phpversion();
// ...// Sending mail.
mail($ontvanger, $onderwerp, $bericht, implode(PHP_EOL, $headers));

Sending HTML mails

I recommend to send the mails as multipart MIME messages when sending html. This differs from your approch. Because it's not that easy to explain in a message. Maybe try it with that link: http://webcheatsheet.com/php/send_email_text_html_attachment.php

I've used multipart mime messages for example to send HTML mails with custom attachments via PHP.

There is no necessarity of paying attention to the order of the different headers. But it's some sort of a good practice to group them and start with the most important ones.

Post a Comment for "Php Mail() Function With Headers"