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:
- PHP: Verify And Sanitize Email Address
- PHP Send Email Using Authenticated SMTP Mail Server In Real Time
- Install PHP Pear Mail / SMTP package on CentOS / Red Hat Enterprise Linux
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop













{ 9 comments… read them below or add one }
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.
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?
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.
Try to use phpMailer. It has a lot of settings. Can send email via mail(), sendmail, smtp
SwiftMailer is far more easier and efficient. It has a lot more features for heavy email requirements.
ta ggt ….pa p compran nanier
translate:its so clear man..thnx 4 sharing
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/
Hi,
First – thanks;
Second question, how to add more than just one to: email addres1, email address2…
Thanks!
Marko
nice codes!!!!! love it!!!