forked from tsung/scheduler-sendmail-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler_sendmail.php
executable file
·71 lines (59 loc) · 1.83 KB
/
scheduler_sendmail.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/php -q
<?php
include('PHPMailer/class.phpmailer.php');
include('PHPMailer/class.smtp.php');
include('PHPMailer/language/phpmailer.lang-zh.php');
define('ON_SECOND', true); // 準時寄出(false)、準時收到(true)
define('SMTP_USERNAME', '[email protected]');
define('SMTP_PASSWORD', 'email_password');
define('FROM_NAME', 'username');
define('FROM_MAIL', '[email protected]');
define('TO_MAIL', '[email protected]');
define('TO_NAME', 'sendto_username');
mb_internal_encoding('UTF-8');
$mail = new PHPMailer();
if (!is_object($mail) || !$mail) {
echo "Mail initial error\n";
exit;
}
$mail->IsSMTP();
// $mail->SMTPDebug = 1;
$mail->Port = 465; // 465 or 578
$mail->SMTPSecure = 'ssl'; // ssl or tls
$mail->Host = 'smtp.gmail.com'; // ssl://smtp.gmail.com
$mail->Encoding = 'base64';
$mail->CharSet = 'utf-8';
$mail->SMTPAuth = true;
$mail->Username = SMTP_USERNAME;
$mail->Password = SMTP_PASSWORD;
$mail->WordWrap = 70;
$mail->From = FROM_MAIL;
$mail->FromName = FROM_NAME;
$mail->AddAddress(TO_MAIL, TO_NAME);
// $mail->AddAddress(TO_MAIL2, TO_NAME2); // add more email
// $mail->AddAttachment('path/filename'); // Attachment
$mail->IsHTML(true);
$mail->Subject = '標題-測試';
$mail->Body = "內容:<br>\r\n測試<br>\r\n<a href=\"http://example.com\">Test</a>Test<br>\r\n";
if (ON_SECOND) {
// crontab 設定需要提前一分鐘
while (1) {
if (date('s') < 56) { // SMTP process need 4 secs
sleep(1);
continue;
}
if ($mail->Send()) {
echo date('Y-m-d H:i:s') . " mail sent\n";
exit;
} else {
echo $mail->ErrorInfo;
exit;
}
}
} else {
if ($mail->Send())
echo date('Y-m-d H:i:s') . " mail sent\n";
else
echo $mail->ErrorInfo;
}
?>