≡ Menu

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 = "vivek@nixcraft.co.in";
// 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 = 'user@example.com';
$subject = 'Test';
$message = 'This is a test.';
// set headers as per your requirements. 
$headers = 'From: webmaster@nixcraft.co.in' . "\r\n" .
    'Reply-To: webmaster@nixcraft.co.in' . "\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

{ 11 comments… add one }

  • willc October 20, 2008, 8:05 pm

    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 October 20, 2008, 8:38 pm

    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 October 20, 2008, 8:48 pm


    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 October 21, 2008, 2:22 pm

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

  • Anjanesh October 28, 2008, 5:07 am

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

  • vcishal ghuna November 18, 2008, 8:47 am

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

  • Digital Lynx July 8, 2009, 4:14 am

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


  • Marko February 16, 2010, 5:53 pm

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



  • souvik dasgupta January 18, 2013, 6:27 am

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

  • vipul July 6, 2013, 8:07 am

    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 February 4, 2014, 1:44 pm

    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 Comment

   Tagged with: , , , , , , , , , , , ,