PHP scheduled execution [reprint]

Source: Internet
Author: User

 

We often encounter this problem when sending php mail, that is, users need to regularly send some materials to their mailboxes for subscription. I searched the internet and found that there are not many such articles. This article introduces a method implemented using PHP. I have not used PHP for a long time. Welcome to PP.
1. scheduled transmission is the main solution to the problem.

When writing a program, we need to add a message if at a certain time = a certain time. However, to implement this process, we need to execute this page before sending the message. Therefore, the main problem to be solved is how to make the server regularly execute this page at that time, which seems difficult to implement.

2. I opened the PHP manual, found the PHP Command Line Mode chapter, and found that it can solve this problem. If you want to use this method, first look at this chapter.

Iii. solution:

1. On Windows, you can associate cli \ php.exe with the double-click attribute of the. php file. You can also compile a batch file to execute scripts in PHP. We put the program in a directory, such as E: \ web \ mail. php.

Then write a windows batch processing file with the following content.

@ D: \ php \ cli \ php.exe E: \ web \ mail. php> d: \ php \ cli \ sendmail. log

Pause

The D: \ php \ cli \ php.exe is the directory where my PHP installation file is located. Php.exe is a program in windows PHP Command Line Mode.

Okay. Save the file as mail. bat and then add a task to the scheduled task in windows so that the operating system can run the batch file at a certain time.

2. If you use a Unix system, you need to add a special line of code at the beginning of your PHP script so that it can be executed, in this way, the system will know what program to use to run the script. The first line of code added to the Unix system does not affect the running of the script in Windows. Therefore, you can compile a cross-platform script program using this method. The following is an example of a simple PHP Command Line program.

Iv. Example:

E: \ web \ Timesend. php

#! /Usr/bin/php

<? Php

Require_once ("E: \ web \ shortdes \ config. php ");

Require_once ("E: \ web \ shortdes \ class \ mail. class. php ");

Require_once ("E: \ web \ shortdes \ class \ smtp. class. php ");

// + ------------------------------------------------------ +

// Database Configuration

$ Dbhost = "localhost ";

$ Dbport = "3306 ";

$ Dbname = "";

$ Dbuser = "";

$ Dbpawd = "";

// + ------------------------------------------------------ +

// Database connection object

$ Db = new dbLink ($ dbhost, $ dbport, $ dbuser, $ dbpawd, $ dbname );

$ Query = "SELECT * FROM wl_mailtemplate WHERE mt_name = 'userupdate '";

$ Mailtemplate = $ db-> dbQuery ($ query );

$ Username = 'sdfsdfdsd ';

$ Sex = "Mr ";

$ Accounts = "sdfasdfasdfsad ";

$ Password = "sdfsadfsdasdasddssfds ";

$ Message ="

". $ Mailtemplate [0] ['mt _ message']."

";

$ Message = addslashes ($ message );

Eval ("\ $ message = \" $ message \";");

$ Mail = new SendMail ('wfits @ 126.com ', $ mailtemplate [0] ['mt _ subject'], nl2br ($ message ));

If ($ mail-> Send ())

{

$ Feedback = "the modification confirmation information has been sent to your registered Email and the current logon has been canceled. \ N check the confirmation letter and obtain a new logon password .";

Echo $ feedback;

}

?>

Write a bat file.

@ D: \ php \ cli \ php.exe E: \ web \ mail. php> d: \ php \ cli \ sendmail. log

Pause

Save timesend. bat in the @ D: \ php \ cli \ php.exe directory.

Add a scheduled task to the window!

5. description.

1. I use a template to store emails in the database, and two other mail sending classes are not provided. If you want to, contact me.

2. Use the absolute path when using requrie_once.

3. The PHP Command Line mode allows the PHP script to run independently of the WEB server. Therefore, it can reduce the burden on the server if a large number of emails are sent.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.