Use phpmailer for automatic email notification
In a recent small project, you need to use the mail automatic notification function. after searching, we found that phpmailer is a lot of things.
First, go to sourceforge to download phpmailer, or go to my online storage. I use version 5.1.
The phpmailer directory contains three class files, which are the core of phpmailer. you can copy these files to your project.
Phpmailer is easy to use, as follows:
require("class.phpmailer.php");
$ Mail = new PHPMailer (); // Create a mail sending class
$mail->CharSet = "GB2312";
$ Mail-> IsSMTP (); // send using SMTP
$ Mail-> SMTPAuth = true; // enable SMTP verification
$mail->Port=25;
$ Mail-> Host = "smtp.qq.com"; // your enterprise Post Office domain name
$ Mail-> Username = "frommail@qq.com"; // Post Office Username (enter the complete email address)
$ Mail-> Password = "213123"; // Post Office Password
$ Mail-> From = "frommail@qq.com"; // email address of the sender
$mail->FromName = "tester";
$ Address = "tomail@qq.com"; // recipient address
$mail->AddAddress("$address", "a");
$ Mail-> Subject = "test message notification ";
$ Mail-> Body = "Hello! Information in the system is not reviewed. "; // Email content
if(!$mail->Send())
{
Echo "failed to send the email.";
Echo "error cause:". $ mail-> ErrorInfo;
exit;
}
You can call the database before sending a message to check whether a message needs to be sent.
Because our project runs on a windows Server, we need to regularly check whether emails need to be sent. In Linux, you can use crontab to easily solve the problem. in Windows, you need to use the windows task plan.
First, you need to write a bat script, but how does a black box appear when the bat script executes the task plan? it is quite scary. Therefore, first write a vbs and use it to call the bat script, the bat script then calls the php implementation function.
The vbs content is as follows:
Set ws = CreateObject("Wscript.Shell")
ws.run "cmd /c E:\xampp\htdocs\sdc\application\mail\timeSend.bat",vbhide
Vbs calls timeSend. bat. the content is as follows:
E:\xampp\php\php.exe E:\xampp\htdocs\sdc\application\mail\mail.php > E:\xampp\htdocs\sdc\application\mail\log.txt
OpenTask panel-> System and Security-> Management Tools-> schedule tasks, NewBasic Task, The time when the trigger wants to be triggered, and select the operationStart the program, Browse and select your own vbs program, and click finish.
In this way, the program can send emails at a specified time.
|