In a recent small project, I need to use the automatic email notification function. After searching, I found that phpmailer is used a lot.
First go to sourceforge to download phpmailer, or you can go to my network disk to download it. I am using version 5.1.
See that there are three class files in the phpmailer directory. This is the core of phpmailer. You can copy these files to your project.
-
It is very convenient to use phpmailer, as follows:
require("class.phpmailer.php"); </li>
<li>$mail = new PHPMailer(); //Create a mail sending class</li>
<li>$mail->CharSet = "GB2312"; </li>
<li>$mail- >IsSMTP(); // Use SMTP to send </li>
<li>$mail->SMTPAuth = true; // Enable SMTP authentication function </li>
<li>$mail->Port=25; </li>
<li>$mail->Host = "smtp.qq .com"; // Your business post office domain name </li>
<li>$mail->Username = "frommail@qq.com"; // Post office user name (please fill in the complete email address) </li>
<li>$mail->Password = "213123 "; //Post office password</li>
<li>$mail->From = "frommail@qq.com"; //Email address of the email sender</li>
<li>$mail->FromName = "tester";</li>
<li>$address = "tomail@qq. com";//Receive address</li>
<li>$mail->AddAddress("$address", "a");</li>
<li>$mail->Subject = "Test message notification";</li>
<li>$mail->Body = "You OK! There is a message in the system that has not been reviewed. "; //Email content</li>
<li>if(!$mail->Send())</li>
<li>{</li>
<li> echo "Failed to send email. <p>";</li>
<li> echo "Error reason: " . $mail->ErrorInfo;</li>
<li> exit;</li>
<li>}</li>
<li>
Copy code
You can call the database before sending the message to check whether there is any message that needs to be sent.
Since our project is run on a Windows server, we need to check regularly whether we need to send emails. It can be easily solved using crontab under Linux, and Windows Task Scheduler under Windows.
First of all, you need to write a bat script, but when the bat script executes the task plan, a black box appears, which is quite scary, so first write a vbs, use it to call the bat script, and then the bat script calls php to implement the function.
-
vbs content is as follows:
Set ws = CreateObject("Wscript.Shell") </li>
<li>ws.run "cmd /c E:xampphtdocssdcapplicationmailtimeSend.bat",vbhide</li>
<li>
Copy code
-
vbs calls timeSend.bat, the content is as follows:
E:xamppphpphp.exe E:xampphtdocssdcapplicationmailmail.php >
-
Open the Task Panel->System and Security->Management Tools->Scheduled Tasks , create a new Basic Task , set the time you want to trigger the trigger, select Start Program- , browse and select your own vbs program, Click Done.
In this way, the program can send emails at the specified time.
-
|