Change the From: email address in PHP mail()

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

ADVERTISEMENTS

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

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
15 comments… add one
  • bob Jan 3, 2009 @ 17:37

    Is there anyway to solve it in HOTMAIL..

    FROM user@hostname on behalf of email address

    • Jab Dec 14, 2011 @ 19:42

      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.

  • Nuwan Feb 12, 2009 @ 11:45

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

  • Paolo Jul 2, 2009 @ 13:32

    Thanks for sharing. Great script. :)

  • Dave C Nov 3, 2009 @ 20:09

    Great script! Thank you!

  • Adrenaline Films Jun 9, 2011 @ 15:10

    I’m having a heck of a time with bluehost. No matter how I alter the php and/or php.ini, my ‘from’ can’t be changed. Any ideas?

  • Amit Oct 7, 2011 @ 4:46

    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.

  • mr.lecoqsportif Sep 25, 2012 @ 11:15

    I have been looking for a simple example and wasted hours and hours.
    my problem is sending email as follow:
    From www-data@servername. and all the mails go to junk .

    However, this one sort it nicely as follow:
    From abcd@alpherbet.com and does not goes to junk.

    recommended.

  • Credevator Oct 11, 2012 @ 4:14

    Helpful script,
    thnx

  • Joy Antony Nov 22, 2012 @ 14:26

    Great one! Thank you.

  • enrique May 10, 2013 @ 8:50

    works! thanks

  • Imo Douglas Sep 21, 2013 @ 11:48

    Thanks alot. Works perfectly.

  • Sim Simma Apr 8, 2014 @ 14:13

    Good help ;) Much appreciated.

  • Ankit Apr 4, 2015 @ 12:09

    I am new to web development. i want to send mails to different email id’s. Like i am working on a matrimonial site there user can send mails to their matches. But in my case emails are going to a particular email address for a particular user.

  • tom quilter Jan 10, 2016 @ 15:30

    legend – thanks

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.