PHP Send Email

Posted: November 3, 2008 in LINUX
Tags:

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!’; } ?> 

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s