サイト管理人Blog

cloudfreeで「さぁ始めましょう♪」

ローカル環境でもsendmailが使えるようにfake sendmailをSetupする

2023-12-16 記載
概要 : ローカル環境でもsendmailが使えるようにfake sendmailをSetupする
Keyword : FakeSendmail, sendmail, メール, windows

sendmailが送れるのは、php.iniの[mail function]に適切な設定が書かれてあるからです。
このセクションの初期値は以下です。

[mail function]
; For Win32 only.
; https://php.net/smtp
SMTP = localhost
; https://php.net/smtp-port
smtp_port = 25

; For Win32 only.
; https://php.net/sendmail-from
;sendmail_from = me@example.com

; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
; https://php.net/sendmail-path
;sendmail_path =

; 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

; Use mixed LF and CRLF line separators to keep compatibility with some
; RFC 2822 non conformant MTA.
mail.mixed_lf_and_crlf = 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 = syslog

UnixとWin32でなにかと違うようです。
ネット情報をみますと、Win32でsendmailを使えるようにする方法はいろいろありますが、一番お手軽なのは FakeSendmail を利用する方法のようです。

ローカル環境はあくまでテストですので送れたらよしとします。
Gitからのダウンロードはこちら。

Fake sendmail for Windows with TLS v1.2
https://github.com/sendmail-tls1-2/main


配置したら、ApacheのCGIを請けている php.iniと
Fake sendmail フォルダ内のsemdmail.iniの設定を適切に書いたら完了。
サーバ用に書いたソース、例えば先日のPHPソースがそのままローカルでも走りました。

mb_language('ja');
mb_internal_encoding('UTF-8');
date_default_timezone_set("Asia/Tokyo");
setlocale(LC_ALL, 'ja_JP.UTF-8');

$to = 'test_dummy@gmail.dummy';
$subject = '日本語が表示されていますか?';
$message = "春はあけぼの。\nやうやう白くなりゆく山ぎは\nすこしあかりて";
$headers = "From: cfsid@svXX.cloudfree.ne.jp";
// メールを送信
$ret = mb_send_mail($to, $subject, $message, $headers);
var_dump($ret);

その設定方法は。
まずphp.ini
sendmail_path = が肝です。ほかは変えてない、というだけです。
パスはダウンロードしたFakeSendmailパッケージを配置したフォルダを設定してください。下記の例は私の場合。
-t -i のオプションはつけとけばよいものだそうです。ここでは説明は省きます。

[mail function]
sendmail_path =c:/pgm/sendmail/FakeSendmailForWindowsWithTLSv_1_2/sendmail.exe -t -i
mail.add_x_header = Off
mail.mixed_lf_and_crlf = Off

semdmail.ini の初期状態はこう。

; configuration for fake sendmail

; if this file doesn't exist, sendmail.exe will look for the settings in
; the registry, under HKLM\Software\Sendmail

[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 port (normally 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=tls

; 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

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

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

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

変更後。
fakesendmail_sender@gmail.sample
はサンプルアドレスです。
error.log は同一フォルダのもの。
gmailは初期でOAuth2.0の認証ですから、以下のようーザー名パスワードでメール送信するには事前に2段階認証の設定をしてアプリパスワードを得ておく必要があります。

[sendmail]
smtp_server=smtp.gmail.com
smtp_port=587
smtp_ssl=auto
error_logfile=error.log
;debug_logfile=debug.log
auth_username=fakesendmail_sender@gmail.sample
auth_password=2dankai_password
pop3_server=
pop3_username=
pop3_password=
force_sender=
force_recipient=
hostname=

auth_username

$headers = “From: fakesendmail_sender@gmail.sample”;
のメールアドレスは一致していないとエラーになるようです。
error.logに
Message is missing sender’s address
と表示されたらそのあたりをご確認ください。

投稿へのコメントは コチラ(掲示板) へお願いします。

サイト管理人Blog