2

申し訳ありませんが、時間どおりに返信するために別の投稿をする時間がありました。

このプロセスの目的は、一定時間後にメールがWebサイトのユーザーに送信されることです。

特定の時間にphpファイルを実行するcronがあります

たとえば、毎週月曜日の午前8時

* 8 ** 1 php-f / $ filepath ()

このphpファイルは何千ものメールを送信しますが、すべてを午前8時から継続的に送信するのではなく、毎分30通のメールを午前8時から送信したいと思います。

あれは:

08:00->0-30メール

08:01->30-60メール

08:02->60-90メール...など

cronの起動は午前8時に1回実行されるため、php sleep関数を使用して数分の間一時停止することを考えましたが、コマンドを尊重しません。システムは失敗し、ロックされます。私のCの経験では、睡眠機能は常に正しく機能します。

発送時に30通のメールを送信し、カウンターでループを終了するように設定しました

 ***** php-f /$filepath ()

そのため、システムに毎分ファイルを実行するように強制します。

私のコード

    $res = $admin->array_mixed(); //Array with all mails address

    $send_per_min=30;
    $send = 0;

    foreach ($res as $r){
    $mails->AddAddress("$r['invitacion_email']");
    .
    .
    .
    $mails = new PHPMailer(true);
$mails->Mailer = "smtp";
    .
    .
    .
    if($mails->Send()){                                      
    $send++;
    $log_OK .= $mail." Send!!! \n";
    }else{                              
    $log_KO .= $mail." Failed!!! \n";
    }


    if ($send == $send_per_min){//With this line I checked that after making 30 shipments out of the loop until the next minute the cron rerun
    //I want to replace it with a sleep that once sent 30 mails, wait until the next minute. In this way, you could set the cron at a specific time
    break;
    }

    }//End for

以前の投稿( https://stackoverflow.com/questions/15393432/sending-emails-with-phpmailer-partitioned )よりも明確になっていることを願っています。

コミュニティへのご挨拶。

Pd-私の悪い英語でごめんなさい

4

1 に答える 1

0

スリープを使用しないで、cron で 30 分ごとに php を呼び出すことができます。必要に応じて、送信するメールの量を PHP に how パラメータを渡すことができます。PHP が実行され、すべてのメールが送信されます (30 件またはデータベースで設定された数量のみ)。cron から新しい呼び出しが実行された後、さらにメールを送信し (30 個またはデータベースで構成された量のみ)、送信するメールがある間、もう一度繰り返します。これには、最後に送信された電子メールをデータベースまたはファイルに書き込むことができます (ID またはその他のコントロールを使用)。電子メールの送信を続行しますが、電子メールの再送信は繰り返しません。送信およびログの設定は、SQLite、XML、テキスト、MySQL などで記述できます。

$res に 30 件の未送信メールを入力しただけです。

唯一の例:

SELECT 
    * 
FROM 
    `my_mail` m
WHERE 
    m.`isSent` = 'notsend'
LIMIT 30;

$res は SQL の戻り値を受け取るか、以前に戻り値をフォーマットすることができ、すべての送信メールはエラーなしで送信されます

$log_OK .= $mail." Send!!! \n";

送信に成功した情報をデータベースに書き込みます。

唯一の例:

UPDATE
    `my_mail`
SET
    `isSent` = 'send'
WHERE 
    id = '$id';

以下の他の解決策:

私はあなたの問題に対する1つの可能な解決策でクラスを作成します.メソッド「getMail」はgetMailの関数であり、この関数を置き換えます.メソッド「sentMail」はPHPMailerによってメールを送信する関数です.メソッド「sentScheduleMail」はからのエンジンです毎分送信しますが、メールの送信が 60 秒より遅い場合、関数は次の最初の 1 分間にのみメールを送信します。

<?php

class SentMailByPart {

    /**
     * SQL to recovery the quantity of mails specified
     * @param int $quantity
     */
    private function getMail($quantity) {
        return array(array("test1"), array("test2"));
    }

    /**
     * Function main to send mail
     */
    public function sentScheduleMail() {
        /**
         * While system is running
         */
        while (1) {
            /**
             * Time in unixtime
             */
            $res = $this->getMail(30);
            foreach ($res as $r) {
                // $this->sentMail($r);
                $this->sentMail($r);
            }
            unset($res, $r);
            $time = mktime();
            sleep($time % 60);
        }
    }

    /**
     * Settings of mail, subject and message (Your function to sent e-mail)
     * @param stdClass $settings
     */
    private function sentMail($r) {
        /**
         * New instance of PHP
         */
        $mails = new PHPMailer(true);
        /**
         * Mail address
         */
        $address = $r['invitacion_email'];
        /**
         * Sent by smtp
         */
        $mails->Mailer = "smtp";
        /**
         * Mail of destination
         */
        $mails->AddAddress($address);

        /**
         * Check mail is sent
         */
        if ($mails->Send()) {
            return true;
        } else {
            /**
             * Write error in error log
             */
            error_log(fprintf("Mail (%s) is not send. Erro: %s"
                            , $address
                            , $mails->ErrorInfo), 0);
            return false;
        }
    }

}

$sent = new SentMailByPart();
$sent->sentScheduleMail();

/**
 * if wish stop the send of mail alter $isRunning = 0, is run
 */
?>

私の英語がとても下手で申し訳ありません。私のイディオムは「pt-br」です。

于 2013-03-14T14:39:58.517 に答える