Using phpMailer to Send Mail through PHP
Besides using PHP's mail() function, there are a few other tools you can use to send mail from a PHP script. In this article, we'll show you how you can use PHPMailer to to send email from within PHP.
When sending email from PHPMailer, you will be sending
email from an actual email address. If you don't already have an email
address setup specifically for sending email, please setup an email address now. In the example below, we created the email address send_from_PHPMailer@bradm.inmotionhosting.com with "password" as the password.
Download the PHPMailer script
First, download PHPMailer using the direct link below:After you have downloaded the file, unzip and extract it to your public_html. After unzipping the file we have public_html/PHPMailer_5.2.0. Next you will need to edit your web pages to use the PHPMailer code.
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@yourdomain.com mail( "bradm@inmotiontesting.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 = "smtp1.example.com;smtp2.example.com"; // specify main and backup server $mail->SMTPAuth = true; // turn on SMTP authentication $mail->Username = "jswan"; // SMTP username $mail->Password = "secret"; // 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:<?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 = "localhost"; // 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_PHPMailer@bradm.inmotiontesting.com // pass: password $mail->Username = "send_from_PHPMailer@bradm.inmotiontesting.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("bradm@inmotiontesting.com", "Brad Markle"); // 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. <p>"; echo "Mailer Error: " . $mail->ErrorInfo; exit; } echo "Message has been sent"; ?>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.
Comments
Post a Comment