PHP Send Email

Q. How do I send an email using PHP and Apache webserver under Linux / UNIX operating systems? How do I send email from a PHP Script?

A. The mail() function under PHP allows you to send mail. For the Mail functions to be available, PHP must have access to the sendmail binary on your system during compile time. Usually most webservers are installed with sendmail binary.

Sample PHP Send Email Code

// Send to?
$to = "";
// The Subject
$subject = "Email Test";
// The message
$message = "This is a test.\n
How much is Linux worth today?\n
End of email message!";
// In case any of our lines are larger than 70 characters, we should use wordwrap()
$message = wordwrap($message, 70);
// Send email
// Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise. 
// Use if command to display email message status
if ( mail($to, $subject, $message) )
     echo("Your email message successfully sent.");
     echo("Sorry, message delivery failed. Contact webmaster for more info.");

Adding Email Headers such as From Email ID

You can add 4th parameters to mail(). This is typically used to add extra headers (From, Cc, and Bcc). Multiple extra headers should be separated with a CRLF (\r\n):

$to = '';
$subject = 'Test';
$message = 'This is a test.';
// set headers as per your requirements. 
$headers = 'From:' . "\r\n" .
    'Reply-To:' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();
if ( mail($to, $subject, $message, $headers) ) {
	echo 'Message sent!';
	echo 'Message failed, contact webmaster for more info!';

See also:

  1. PHP: Verify And Sanitize Email Address
  2. PHP Send Email Using Authenticated SMTP Mail Server In Real Time
  3. Install PHP Pear Mail / SMTP package on CentOS / Red Hat Enterprise Linux

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 11 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig 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 glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
11 comments… add one
  • willc Oct 20, 2008 @ 20:05

    Please note that as it is, this script should not be called from a public-facing web site HTML form, as it doesn’t do any validation/sanitizing of data which users may input into the form. This could leave you wide open to nasty tricks, including spam relaying off your web form.

  • mhernandez Oct 20, 2008 @ 20:38

    Once again, nice post!
    However, when I tried this with a page of mine a couple weeks ago I noticed that the emails didn’t arrive to their destinataries.
    Is it enough condition that the @ in the from field matches the domain where Apache lies for the mails to get to their targets?

  • 🐧 nixCraft Oct 20, 2008 @ 20:48


    mail() requires:

    + sendmail binary installed
    + properly configured email server
    + Permission to use sendmail binary

    If everything is installed with permission check out your mail server log file.

  • amonic Oct 21, 2008 @ 14:22

    Try to use phpMailer. It has a lot of settings. Can send email via mail(), sendmail, smtp

  • Anjanesh Oct 28, 2008 @ 5:07

    SwiftMailer is far more easier and efficient. It has a lot more features for heavy email requirements.

  • vcishal ghuna Nov 18, 2008 @ 8:47

    ta ggt ….pa p compran nanier
    translate:its so clear man..thnx 4 sharing

  • Digital Lynx Jul 8, 2009 @ 4:14

    mMail packs most of this into an easy to use class library. Might want to give that a try:

  • Marko Feb 16, 2010 @ 17:53

    First – thanks;
    Second question, how to add more than just one to: email addres1, email address2…



  • souvik dasgupta Jan 18, 2013 @ 6:27

    nice codes!!!!! love it!!!

  • vipul Jul 6, 2013 @ 8:07

    how to solve following error:

    please help me.

    Warning: mail(): SMTP server response: 530 5.7.0 Must issue a STARTTLS command first. z5sm11073285pbk.0 – gsmtp in C:\wamp\www\work\SendMail.php on line 12

  • alfiza Feb 4, 2014 @ 13:44

    thanks to post the script .but Its not work on ma local showing an error like ( Failed to connect to mailserver at “localhost” port 25, verify your “SMTP” and “smtp_port” setting in php.ini ) please replay.thanks in advanced…

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum