- Forums
- PHP
- PHP - How To Send Emails With PHP Scripts Code Same Example Mail() Function
this page is a tutorial for you to be able to send email from your php script, i kept it very simple as php is a very simple programming langague it allows you to send emails very easy, here i give you example code to create your own scripts to send email [973], Last Updated: Sat May 18, 2024
john607
Sun Nov 19, 2006
4 Comments
1332 Visits
its very easy to send emails with php. if you don't have semail or your sendmail server is disable, the mail() PHP function is a great alternative.
What is the PHP mail() function? - you can send emails with this function usin php code
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
[email protected] from me (john) and my email address is
[email protected] 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 = '
[email protected]';
# Sender's Name
$from_name = 'John Hernandez';
#Sender's Email
$from_email = '
[email protected]';
# 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<
[email protected]>
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.