Change the From: email address in PHP mail()

by on January 3, 2007 · 9 comments· LAST UPDATED January 3, 2007

in

Q. How do I change the email address in PHP mail() function?

A. The php mail() function allows you to send email.

mail() syntax:
bool mail ( string to, string subject, string message [, string additional_headers [, string additional_parameters]] )

Where,
=> to: Receiver, or receivers of the mail.

=> subject: Subject of the email to be sent.

=> message: Message to be sent.

=> additional_headers: String to be inserted at the end of the email header.
This is typically used to add extra headers (From, Cc, and Bcc). Multiple extra headers should be separated with a CRLF (\r\n).

string additional_headers is use to specify From: email address in PHP. Generally you write mail() as follows:

<?
$message = "This is a test";
mail('user@domain.com', 'Subject', $message);
?>

If you would like to add From : email address

Use code as follows:

<?php
$to      = 'user@domain.com';
$subject = 'Subject';
$message = 'This is a test';
$headers = 'From: webmaster@yourdot.com' . "\r\n" .
   'Reply-To: webmaster@yourdot.com' . "\r\n" .
   'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>

References



If you would like to be kept up to date with our posts, you can follow us on Twitter, Facebook, Google+, or even by subscribing to our RSS Feed.


{ 9 comments… read them below or add one }

1 bob January 3, 2009 at 5:37 pm

Is there anyway to solve it in HOTMAIL..

FROM user@hostname on behalf of email address

Reply

2 Jab December 14, 2011 at 7:42 pm

ini_set(‘sendmail_from’, ‘info@yoursite.com’);
if you own the server…or you’ll have to play in the php.ini/call your server/host manager.

Reply

3 Nuwan February 12, 2009 at 11:45 am

This was useful,
Thank you so much,
Keep up good works!

Reply

4 Paolo July 2, 2009 at 1:32 pm

Thanks for sharing. Great script. :)

Reply

5 Dave C November 3, 2009 at 8:09 pm

Great script! Thank you!

Reply

6 Amit October 7, 2011 at 4:46 am

if i want to provide the text box for to and from mail ids to the users then what i will do for sending mail from id to To id. I have tried it but i am only able to send mail to the same domain email id’s not all domain email id’s.

Reply

7 Credevator October 11, 2012 at 4:14 am

Helpful script,
thnx

Reply

8 Joy Antony November 22, 2012 at 2:26 pm

Great one! Thank you.

Reply

9 enrique May 10, 2013 at 8:50 am

works! thanks

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <kbd> <blockquote> <pre> <a href="" title="">

Tagged as:

Previous Faq:

Next Faq: