Day2day Opportunism

まぁ、平たく言うと「雑記」がだらだらと・・・

XAMPPのGMail利用

ちょっとローカル環境でテスト用にメールを送信したかったけど、
XAMPPにある「Mercury」をいじるのがメンドクサイ…

ちょっとググったら「GMailを利用して~」ってのが見つかったんで、参考にして
やってみた。
参考ていうか、元ページ
http://d.hatena.ne.jp/sutara_lumpur/20100617/1276728122

環境:
OS:Windows7 64bit
XAMPP:1.7.3
インストール先:C:\Tools\xampp

php.ini (xampp/php/php.ini)の編集
キーワード:mail function で検索(1100行目辺り)

[mail function]
; For Win32 only.
; http://php.net/smtp
;SMTP = localhost            ← コメント化
SMTP = smtp.gmail.com    ← 追加
; http://php.net/smtp-port
;smtp_port = 25                ← コメント化
smtp_port = 587               ← 追加

; For Win32 only.
; http://php.net/sendmail-from
;sendmail_from = postmaster@localhost

; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
;sendmail_path = "\"C:\Tools\xampp\sendmail\sendmail.exe\" -t"
sendmail_path = "\"C:\Tools\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(), even in safe mode.
;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

; Log all mail() calls including the full path of the script, line #, to address and headers
;mail.log = "C:\Tools\xampp\apache\logs\php_mail.log"
mail.log = "C:\Tools\xampp\apache\logs\php_mail.log"  ← コピーしてコメント外し
; 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(), even in safe mode.
;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

; Log all mail() calls including the full path of the script, line #, to address and headers
;mail.log = "C:\Tools\xampp\apache\logs\php_mail.log"
mail.log = "C:\Tools\xampp\apache\logs\php_mail.log"  ← コピーしてコメント外し

sendmail.ini (sendmail/sendmail.ini)の編集
「Set a default account」の前に追加

# gmail
account gmail
tls on
tls_certcheck off
host smtp.gmail.com
port 587
from gmail.account@gmail.com ← gmail.account部分は、自分のアカウント
auth on
user gmail.account                  ← gmail.account部分は、自分のアカウント
password gmailpassword           ← gmailpassword部分は、自分のパスワード

「Set a default account」の内容を変更
# Set a default account
#account default : Mercury      ← コメント化
account default : gmail            ← 追加

apacheの再起動

■送信確認
下記のような内容のphpファイルを作成して動作確認する。

//HTML内容出力
echo "";
echo "";
echo "";
echo "";
echo "";
echo "";

//文字コード設定
mb_language("Japanese");
mb_internal_encoding("SHIFT-JIS");

//*環境設定*************************************
//件名
$subject = "テスト送信";
//管理人メールアドレス(宛先)
$to = "gmail.account@gmail.com";
//**********************************************

//**POSTデータ受け取り**************************
//差出人メールアドレス格納
$from = "gmail.account@gmail.com";
$header   = "From: $from\n";
  $header  .= "Reply-To: $reply_to\n";
  $header  .= "X-Mailer: myphpMail ". phpversion(). "\n";

//本文格納
$body = "メール送信テスト" ."\n";

//************************************************

$sm_code = mb_send_mail($to, $subject, $body, $header);
if($sm_code){
   echo "メール送信成功しました。";
}else{
   echo $sm_code. "メール送信失敗しました。";
}
//HTML内容出力
echo "";
echo "";

コレで出来るはず。

 

XAMPPのバージョンが代わって、ini ファイルの内容が変わったので

下記を見るのが良いかと。

noah-anz.hatenablog.com