Sunday, April 27, 2008

Using PHPMailer of WordPress to Send Email

In PHP langauge, the usage of the mail() function is too complex. Besides, when you are working on Windows operating system, it is diffcult to find an appropriate "sendmail" program. But if you work on Linux or Unix, mail() function may work better, althought its arguments are too complex.

I read the source code of WordPress this afternoon and found two classes for sending mail -- PHPMailer and SMTP. WordPress uses PHPMailer to send new password when you lost your password. Moreover, PHPMailer supports multiple sending method, such as PHP function main(), Qmail, remote SMTP server or other "sendmail" programs. Especially SMTP server. So far, there are fewer and fewer anonymous SMTP servers on the Internet, most of them need authentication. SMTP class make these things simple and easy. When you let PHPMailer use remote SMTP server to send Email, PHPMailer will use SMTP to connect to the remote server to accomplish the task.

Now let's see how to use PHPMailer to send your Email.

Firstly, you need to copy class-phpmailer.php and class-smtp.php from wp-includes directory of WordPress to your working directory. Then make a new PHP file in your working directory to enter codes. See below:

<?php
include ('class-phpmailer.php');
include ('class-smtp.php');

$mail=new PHPMailer();

// Let PHPMailer use remote SMTP Server to send Email
$mail->IsSMTP();

// Set the charactor set. The default is 'UTF-8'
$mail->CharSet='UTF-8';

// Add an recipients. You can add more recipients
// by using this method repeatedly.
$mail->AddAddress('xxxxxxx@gmail.com');

// Set the body of the Email.
$message='This Email is sent by PHPMailer of WordPress';
$mail->Body=$message;

// Set "From" segment of header.
// For 163.com Email service, it must be the same
// with your account.
$mail->From='xxxxxxx@163.com';

// Set addresser's name
$mail->FromName='Wang Jinbo';

// Set the title
$mail->Subject='Test Mail';

// Set the SMTP server.
$mail->Host='smtp.163.com';

// Set "need authentication".
$mail->SMTPAuth=true;

// Set your username and password.
$mail->Username='xxxxxx';
$mail->Password='xxxxxx';

// Send Email.
$mail->Send();
?>


Here I described how to use PHPMailer to send Email. Of course, there are other options in PHPMailer class. You can understand the usage of it by reading the source code and trying it in person.

5 comments:

stephen said...

Hi,

Been looking for a simple php script I could use to send email from a WAMP server (most email plugins don't work on that platform): this worked first go!

thanks

Stephen

Tony said...

Hello,

I am desperately in need of an ability to send email from a contact form on WordPress.

In saying that, I have come across your page, and now I would like to use the option that you suggest, however im not quite sure how to go about it.

I have already copied over the files you suggested to the main directory of my site, which happens to be "wordpress". That is also the file folder where when main wordpress files where installed.

In addition to that, I used the code listed on your website, and pasted into notepad to make a php file called mailform.php.

With that in mind, how do I go about using it now....

I would really appreciate your assistance on this greatly...

Hoping to hear from you soon..

Tony.

wpthemes said...

question ...

is the username and password is email username and password or domain username and password or what?

Thanks for the tutorial

Dan Smart said...

@wpthemes - it's your SMTP email username and password.

Ardee Aram said...

I see, so what the Configure SMTP plugin does (http://wordpress.org/extend/plugins/configure-smtp/) is just configure PHPMailer. SMTP is built-in to WordPress, it's just a matter of configuration.

Nice article, very helpful!