Using php mailer to send email in php

Wikitechy | 1485 Views | php | 09 Jun 2016

 

Step1:

  • Download the php mailer
  • First download the php mail “PHPMailer_5.2.0.zip”
  • After downloading the file, unzip it and extract it to our public_html. 
  • After unzipping the file, we have public_html/PHPMailer_5.2.0. 
  • Next we need to edit our web pages in order to use the PHPMailer code.

Step2 :

  • Add the HTML form to your page
  • Generally, our php setup will include a form being sent to a PHP script for processing. 
  • Considering this case, there is a basic HTML file that is contact_us.html, will makes up a feedback form.
  • We need to set the action of our form to "email.php" for the PHP script in order to process. 
  • Below is the code which we need for the form in our 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>

Step3 :

  • Add the PHP mail() function code
  • Typically When the form is filled out, the information will be passed over to "email.php"', which currently uses the PHP's mail() function in order to send the message. 
  • Below is the PHP code which we have shown where it 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( "venkat@wikitechy.com", "Feedback Form Results",$message, "From: $email" );
?>

Step4 :

  • Add the PHPMailer code to our site
  • Because we are using the phpmailer instead of the generic php mail function, so update our modified "email.php" file. 
  • Then we will begin to update our "email.php" file.
  • In our PHPMailer folder we will see a README file that includes sample PHP code. 
  • The sample PHP code will be like this format:
<?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";
?>

Step5 :

  • Final Configuration of the PHPMailer code
  • Now We have to updated the sample code to work with our initial html contact form. 
  • After updating the code to include additional comments, and the final code we use has been below 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"); $email = new PHPMailer(); // set mailer to use SMTP $email->IsSMTP(); // As this email.php script lives on the same server as our email server // we are setting the HOST to localhost $email->Host = "localhost"; // specify main and backup server $email->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@venkat@wikitechy.com // pass: password $email->Username = "send_from_PHPMailer@venkat@wikitechy.com"; // SMTP username $email->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'] ;
$email->From = $email; // below we want to set the email address we will be sending our email to. $email->AddAddress("venkat@wikitechy.com", "venkat"); // set word wrap to 50 characters $email->WordWrap = 50; // set email format to HTML $email->IsHTML(true); $email->Subject = "You have received feedback from your website!" $email->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'] ; $email->Body = $message; $email->AltBody = $message; if(!$mail->Send()) { echo "Message could not be sent. <p>"; echo "Mailer Error: " . $mail->ErrorInfo; exit; } echo "Message has been sent"; ?>

Applies to :

  • PHP 3 and 4
  • PHP 5
  • PHP 6 and Unicode
  • PHP 7

Related Tags :

  • Using phpMailer to Send Mail through PHP
  • Sending Emails in PHP with PHPMailer
  • How to Send with PHPMailer
  • Basic Example using Sendmail through the PHP Mailer
  • send email using Gmail SMTP server through PHP Mailer
  • Sending e-mails via SMTP with PHPMailer 
  • phpmailer send email example
  • phpmailer send email without smtp