Go Back   Ancestry Aid Genealogy and Family History Forum » Misc Board » The Coffee Room
Register Login AA Calendar NEW! Groups Mark Forums Read

Thread: Tutorial 04 - Basic Email

Creating a small PHP script to send an email is a very simple thing to do. Firstly we want to .......


Reply
 
LinkBack Thread Tools
Old 16-11-2006, 11:33 AM   #1 (permalink)
Jon
Webmaster & Developer
 
Jon's Avatar
 
Join Date: Dec 2005
Location: UK - England
Posts: 993
Exclamation Tutorial 04 - Basic Email

Creating a small PHP script to send an email is a very simple thing to do.

Firstly we want to create some variables and asign each of them with the required values.

PHP Code:
<?php // The opening PHP tag
 
$msgbody "This is my first PHP email"// This has asigned the email body value
 
$from "you@somewhere.com"// Your email address
 
$to "them@somewhere.com"//The person you are sending the email to
 
$subject "My First PHP Email"// The title of the email
 
$headers "From: $from"// Placing the headers to show who the email is from
 
// Close the PHP tag
 
?>
Now that the variables have been asigned with a value we can now use the PHP mail() function to send the email. The format of the function is as follows:

PHP Code:
mail($to$subject$msgbody$headers); 
The above piece of code will actually send the code. Lets put that all together and add another piece of code with it:

PHP Code:
<?php // The opening PHP tag
 
$msgbody "This is my first PHP email"// This has asigned the email body value
 
$from "you@somewhere.com"// Your email address
 
$to "them@somewhere.com"//The person you are sending the email to
 
$subject "My First PHP Email"// The title of the email
 
$headers "From: $from"// Placing the headers to show who the email is from
 
mail($to$subject$msgbody$headers); //Use the mail function to send the email
 
// Check the email was sent
 
if(mail($to$subject$msgbody$headers)) { //If the email has been sent
 
echo "Your Mail Has Been Sent"// Mail sent ok
 
} else { //If it has not been sent do this.
 
echo "Error sending your email."//Mail could not be sent
 
}
 
// Close the PHP tag
?>
The last piece of code is checking to see if the email was sent. If it was it will state "Your mail has been sent" but if there was an error the the second message would be displayed.

Next tutorial: Send email with the use of forms.

Jon
Ancestry Aid Webmaster & Developer



Jon is offline   Reply With Quote Top
Advertisement
Reply

Bookmarks

Thread Tools


Advertisement

This is a Genealogy site.