PHP Send Email

by on October 20, 2008 · 11 comments· LAST UPDATED October 20, 2008

in , ,

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

<?php
// 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.");
}
else
{
     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):

<?php
$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!';
}
else
{
	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
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 11 comments… read them below or add one }

1 willc October 20, 2008 at 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.

Reply

2 mhernandez October 20, 2008 at 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?

Reply

3 nixCraft October 20, 2008 at 8:48 pm

mhernandez,

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.

Reply

4 amonic October 21, 2008 at 2:22 pm

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

Reply

5 Anjanesh October 28, 2008 at 5:07 am

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

Reply

6 vcishal ghuna November 18, 2008 at 8:47 am

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

Reply

7 Digital Lynx July 8, 2009 at 4:14 am

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

http://www.hotscripts.com/listing/mmail-php-class/

Reply

8 Marko February 16, 2010 at 5:53 pm

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

Thanks!

Marko

Reply

9 souvik dasgupta January 18, 2013 at 6:27 am

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

Reply

10 vipul July 6, 2013 at 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

Reply

11 alfiza February 4, 2014 at 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…

Reply

Leave a Comment

Tagged as: , , , , , , , , , , , ,

Previous Faq:

Next Faq: