How to send mails with PHPMailer

PHP mail() is an convenient solution of sending emails, it was easy to configure and didn’t take much time to get it work. Recently I found that Hotmail, Live.com couldn’t receive any mail using this built-in PHP function. It seems like some mail server require authentication before you send an email, and PHP mail() is not very capable of it. So I found this PHPMailer is really a handy alternative class for PHP mail().

PHPMailer Introduction

PHPMailer is a PHP class for sending email. It has far more features than the regular mail() function, such as containing attachments and inline images. It is very useful for actions like “Contact Us” forms, and it avoids header injection and spamming.

Preparation

1.Download PHPMailer from its project page. the newest version is 5.2.4.

2.Extract class.phpmailer.php and class.smtp.php to your project directory, we’ll need them later.

Create your mailsending function

Use the following code to create a new function used for sending mails, you need to modify the smtp settings.

function sendmail($to,$subject = '',$body = ''){
    //Author:Codemug Website: http://codemug.com
    //$to:receiver's email address; $subject:title of the mail; $body: content of the mail
    //error_reporting(E_ALL);
    error_reporting(E_STRICT);
    date_default_timezone_set('America/Los_Angeles');//Set the server's timezone
    require_once('class.phpmailer.php');
    include('class.smtp.php');
    $mail             = new PHPMailer(); //Initialize a new PHPMailer object;
    $body            = eregi_replace("[\]",'',$body); //Replace unwanted characters of the content
    $mail->CharSet ="ISO-8859-1";//Set the character set you need to specify
    $mail->IsSMTP(); // Use SMTP service
    $mail->SMTPDebug  = 1;                     // Enable debugging for SMTP
    // 1 = errors and messages
    // 2 = messages only
    $mail->SMTPAuth   = true;                  // Enable authentication feature for the SMTP server
    $mail->SMTPSecure = "ssl";                 // Use SSL, you may comment this line out
    $mail->Host       = 'smtp.hotmail.com';      // SMTP server
    $mail->Port       = 25;                   //SMTP port, not all email services use default port 25, please refer to your mail service provider.
    $mail->Username   = 'somebody';  //Username of your email account
    $mail->Password   = 'password';            //Password of your email account
    $mail->SetFrom('xxx@xxx.xxx', 'who');
    $mail->AddReplyTo('xxx@xxx.xxx','who');
    $mail->Subject    = $subject;
    $mail->AltBody    = 'To view the message, please use an HTML compatible email viewer!'; // optional, comment out and test
    $mail->MsgHTML($body);
    $address = $to;
    $mail->AddAddress($address, '');
    //$mail->AddAttachment("images/phpmailer.gif");      // attachment
    //$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
    if(!$mail->Send()) {
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    } else {
//        echo "Message sent successfully!";
    }
}

Use the function

postmail('somebody@somehost.com','Howdy!','Hello, my friend!');

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>