PHP : Send email via native function

Sending email from php via native functions is easy.


mail("", "Test Email", "Error in email");


To send email from a specific recipient, I noticed that headers can be added to the mail function (which are optional). Here’s what I had added to send HTML emails from specific headers:

// 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";
$headers .= 'From: ' . "\r\n";
mail("", "Test Email", "Error in email", $headers);

But found that on occasions, the email was being sent as the apache user and the hostname, e.g. On more investigation, I found that setting up the from recipient in the additional parameter in mail function is better than passing it on the header. So here’s what I finally did which worked successfully. You can look at /var/log/mail.log to see the from address:

mail("", "Test Email", "Error in email", $headers, '');
Posted in PHP

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s