Tuesday, May 20, 2008

Send email using the PHP mail() function

How to send an email from within a PHP page using the built in mail() function.

The code



// Your email address
$email = "you@example.com";

// The subject
$subject = "Enter your subject here";

// The message
$message = "Enter your message here";

mail($email, $subject, $message, "From: $email");

echo
"The email has been sent.";

?>

Example 2:

How to Send Email from a PHP Script

PHP seems to make most things extremely easy. Sending mail is no exception.

Send Email from a PHP Script Example

The first argument to this function is the recipient, the second specifies the message's subject and the third one should contain the body. So to send a simple sample message, we could use:

$to = "recipient@example.com";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
if (mail($to, $subject, $body)) {
echo("

Message successfully sent!

");
} else {
echo("

Message delivery failed...

");
}
?>

That's it!

Example 3:
Using PHP to send mail
Let's get interactive with surfers again. I'd like to create a form mail system in PHP. First we need to create a form in HTML to gather the information. I am going to use one similar to my Perl from mailer script. because it looks clean.

The important thing, remember, when taking input in PHP is the field names for the information you are gathering. "From" will be converted into a variable with the same name by php (it would become $from), the "subject" will be made into $subject and the "contents" of the message will be made into $contents. For submitting the actual form, we will activate the php script by using the form action tag and linking to send_mail.php3:

<form action="send_email.php3" method="POST">

The $to variable will be defined in the send_mail.php3 script to point to my email address (you'll see the php code momentarily), of course. I also prefill the subject with the words "diary entry suggestion" which visitors can change to something else, if they want:

<input type="text" size="22" name="subject" value="diary entry suggestion">

Ok, let's take a look at the code to mail the contents of the above form to me and if the process is successful it will redirect you right back to this page. In order for this code to work you will need to know the path to sendmail on your server.

$to = "webmaster@php-scripts.com";
$from_header = "From: $from";

if($contents != "")
{
//send mail - $subject & $contents come from surfer input
mail($to, $subject, $contents, $from_header);
// redirect back to url visitor came from
header("Location: $HTTP_REFERER");
}
else
{
print("Error, no comments were submitted!");
}
?>

Notes: I make sure there are some comments submitted or else show an error message by using the != (not equal) with an if statement. When you want to redirect the browser using the header function (like when using the setcookie function) you must do it before any HTML. With the header and $HTTP_REFERER in the code above I am simply sending people who submit the form back where they came from, which should will be this page that calls the form. I could have easily changed the header line to read:

header("Location: http://www.apniphp.blogspot.com");

And at a page named thankyou.html I could have thanked them for submitting the form. I could have also added HTML directly to the send_email.php3 script.

Building an opt-in email list?

Bet some of you are wondering about the checkbox which indicates that "I would like to be notified when you release freeware or commercial php scripts" because that is not part of the code above. What does that do? Well that is my first stab at using PHP to build a mailing list. Go ahead and try to join the list repeatedly and you will see the script won't let you join twice with the same email address. In tomorrow's diary I will show you how I did this, but suffice to say if you check that box you will be added to our php-scripts mail list. Don't worry, I'm not going to spam you with unrelated information, or sell or give away your email address like a lot of other lame sites do. I will also show you how to create an unsubscribe script so that people in your opt-in email list can easily unsubscribe. It isn't very acceptable these days to create mail lists that people can't easily unsubscribe from. If you do stay on my mail list, however, I will notify you when I start releasing freeware and commercial PHP scripts on this site, which, depending on when you are reading this might be already :)

No comments: