想要在 Gmail 預定時間寄信, 查查發現沒這個功能, 不過外部倒是有些服務有提供此功能(列在相關網站, 有興趣可以參考看看)~
但是外部服務看到得都是 可以預約寄信、提醒 等等, 只是我需要的是下面功能:
- 確定 時間點 一定要發信
- 準確度 需要確認 0分0秒寄到
外部服務不一定能達到此需求, 於是來簡單寫寫寄信程式.
PHP 使用 PHPMailer 透過 Gmail 預約/排程 寄信
- Source code: https://github.com/tsung/scheduler-sendmail-php
在發信的時候, 有發現幾個小問題:
- Gmail SMTP 從開始寄信到寄出, 會需要 4秒鐘, 可見下述 Log. (Source code 打開 $mail->SMTPDebug = 1; 就可以觀察到)
- 第一封測試
2014-06-15 14:33:58 CLIENT -> SERVER: EHLO www 2014-06-15 14:33:58 CLIENT -> SERVER: AUTH LOGIN 2014-06-15 14:33:58 CLIENT -> SERVER: ooxx== 2014-06-15 14:33:58 CLIENT -> SERVER: ooxx= 2014-06-15 14:33:58 CLIENT -> SERVER: MAIL FROM:<[email protected]> 2014-06-15 14:33:59 CLIENT -> SERVER: RCPT TO:<[email protected]> 2014-06-15 14:33:59 CLIENT -> SERVER: DATA 2014-06-15 14:33:59 CLIENT -> SERVER: Date: Sun, 15 Jun 2014 22:33:57 +0800 2014-06-15 14:33:59 CLIENT -> SERVER: Return-Path: <[email protected]> 2014-06-15 14:33:59 CLIENT -> SERVER: To: =?utf-8?B?to_name?= <[email protected]> 2014-06-15 14:33:59 CLIENT -> SERVER: From: from_name <[email protected]> 2014-06-15 14:33:59 CLIENT -> SERVER: Subject: =?utf-8?B?title==?= 2014-06-15 14:33:59 CLIENT -> SERVER: Message-ID: <a123@www> 2014-06-15 14:33:59 CLIENT -> SERVER: X-Priority: 3 2014-06-15 14:33:59 CLIENT -> SERVER: X-Mailer: PHPMailer 5.2.7 (https://github.com/PHPMailer/PHPMailer/) 2014-06-15 14:33:59 CLIENT -> SERVER: MIME-Version: 1.0 2014-06-15 14:33:59 CLIENT -> SERVER: Content-Type: text/html; charset=utf-8 2014-06-15 14:33:59 CLIENT -> SERVER: Content-Transfer-Encoding: base64 2014-06-15 14:33:59 CLIENT -> SERVER: 2014-06-15 14:33:59 CLIENT -> SERVER: base64content== 2014-06-15 14:33:59 CLIENT -> SERVER: 2014-06-15 14:33:59 CLIENT -> SERVER: . 2014-06-15 14:34:00 CLIENT -> SERVER: QUIT 2014-06-15 22:34:00 mail sent
- 第二封測試
2014-06-15 14:43:59 CLIENT -> SERVER: EHLO www 2014-06-15 14:43:59 CLIENT -> SERVER: AUTH LOGIN 2014-06-15 14:44:00 CLIENT -> SERVER: ooxx== 2014-06-15 14:44:00 CLIENT -> SERVER: ooxx= 2014-06-15 14:44:00 CLIENT -> SERVER: MAIL FROM:<[email protected]> 2014-06-15 14:44:00 CLIENT -> SERVER: RCPT TO:<[email protected]> 2014-06-15 14:44:00 CLIENT -> SERVER: DATA 2014-06-15 14:44:01 CLIENT -> SERVER: Date: Sun, 15 Jun 2014 22:43:59 +0800 2014-06-15 14:44:01 CLIENT -> SERVER: Return-Path: <[email protected]> 2014-06-15 14:44:01 CLIENT -> SERVER: To: =?utf-8?B?to_name?= <[email protected]> 2014-06-15 14:44:01 CLIENT -> SERVER: From: from_name <[email protected]> 2014-06-15 14:44:01 CLIENT -> SERVER: Subject: =?utf-8?B?title==?= 2014-06-15 14:44:01 CLIENT -> SERVER: Message-ID: <a123@www> 2014-06-15 14:44:01 CLIENT -> SERVER: X-Priority: 3 2014-06-15 14:44:01 CLIENT -> SERVER: X-Mailer: PHPMailer 5.2.7 (https://github.com/PHPMailer/PHPMailer/) 2014-06-15 14:44:01 CLIENT -> SERVER: MIME-Version: 1.0 2014-06-15 14:44:01 CLIENT -> SERVER: Content-Type: text/html; charset=utf-8 2014-06-15 14:44:01 CLIENT -> SERVER: Content-Transfer-Encoding: base64 2014-06-15 14:44:01 CLIENT -> SERVER: 2014-06-15 14:44:01 CLIENT -> SERVER: base64content== 2014-06-15 14:44:01 CLIENT -> SERVER: 2014-06-15 14:44:01 CLIENT -> SERVER: . 2014-06-15 14:44:02 CLIENT -> SERVER: QUIT 2014-06-15 22:44:02 mail sent
- 註1: mail sent 是系統時間, CLIENT -> SERVER 是 Gmail 的時間, 所以會有時差.
- 註2: 因為有 4秒差距, 所以程式加上一個判斷由 56秒後才會開始寄信的設定.
- 第一封測試
- 若收件信箱是 Gmail, 59分56秒開始寄出, 確定寄出時間是 0分0秒, 但是 Gmail 收到信的部份還是寫 59分, 這點需要另外注意. (這個問題就得要 0分0秒時寄出, 不可提早寄出)
使用方式
- 抓下 Soruce code
- 決定 define('ON_SECOND', true); // 準時寄出(false)、準時收到(true)
- 準時寄出: 0 0 * * * cd scheduler_sendmail/; ./scheduler_sendmail.php # 設定確定的時間
- 準時收到 59 23 * * * cd scheduler_sendmail/; ./scheduler_sendmail.php # 設定前一分鐘
- 修改信件 define 值(寄件者、收件者.. 等), 信件標題、內容.. 等等.
- 到此就可以寄信測試看看了~