Trang chủ » php » Using phpMailer to Send Mail through PHP

Using phpMailer to Send Mail through PHP

All of our newer-generation servers disable php from sending mail out as the user ‘nobody’, which is the default when sending mail using php’s mail() function. Even if you have a ‘from’ address configured, the envelope of the email will identify the mail as being sent from ‘nobody’, and most mail servers will automatically identify this type of mailing as SPAM.

In order to send mail with php, you will need to use SMTP with a valid username and password. If you are using a custom mailing script, the best way to do this is by using the phpMailer class. This walkthrough will demonstrate the basic method of using phpMailer to send email from our servers, and is intended for webmasters who are moderately familiar with php. Note that since your script may be written differently, you may need to make some adjustments to allow this modification to work for you.

**InMotion Hosting does not officially provide technical support for programming-related issues, and is limited to assisting with server-side variables only. If you are having difficulty with your script, please consult a developer.

First, download phpMailer from , then unzip the archive and upload the resulting ‘phpmailer’ folder into your public_html.

Generally your setup would include a form being sent to a php script for processing. In this case there is a basic HTML file that makes up a feedback form:

<form method="post" action="email.php">
Email: <input name="email" type="text" />
<textarea name="message" rows="15" cols="40"></textarea>
<input type="submit" />

When this form is filled out, the information is passed over to ‘email.php’, which currently uses the mail() function to send the message:

  $email = $_REQUEST[’email’] ;
  $message = $_REQUEST[‘message’] ;

  mail( "", "Feedback Form Results",
    $message, "From: $email" );
  header( "Location:" );

Since the mail() function will not allow the email to be sent, this is where we will incorporate phpMailer into the script to use SMTP. To do this, first you will need to use php’s require() function to include the class files that you’ve downloaded:

$mail = new PHPMailer();


Note that the require path should reflect the location of the phpmailer files that you uploaded! Now, your actual mailing script in this case would end up looking like my example below:

$mail = new PHPMailer();
$mail->Host = "localhost";
$mail->From = "";
$mail->FromName  =  "Your Name";

$mail->SMTPAuth = "true";
$mail->Username = "your@emailaddress";
$mail->Password =  "yourpassword";
$mail->Port  =  "25";

$mail->Subject = "Feedback form results";
$mail->Body = $message;
$mail->WordWrap = 50;

   echo ‘Message was not sent.’;
   echo ‘Mailer error: ‘ . $mail->ErrorInfo;
   echo ‘Thank you for your feedback.’;
  $email = $_REQUEST[’email’] ;
  $message = $_REQUEST[‘message’] ;

To explain what variable we are defining here:



This tells the script that the method of sending is SMTP

$mail->Host = “localhost”;


This is the hostname of the mail server being used. If you are sending from our server, this can be localhost or

$mail->From = “”;


The email address that the mail will appear to be from. This does not have to match your SMTP username, but must be a valid email address.

$mail->FromName = “Your Name”;


The name that will appear as the sender of the email



Who will be receiving the form results. You can add as many recipients as you want simply by duplicating this line in the script.

$mail->SMTPAuth = “true”;


This tells the script to use SMTP authentication, which is required to send mail through our servers.

$mail->Username = “your@emailaddress”;
$mail->Password = “yourpassword”;


These are your login credentials for SMTP, which should reflect a valid email username and password. If this is an account you set up from your cPanel, your username will be your entire email address.

$mail->Subject = “Feedback form results”;


This is the subject line of the email that will be sent.

$mail->Body = $message;


For the message body, you will need to rename $message with the variable that you created in your HTML form for the message field.

$email = $_REQUEST[’email’] ;
$message = $_REQUEST[‘message’] ;


These were in the original form script, but simple defines the variables that can be passed from the HTML form. This is in good practice with register_globals being disabled for security.

Now when you send your form, the form will use SMTP as if it were being sent from a mail client.

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:

Source :


2 thoughts on “Using phpMailer to Send Mail through PHP

Bình luận

Mời bạn điền thông tin vào ô dưới đây hoặc kích vào một biểu tượng để đăng nhập: Logo

Bạn đang bình luận bằng tài khoản Đăng xuất /  Thay đổi )

Google photo

Bạn đang bình luận bằng tài khoản Google Đăng xuất /  Thay đổi )

Twitter picture

Bạn đang bình luận bằng tài khoản Twitter Đăng xuất /  Thay đổi )

Facebook photo

Bạn đang bình luận bằng tài khoản Facebook Đăng xuất /  Thay đổi )

Connecting to %s