What is the PHP mail() function?
The mail() function allows you to send mail.
What Do I need To Get it Working?
For the Mail functions to be available, PHP must have access to the sendmail binary on your system during compile time. If you use another mail program, such as qmail or postfix, be sure to use the appropriate sendmail wrappers that come with them. PHP will first look for sendmail in your PATH, and then in the following: /usr/bin:/usr/sbin:/usr/etc:/etc:/usr/ucblib:/usr/lib. It's highly recommended to have sendmail available from your PATH. Also, the user that compiled PHP must have permission to access the sendmail binary.
How do I install it?
There is no installation needed to use these functions; they are part of the PHP core.
Ok, now i will show you how to do this. copy and paste the following code into your php document. Dreamweaver is a good WYSIWYG, but for this tutorial guide, i will be using a simple text editor, notepad.exe in windows xp. so open notepad and copy and past the following code:
Ok, in my example, i will be sending an email to steve at steve@domain.com from me (john) and my email address is john@domain.com I want to tell john about the cool and beautiful wallpapers i found at wallpaperama.com. so here is how my code would look like:
Code:
<?php
# EMAIL MESSAGE By Wallpaperama.com
# Email Address of receiver
$to_email = 'steve@domain.com';
# Sender's Name
$from_name = 'John Hernandez';
#Sender's Email
$from_email = 'john@domain.com';
# Email Subject Title
$subject = 'Cool Wallpapers';
# The email message
$message ='
Hi Steve,
I want to invite you to visit wallpaperama.com,
They have cool and beautiful wallpapers.
You can visit them at http://www.wallpaperama.com
Talk to you later,
John ';
# STOP
############################################################################
##### D O N O T E D I T B E L O W T H I S L I N E ####
############################################################################
# The Header
$mailheaders = 'From: '.$from_name.'<'.$from_email .'>';
# This is the function that does it all
mail($to_email, $subject, $message, $mailheaders);
?>
<style type="text/css">
<!--
.border {
background-color: #FFFFE6;
border: 1px solid #FF6600;
padding: 5px;
width: 300px;
}
-->
</style>
<h1 align="center">Congratulation!</h1><hr align="center" width="50%" />
<p><strong>Email has been set to: <?php echo $to_email; ?></strong></p>
<p><strong>This is how the email will look like:</strong></p>
<p class="border"><strong>From: <?php echo $header; ?> </strong></p>
<p class="border"><strong>Subject: <?php echo $subject; ?></strong></p>
<p><strong class="border">Message: </strong><?php echo $message; ?> </p>
<p><a href="http://www.wallpaperama.com"><< Go Back </a></p>
Now edit these variable ONLY, and the script should work;
$to_email
$from_name
$from_email
$subject
$message
IMPORTANT: REMEMBER TO MAKE CHANGES WITH REAL EMAIL ADDRESES, THE ONES I PROVIDED IN THIS SAMPLE, ARE FAKE AND IT WILL MAKE THIS SCRIPT FAIL.
Step 2. Now save this file as wallpaperama-email.php and uploaded it into your server.
Step 3. Now that you have uploaded the wallpaperama-email.php file into your server, call it with your browser
(example: http://www.yourdomain.com/wallpaperama-email.php)
once you do that, you will see a "Successful" message, when the receiver gets this email, it will look like this in plain text (not html) format:
Code:
From: John<john@domain.com>
To: steve @ domain.com
Subject: Cool Wallpapers
Date: Sun, 19 Nov 2006 23:39:17 +0000
>
> Hi Steve,
>
> I want to invite you to visit wallpaperama.com,
>
> They have cool and beautiful wallpapers.
>
> You can visit them at http://www.wallpaperama.com
>
> Talk to you later,
>
> John
To: steve @ domain.com
Subject: Cool Wallpapers
Date: Sun, 19 Nov 2006 23:39:17 +0000
>
> Hi Steve,
>
> I want to invite you to visit wallpaperama.com,
>
> They have cool and beautiful wallpapers.
>
> You can visit them at http://www.wallpaperama.com
>
> Talk to you later,
>
> John
Hope this helps, If for some reason the receiver is not receving emails, your sendmail server is not configure correctly. You will have to check with you hosting company or your admin. I tested this script and it works fine. But if you have any question, you can post them here.
Why do this tutorial? I remember when i started to do all this php stuff, it was hard to find information of howto's to do this kind of things, so now that i know how to do it, i want to share with anyone. While i was learning i remember coming accross web sites where they will show you stuff like this but you have to pay.. Hmm, this is my way of giving away something for free for the Open Source Community. After all PHP is Open Source, Remmember That! Nice People Those PHP Creators.