Thursday, April 2, 2009

Using PHP to send email

This feature is useful if you want your site to auto-send you alerts when a transaction is made, or user submits a request, etc. Here is a code to send a simple text email. You can find other codes (send HTML email, send email with attachments) at this site.

<?php
//define the receiver of the email
$to = 'youraddress@example.com';
//define the subject of the email
$subject = 'Test email';
//define the message to be sent. Each line should be separated with \n
$message = "Hello World!\n\nThis is my first mail.";
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed";
?>

Source: http://www.webcheatsheet.com/PHP/send_email_text_html_attachment.php

No comments:

Post a Comment