XAMPP:設定 Gmail SMTP 在 XAMPP snedmail 寄信
前言
最近在做一個可以偵測自己網站是否異常的小程式,為了可以讓網頁發送通知給我,所以就開始研究一個會自動發信件的程式。
目前找到最快的方案就是使用PHP自帶的寄信函數 — mail,從官方文件來看,要測試寄信功能這應該是最快的方式。
一般電腦瀏覽器不支援直接執行PHP,如果要執行就要安裝Apache網頁伺服器來執行PHP程式,不過現在有像XAMPP這種整合程式,基本上要架設一個網頁伺服器在電腦測試是很快的。
mail function 的使用
mail函數會根據Apache裡面的php.ini進行主機設定
mail ( string $to ,
string $subject ,
string $message ,
array|string $additional_headers = [] ,
string $additional_params = "" ) : bool
Parameter
- $to: 收件者
- $subject: 信件主旨
- $message: 信件內容
- $additional_headers: 可以加入主機資訊,有些資訊也會抓php.ini設定。
Return Values
成功寄信會回傳true;反之失敗回傳false
Example-1
<?php
// The message
$message = "Line 1\r\nLine 2\r\nLine 3";
// In case any of our lines are larger than 70 characters, we should use wordwrap()
$message = wordwrap($message, 70, "\r\n");
// Send
mail('[email protected]', 'My Subject', $message);
?>
Example-2
<?php
$to = '[email protected]';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: [email protected]' . "\r\n" .
'Reply-To: [email protected]' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
利用內建sendmail來設定gmail的伺服器
連結到gmail SMTP server就可以讓自己不用去弄一個Mail server出來。
需要改兩個設定檔:
- \xampp\php\php.ini
- \xampp\sendmail\sendmail.ini
(一) \xampp\php\php.ini
修改[mail function],要修改兩個地方,分別是:
- sendmail_path:設定sendmail這個應用程式的路徑。
sendmail_path = "\"\xampp\sendmail\sendmail.exe\" -t"
- mail.log:寄送郵件的記錄檔。
mail.log = "\xampp\php\php_mail.log"
完整範例
[mail function]
; For Win32 only.
; http://php.net/smtp
;SMTP=127.0.0.1
; http://php.net/smtp-port
;smtp_port=25
; For Win32 only.
; http://php.net/sendmail-from
;sendmail_from = [email protected]
; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
sendmail_path = "\"\xampp\sendmail\sendmail.exe\" -t"
; Force the addition of the specified parameters to be passed as extra parameters
; to the sendmail binary. These parameters will always replace the value of
; the 5th parameter to mail().
;mail.force_extra_parameters =
; Add X-PHP-Originating-Script: that will include uid of the script followed by the filename
mail.add_x_header=Off
; The path to a log file that will log all mail() calls. Log entries include
; the full path of the script, line number, To address and headers.
;mail.log =
; Log mail to syslog (Event Log on Windows).
mail.log = "\xampp\php\php_mail.log"
(二) \xampp\sendmail\sendmail.ini
修改[sendmail],要修改四個地方,分別是:
- smtp_server:設定gmail SMTP伺服器。
smtp_server=smtp.gmail.com
- smtp_port:設定要連接埠。
smtp_port=465
- default_domain:設定到GOoogle網域。
default_domain=gmail.com
- Google帳號設定
auth_username=你的GOOGLE帳戶名稱@gmail.com
auth_password=你的GOOGLE帳戶密碼 (我有另外申請應用程式密碼)
Note: 好像要去設定一個應用程式用的密碼,用自己的密碼好像會失敗。
完整範例
[sendmail]
; you must change mail.mydomain.com to your smtp server,
; or to IIS's "pickup" directory. (generally C:\Inetpub\mailroot\Pickup)
; emails delivered via IIS's pickup directory cause sendmail to
; run quicker, but you won't get error messages back to the calling
; application.
smtp_server=smtp.gmail.com
; smtp port (normally 25)
smtp_port=465
; SMTPS (SSL) support
; auto = use SSL for port 465, otherwise try to use TLS
; ssl = alway use SSL
; tls = always use TLS
; none = never try to use SSL
smtp_ssl=auto
; the default domain for this server will be read from the registry
; this will be appended to email addresses when one isn't provided
; if you want to override the value in the registry, uncomment and modify
default_domain=gmail.com
; log smtp errors to error.log (defaults to same directory as sendmail.exe)
; uncomment to enable logging
error_logfile=error.log
; create debug log as debug.log (defaults to same directory as sendmail.exe)
; uncomment to enable debugging
;debug_logfile=debug.log
; if your smtp server requires authentication, modify the following two lines
auth_username=你的GOOGLE帳戶名稱@gmail.com
auth_password=你的GOOGLE帳戶密碼 (我有另外申請應用程式密碼)
; if your smtp server uses pop3 before smtp authentication, modify the
; following three lines. do not enable unless it is required.
pop3_server=
pop3_username=
pop3_password=
; force the sender to always be the following email address
; this will only affect the "MAIL FROM" command, it won't modify
; the "From: " header of the message content
force_sender=你的GOOGLE帳戶名稱@gmail.com
; force the sender to always be the following email address
; this will only affect the "RCTP TO" command, it won't modify
; the "To: " header of the message content
force_recipient=
; sendmail will use your hostname and your default_domain in the ehlo/helo
; smtp greeting. you can manually set the ehlo/helo name if required
hostname=