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 http://phpmailer.sourceforge.net , 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" />
Message:
<textarea name="message" rows="15" cols="40"></textarea>
<input type="submit" />
</form>

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

<?php
  $email = $_REQUEST[’email’] ;
  $message = $_REQUEST[‘message’] ;

  mail( "me@mydomain.com", "Feedback Form Results",
    $message, "From: $email" );
  header( "Location: http://www.domain.com/thankyou.html" );
?>

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:

<?php
require("phpmailer/class.phpmailer.php");
$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:

<?php
require("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "localhost";
$mail->From = "your@emailaddress.com";
$mail->FromName  =  "Your Name";
$mail->AddAddress("recipient_1@domain.com");
$mail->AddAddress("recipient_2@domain.com");

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

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

if(!$mail->Send())
{
   echo ‘Message was not sent.’;
   echo ‘Mailer error: ‘ . $mail->ErrorInfo;
}
else
{
   echo ‘Thank you for your feedback.’;
}
  $email = $_REQUEST[’email’] ;
  $message = $_REQUEST[‘message’] ;
?>

To explain what variable we are defining here:

$mail->IsSMTP();

 

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.yourdomain.com

$mail->From = “your@emailaddress.com”;

 

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

$mail->AddAddress(“recipient_1@domain.com”);

 

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:

http://phpmailer.sourceforge.net/docs/

Source : https://support.inmotionhosting.com

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:

WordPress.com Logo

Bạn đang bình luận bằng tài khoản WordPress.com Log Out / Thay đổi )

Twitter picture

Bạn đang bình luận bằng tài khoản Twitter Log Out / Thay đổi )

Facebook photo

Bạn đang bình luận bằng tài khoản Facebook Log Out / Thay đổi )

Google+ photo

Bạn đang bình luận bằng tài khoản Google+ Log Out / Thay đổi )

Connecting to %s