PHP sendmail Settings

Using phpMailer to Send Mail through PHP

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" />

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.


  $email = $_REQUEST['email'] ;
  $message = $_REQUEST['message'] ;

  // here we use the php mail function
  // to send an email to:
  mail( "", "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:


$mail = new PHPMailer();

$mail->IsSMTP();                                      // set mailer to use SMTP
$mail->Host = ";";  // specify main and backup server
$mail->SMTPAuth = true;     // turn on SMTP authentication
$mail->Username = "jswan";  // SMTP username
$mail->Password = "secret"; // SMTP password

$mail->From = "";
$mail->FromName = "Mailer";
$mail->AddAddress("", "Josh Adams");
$mail->AddAddress("");                  // name is optional
$mail->AddReplyTo("", "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";

   echo "Message could not be sent. <p>";
   echo "Mailer Error: " . $mail->ErrorInfo;

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



// $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

$mail = new PHPMailer();

// set mailer to use SMTP

// 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:
// pass: password
$mail->Username = "";  // 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("", "Brad Markle");

// set word wrap to 50 characters
$mail->WordWrap = 50;
// set email format to HTML

$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;

   echo "Message could not be sent. <p>";
   echo "Mailer Error: " . $mail->ErrorInfo;

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.phpemail.php calls PHPMailer, and then sends us the data that the user submitted.

Note that PHPMailer is very flexible and has more features than described here. For a listing of the optional variables that you can add, you should read the documentation:

  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

301 Redirect from non-www to ww -- Without Looping!

o - you want to improve your search engine rankings, and SEO gurus told you that Google might be...

Feedback Form Code / Instructions

Feedback Form Code / InstructionsThe source code for the two files (feedback.php and feedout.php)...

How Pirated Themes Harm your site?

Nulled or pirated themes may contain malicious codes. Some part of coding in Encrypted form,...

How to install Laravel with Softaculous

Laravel is the new web framework for web artisans and is now being available for installation on...

How to move Wordpress from one URL to another URL

Changing URLs on Wordpress is unlike other scripts in which you would have a configuration file...

Powered by WHMCompleteSolution