PHP Send Email

Posted on in Categories , , , , , , , , , , , , , , , last updated October 20, 2008

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 = "[email protected]";
 
// 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 = '[email protected]';
$subject = 'Test';
$message = 'This is a test.';
// set headers as per your requirements. 
$headers = 'From: [email protected]' . "\r\n" .
    'Reply-To: [email protected]' . "\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

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

11 comment

  1. 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.

  2. 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?

  3. 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

  4. 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