Where I put SMTP details and password
Add the HTML form to your page
Generally, your setup would include a form being sent to a PHP script for processing. In this case, there is a basic HTML file, contact_us.html, that makes up a feedback form. You will need to set the action of your form to âemail.phpâ for the PHP script to process. Below is the code you need for the form in your page.
<form method="post" action="email.php">
Email: <input name="email" id="email" type="text" /><br />
Message:<br />
<textarea name="message" id="message" rows="15" cols="40"></textarea><br />
<input type="submit" value="Submit" />
</form>
Add the PHP mail() function code
When the form is filled out, the information is passed over to âemail.phpââ, which currently uses PHPâs mail() function to send the message. Below is the PHP code that sends the email from the server.
<?php
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
// here we use the php mail function
// to send an email to:
// you@example.com
mail( "example@example.com", "Feedback Form Results",$message, "From: $email" );
?>
Add the PHPMailer code to your site
Because we are using the PHPMailer instead of the generic php mail function, weâll begin to update our âemail.phpâ file. If you look in your PHPMailer folder youâll see a README file that includes sample PHP code. The sample PHP code should look like below:
<?php
require("class.PHPMailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "smtp.gmail.com;smtp2.gmail.com"; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "email@gmail.com"; // SMTP username
$mail->Password = "secret123"; // SMTP password
$mail->From = "from@example.com";
$mail->FromName = "Mailer";
$mail->AddAddress("josh@example.net", "Josh Adams");
$mail->AddAddress("ellen@example.com"); // name is optional
$mail->AddReplyTo("info@example.com", "Information");
$mail->WordWrap = 50; // set word wrap to 50 characters
$mail->AddAttachment("/var/tmp/file.tar.gz"); // add attachments
$mail->AddAttachment("/tmp/image.jpg", "new.jpg"); // optional name
$mail->IsHTML(true); // set email format to HTML
$mail->Subject = "Here is the subject";
$mail->Body = "This is the HTML message body <b>in bold!</b>";
$mail->AltBody = "This is the body in plain text for non-HTML mail clients";
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
?>
The great thing about this example is that it includes comments, which explain what most of the code does.
Final Configuration of the PHPMailer code
Weâve updated the sample code to work with our initial html contact form (first code example on this page). We updated the code to include additional comments, and the final code we used is below.
Note you will also need an additional file for the code below. Upload the PHPMailerAutoload zip file to your cPanel and extract it into your domain root folder. This file was taken from github and is up to date as of 1/13/2015. It is designed to create the proper structure when extracted. If you want the most up to date copy, you can get it from here, but will need to create a lib/PHPMailer directory in your account to upload it to.
[code]<?php
// $email and $message are the data that is being
// posted to this page from our html contact form
$email = $_REQUEST[âemailâ] ;
$message = $_REQUEST[âmessageâ] ;
// When we unzipped PHPMailer, it unzipped to
// public_html/PHPMailer_5.2.0
require(âlib/PHPMailer/PHPMailerAutoload.phpâ);
$mail = new PHPMailer();
// set mailer to use SMTP
$mail->IsSMTP();
// As this email.php script lives on the same server as our email server
// we are setting the HOST to localhost
$mail->Host = âmail.example.comâ; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
// When sending email using PHPMailer, you need to send from a valid email address
// In this case, we setup a test email account with the following credentials:
// email: send_from_example@googlemail.com
// pass: password
$mail->Username = "send_from_example@googlemail.com"; // SMTP username
$mail->Password = âpasswordâ; // SMTP password
// $email is the userâs email address the specified
// on our contact us page. We set this variable at
// the top of this page with:
// $email = $_REQUEST[âemailâ] ;
$mail->From = $email;
// below we want to set the email address we will be sending our email to.
$mail->AddAddress("example@googlemail.com", âYour Nameâ);
// set word wrap to 50 characters
$mail->WordWrap = 50;
// set email format to HTML
$mail->IsHTML(true);
$mail->Subject = âYou have received feedback from your website!â;
// $message is the userâs message they typed in
// on our contact us page. We set this variable at
// the top of this page with:
// $message = $_REQUEST[âmessageâ] ;
$mail->Body = $message;
$mail->AltBody = $message;
if(!$mail->Send())
{
echo "Message could not be sent.
";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo âMessage has been sentâ;
?>
[/code]
So in the test the user first visits contact_us.html. When they fill in their email address and comment, the information is posted to email.php. email.php calls PHPMailer, and then sends us the data that the user submitted.