2018/02/27 : PMailServer Version 1.91 フリー版 で送信テストをしました。
host : 192.168.1.16, port : 25
関連する記事
PMailServer Version 1.91 フリー版 を Windows10 にインストールして、Windows7 の PHP よりアクセス
Pear の Net_POP3 で、SSL(995) を使って簡単にメール受信
※ 重要
アプリが正しくても、サーバーでログインを拒否されている場合があります。実際問題、WEBブラウザでログインしに行くと、難読文字画像で認証をされられた事が Hotmail ではありました。Google でも同様の事があると他の記事で読んだ事があるので注意して下さい。
Gmail 側で安全性の低いアプリの許可を『有効』にする必要がありました。
Pear なんで、WEBサーバー(Unix等)でも動くと思いますが、その場合はWEBサーバ用のメールアドレスで sendmail が使えると思います。なので、わざわざ Pear を使う必要は無いですが、フリーの WEBサーバだと、sendmail は使え無いのでその時は試してみる価値はあります。
Windows でPHPからメールを送信するには Pear が必要です。( もう一つの選択肢として、CDO.Message を使う事もできます )
あるいは、fake sendmail for windows を使用できます
(1) php.ini の OpenSSL を有効にする
extension=php_openssl.dll
(2) php.ini の error_reporting に STRICT エラー用の設定を行う
error_reporting = E_ALL & ~E_NOTICE & ~E_STRICT
※ ソースで error_reporting(E_ALL ^ E_NOTICE ^ E_STRICT); でも可
(3) Pear の関係ファイルをダウンロード
➀ Pear
PEAR.php だけでいいです
➁ Mail
Mail フォルダと、Mail.php
➂ Net_SMTP
SMTP.php ( Net ディレクトリ内 )
➃ Net_Socket
Socket.php ( Net ディレクトリ内 )
Net_SMTP の中に以下のようなコメントがあります
* If you have SSL support in PHP, you can connect to a server
* over SSL using an 'ssl://' prefix:
*
* // 465 is a common smtps port.
* $smtp = new Net_SMTP('ssl://mail.host.com', 465);
* $smtp->connect();
send_test.php(UTF-8N で保存)
<?php
error_reporting(E_ALL ^ E_NOTICE ^ E_STRICT);
header( "Content-Type: text/html; Charset=utf-8" );
header( "pragma: no-cache" );
header( "Expires: Wed, 31 May 2000 14:59:58 GMT" );
header( "Cache-control: no-cache" );
require_once("Mail.php");
mb_language("ja");
mb_internal_encoding("UTF-8");
// ***********************************************
// 通信のデバッグ表示
// ***********************************************
$debug = true;
// ***********************************************
// SMTP 接続設定
// ***********************************************
$settings = array(
"host" => "ssl://smtp.gmail.com",
"port" => "465",
"auth" => true,
"username" => "ユーザ名@gmail.com",
"password" => "パスワード",
"debug" => $debug
);
// ***********************************************
// メールアドレス
// ***********************************************
$to_address = "宛先メールアドレス";
$from_address = "ユーザ名@gmail.com";
// ***********************************************
// メールヘッダー
// ***********************************************
$subject = "Gmail(SSL/465)を使ってPHPでメールを送る";
$subject = mb_convert_encoding($subject,"iso-2022-jp");
$to_header = mb_convert_encoding("宛先","iso-2022-jp");
$from_header = mb_convert_encoding("差出人","iso-2022-jp");
mb_internal_encoding("iso-2022-jp");
$subject = mb_encode_mimeheader( $subject );
$to_header = mb_encode_mimeheader( $to_header ) . " <{$to_address}>";
$from_header = mb_encode_mimeheader( $from_header ) . " <{$from_address}>";
mb_internal_encoding("UTF-8");
$headers = array(
"To" => $to_header,
"From" => $from_header,
"Subject" => $subject
);
// ***********************************************
// 本文
// ***********************************************
$body="本文";
$body = mb_convert_encoding($body,"iso-2022-jp");
// ***********************************************
// SMTP 接続設定をオブジェクトに設定
// ***********************************************
$smtp = Mail::factory("smtp", $settings);
// ***********************************************
// 送信
// ***********************************************
print "<pre>";
$result = $smtp->send(
$to_address,
$headers,
$body );
print "</pre>";
if ( PEAR::isError($result) ) {
print "メール送信エラー:" . $result->getMessage();
}
?>
OK
※ Debug フラグを true にしているので、トレースが出力されます
▼ 以下は関数にしたものです
// ***********************************************
// メール送信
// ***********************************************
function send_mail($user,$to,$pass,$message,$subject) {
global $ErrorMessage;
error_reporting(E_ALL ^ E_NOTICE ^ E_STRICT);
// この場合は実行する PHP と同じフォルダに Mail.php があるという前提です
// それ以外の場合は、
// set_include_path( get_include_path() . PATH_SEPARATOR . "Mail.phpのあるフォルダ" );
// を実行します
require_once("Mail.php");
mb_language("ja");
mb_internal_encoding("UTF-8");
// ***********************************************
// 通信のデバッグ表示
// ***********************************************
$debug = false;
// ***********************************************
// SMTP 接続設定
// ***********************************************
$settings = array(
"host" => "ssl://smtp.gmail.com",
"port" => "465",
"auth" => true,
"username" => $user,
"password" => $pass,
"debug" => $debug
);
// ***********************************************
// メールアドレス
// ***********************************************
$to_address = $to;
$from_address = $user;
// ***********************************************
// メールヘッダー
// ***********************************************
$subject = mb_encode_mimeheader( mb_convert_encoding($subject,"iso-2022-jp") );
$to_header = mb_encode_mimeheader( mb_convert_encoding("宛先","iso-2022-jp") ) . " <{$to_address}>";
$from_header = mb_encode_mimeheader( mb_convert_encoding("差出人","iso-2022-jp") ) . " <{$from_address}>";
$headers = array(
"To" => $to_header,
"From" => $from_header,
"Subject" => $subject
);
// ***********************************************
// 本文
// ***********************************************
$body=$message;
$body = mb_convert_encoding($body,"iso-2022-jp");
// ***********************************************
// SMTP 接続設定をオブジェクトに設定
// ***********************************************
$smtp = Mail::factory("smtp", $settings);
// ***********************************************
// 送信
// ***********************************************
print "<pre>";
$result = $smtp->send(
$to_address,
$headers,
$body );
print "</pre>";
if ( PEAR::isError($result) ) {
// print "メール送信エラー:" . $result->getMessage();
$ErrorMessage = "メール送信エラー:" . $result->getMessage();
}
}
▼ PMailServer Version 1.91 フリー版 でのログ
DEBUG: Recv: 220 PMailServer Version 1.91 �t���[�� ESMTP PMailServer [Free Edition] 1.91; Tue, 27 Feb 2018 17:36:11
DEBUG: Send: EHLO localhost
DEBUG: Recv: 250-ENHANCEDSTATUSCODES
DEBUG: Recv: 250-PIPELINING
DEBUG: Recv: 250-8BITMIME
DEBUG: Recv: 250-AUTH PLAIN LOGIN CRAM-MD5
DEBUG: Recv: 250-AUTH=PLAIN LOGIN CRAM-MD5
DEBUG: Recv: 250-HELP
DEBUG: Recv: 250 OK
DEBUG: Send: AUTH LOGIN
DEBUG: Recv: 334 VXNlcm5hbWU6
DEBUG: Send: bGlnaHRib3g=
DEBUG: Recv: 334 UGFzc3dvcmQ6
DEBUG: Send: cGFzc3dvcmQ=
DEBUG: Recv: 235 2.0.0 Authentication success.
DEBUG: Send: MAIL FROM:
DEBUG: Recv: 250 2.1.0 ... Sender ok
DEBUG: Send: RCPT TO:
DEBUG: Recv: 250 2.1.5 ... Recipient ok
DEBUG: Send: DATA
DEBUG: Recv: 354 Enter mail, end with "." on a line by itself (1)
DEBUG: Send: To: =?ISO-2022-JP?B?GyRCMDhAaBsoQg==?=
From: =?ISO-2022-JP?B?GyRCOjk9UD9NGyhC?=
Subject: PMailServer Version 1.91 =?ISO-2022-JP?B?GyRCJVUlaiE8SEcbKEIgGyRCJEsbKEIg?=
=?ISO-2022-JP?B?UEhQIBskQiRHQXc/LhsoQg==?=
DEBUG: Send: $BK\J8(B
DEBUG: Send:
.
DEBUG: Recv: 250 2.0.0 yx1S2MGvv2CB7brTdl1Af5fC40k03OJg Message accepted for delivery
DEBUG: Send: QUIT
DEBUG: Recv: 221 2.0.0 PMailServer Version 1.91 �t���[�� closing connection
OK
※ 化けてるのは、PMailServer の登録時に サーバー名に日本語を使用したからです。