PHP Send Email

by Vivek Gite [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 = "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
Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 6 comments… read them below or add one }

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

2 mhernandez 10.20.08 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?

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

4 amonic 10.21.08 at 2:22 pm

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

5 Anjanesh 10.28.08 at 5:07 am

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

6 vcishal ghuna 11.18.08 at 8:47 am

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

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

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

Previous post: Linux: Find Out If a Particular Driver / Feature Compiled Into Running Kernel or Not

Next post: Linux / UNIX: Administer The Printer From a Web Browser