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.
1 comments:
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
Post a Comment