m_shige1979のときどきITブログ

プログラムの勉強をしながら学習したことや経験したことをぼそぼそと書いていきます

Github(変なおっさんの顔でるので気をつけてね)

https://github.com/mshige1979

Windows8にXAMPPを配置してgmailをsendmailで送信

結構悩みました

基本的にはメール送信はWindowsからはあまり送信しない方がいいと思っていたのでやらなかったのですが、ちょっと必要になったので調査したら…

いろいろなサイトを巡回してgmailによるサンプルはいろいろあったのですが、その通りにやってもうまくいかなかったので…(;´д`)トホホ…

環境

xampp-win32-5.6.8-0-VC11.7z
stunnel-5.17-installer.exe

で対応

stunnelインストール

まあ、流れに合わせてインストールしていってください

f:id:m_shige1979:20150609230044j:plain

pemファイル?を生成する画面になるので最初の入力のみ「jp」であとはエンターしてください

f:id:m_shige1979:20150609230222j:plain

stunnelインストール終わり

f:id:m_shige1979:20150609230443j:plain

デスクトップにこんなアイコンが出ますので実行する

f:id:m_shige1979:20150609230558j:plain

タスクトレイにも出ます

f:id:m_shige1979:20150609230639j:plain
※ここで設定ファイルのリロードなどを行う

設定

stunnel.conf
cert = stunnel.pem
socket = l:TCP_NODELAY=1
socket = r:TCP_NODELAY=1
key = stunnel.pem

; Sample stunnel configuration file for Win32 by Michal Trojnara 2002-2015
; Some options used here may be inadequate for your particular configuration
; This sample file does *not* represent stunnel.conf defaults
; Please consult the manual for detailed description of available options

; **************************************************************************
; * Global options                                                         *
; **************************************************************************

; Debugging stuff (may be useful for troubleshooting)
;debug = info
;output = stunnel.log

; Enable FIPS 140-2 mode if needed for compliance
;fips = yes

; Microsoft CryptoAPI engine allows for authentication with private keys
; stored in the Windows certificate store
; Each section using this feature also needs the "engineId = capi" option
;engine = capi

; **************************************************************************
; * Service defaults may also be specified in individual service sections  *
; **************************************************************************

; Enable support for the insecure SSLv3 protocol
;options = -NO_SSLv3

; These options provide additional security at some performance degradation
;options = SINGLE_ECDH_USE
;options = SINGLE_DH_USE

; **************************************************************************
; * Service definitions (at least one service has to be defined)           *
; **************************************************************************

; ***************************************** Example TLS client mode services

;[gmail-pop3]
;client = yes
;accept = 127.0.0.1:110
;connect = pop.gmail.com:995
;verify = 2
;CAfile = ca-certs.pem
;checkHost = pop.gmail.com

;[gmail-imap]
;client = yes
;accept = 127.0.0.1:143
;connect = imap.gmail.com:993
;verify = 2
;CAfile = ca-certs.pem
;checkHost = imap.gmail.com

[gmail-smtp]
client = yes
accept = 127.0.0.1:25
connect = smtp.gmail.com:465
verify = 2
CAfile = ca-certs.pem
checkHost = smtp.gmail.com

; Encrypted HTTP proxy authenticated with a client certificate
; located in the Windows certificate store
;[example-proxy]
;client = yes
;accept = 127.0.0.1:8080
;connect = example.com:8443
;engineId = capi

; ***************************************** Example TLS server mode services

;[pop3s]
;accept  = 995
;connect = 110
;cert = stunnel.pem

;[imaps]
;accept  = 993
;connect = 143
;cert = stunnel.pem

[ssmtp]
accept  = 465
connect = 25
;cert = stunnel.pem

; TLS front-end to a web server
;[https]
;accept  = 443
;connect = 80
;cert = stunnel.pem
; "TIMEOUTclose = 0" is a workaround for a design flaw in Microsoft SChannel
; Microsoft implementations do not use TLS close-notify alert and thus they
; are vulnerable to truncation attacks
;TIMEOUTclose = 0

; Remote cmd.exe protected with PSK-authenticated TLS
; Create "secrets.txt" containing IDENTITY:KEY pairs
;[cmd]
;accept = 1337
;exec = c:\windows\system32\cmd.exe
;execArgs = cmd.exe
;PSKsecrets = secrets.txt

; vim:ft=dosini

※一応全部載せとく
※ssmtpとgmail-smtpを使用して後は基本、未使用

php.ini
[mail function]
; XAMPP: Comment out this if you want to work with an SMTP Server like Mercury
SMTP = localhost
smtp_port = 25
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
;sendmail_path = "C:\xampp\mailtodisk\mailtodisk.exe"

sendmailを有効にして、mailtodiskはコメントアウト

sendmail.ini
[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=mail.mydomain.com
smtp_server=localhost

; smtp port (normally 25)

;smtp_port=25
smtp_port=25

; 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=none

; 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=mydomain.com
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=
;auth_password=
auth_username=Gメールのアカウント@gmail.com
auth_password=秘密のパスワード

※こんな感じ

メール送信

testmail.php
<?php
if(mb_send_mail("テストメール@gmail.com", "test_title", "messsage")){
	echo "mail send ok";

}else{
	echo "mail send ng";
}
実行
C:\xampp\php>php testmail.php
sendmail: Error during delivery: <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbs9I
7mYAXd-C0yJSzTbcXpzqlwFgcKBPnFsKNDb9Dis_PwFjSxvKORFnxBlV2OMu2ILXW68Qyj
oD9v7DvtGlirIXQ64II8JOfPSHOa-SFB3dkEu0z-dqVVyanSjg1lzxW7XUvVh5Q_9x1xm4
ktoXydlsC2_moIwBCiZs5NmzLaKkU9rkueyR24S7O83U02Jy0NhnpIuMaq9lhne8aN6P3y
a7_AUD62U0-LW6Tr6s-5DquzsX8s> Please log in via your web browser and
then try again.
Learn more at
https://support.google.com/mail/answer/78754 np9sm5833304pdb.9 - gsmtp

mail send ng
C:\xampp\php>

は?

GMAILアカウントの設定を変更

アカウント情報>ログインとセキュリティより遷移

f:id:m_shige1979:20150609231839j:plain

f:id:m_shige1979:20150609231905j:plain

再度、メール送信
C:\xampp\php>php testmail.php
mail send ok
C:\xampp\php>

うまくいきました

※2段階認証にした場合はアプリパスワードをする必要があるようです。(確認しました)