In this lesson of the PHP tutorial, you will learn...
1. To send emails using PHP's built-in mail() function.
2. To send email using PHPMailer, a PHP extension with more features than mail().
mail()
PHP has a built-in mail() function that makes it easy to send email.
Mail Parameters Method Description
To The address to send the email to.
Subject The email's subject.
Message The body of the email.
Additional Headers Optional. Additional headers (e.g, From, Reply-To)
Additional Parameters Optional. Any additional parameters you may want to send to your mail server.
Code Sample: Email/Demos/Mail.php
if (!array_key_exists('Submitted',$_POST))
{
?>
}
else
{
ini_set('SMTP',$_POST['Host']);
$To = $_POST['To'];
$From = 'From: ' . $_POST['From'];
$Subject = $_POST['Subject'];
$Message = $_POST['Message'];
if(mail($To,$Subject,$Message,$From))
{
echo "Message Sent";
}
else
{
echo "Message Not Sent";
}
}
?>
Code Explanation
For this example to work, you will need to have a mail server set up on your server.
The first time a visitor hits the page, he'll be presented with a form. When the user fills out and submits that form, the mail() function will attempt to send an email to the address the user entered.
Note that the mail server is set with the ini_set() function, which is used to temporarily change configuration settings. You can set the default mail server in the php.ini file with the SMTP setting.
Shortcomings of mail()
The mail() function has many limitations.
* No support for SMTP authentication.
* Difficult to send HTML-formatted emails.
* Difficult to add attachments.
Luckily, there are extensions that do provide these features.
PHPMailer
A very good email extension is PHPMailer, which is available for free at http://phpmailer.sourceforge.net. We will use PHPMailer in our examples and exercises. The following tables show some of the more common methods and properties of PHPMailer.
PHPMailer Methods Method Description
AddAddress() Adds a "To" address.
AddAttachment() Adds an attachment from a path on the filesystem.
AddBCC() Adds a "bcc" address.
AddCC() Adds a "cc" address.
AddReplyTo() Adds a "Reply-to" address.
IsHTML() Sets message type to HTML.
IsSMTP() Sets Mailer to send message using SMTP.
Send() Creates message and assigns Mailer. If the message is not sent successfully then it returns false.
PHPMailer Properties Property Description
AltBody Sets the text-only body of the message.
Body Sets the Body of the message. This can be either an HTML or text body.
ErrorInfo Holds the most recent mailer error message.
From Sets the From email address for the message.
FromName Sets the From name of the message.
Host Sets the SMTP hosts. All hosts must be separated by semicolons.
Password Sets SMTP password.
SMTPAuth Sets SMTP authentication. Utilizes the Username and Password properties.
Subject Sets the Subject of the message.
Username Sets SMTP username.
WordWrap Sets word wrapping on the body of the message to a given number of characters.
Code Sample: Email/Demos/PHPMailer.php
if (!array_key_exists('Submitted',$_POST))
{
?>
}
else
{
require("class.phpmailer.php");
$To = $_POST['To'];
$From = $_POST['From'];
$FromName = $_POST['FromName'];
$Subject = $_POST['Subject'];
$Message = $_POST['Message'];
$Host = $_POST['Host'];
if (array_key_exists('HTML',$_POST))
{
$HTML = true;
$Mail->Username=$_POST['Username'];
$Mail->Password=$_POST['Password'];
}
else
{
$HTML = false;
}
$Mail = new PHPMailer();
$Mail->IsSMTP(); // send via SMTP
$Mail->Host = $Host; //SMTP server
if (array_key_exists('Username',$_POST))
{
$Mail->SMTPAuth=true;
}
else
{
$Mail->SMTPAuth=false;
}
$Mail->From = $From;
$Mail->FromName = $FromName;
$Mail->AddAddress($To);
$Mail->AddReplyTo($From);
$Mail->WordWrap = 50; // set word wrap
$Mail->IsHTML($HTML);
$Mail->Subject = $Subject;
$Mail->Body = $Message;
if($Mail->Send())
{
echo "Message Sent";
}
else
{
echo "Message Not Sent
";
echo "Mailer Error: " . $Mail->ErrorInfo;
}
}
?>
Code Explanation
As you can see, PHPMailer comes with a full set of intuitive methods and properties that make sending emails very easy.
0 comments: on "Sending Email with PHP mail() object"
Post a Comment