gmail smtp サーバーを使用して、php フォームから php メーラー経由でメールを送信しようとしています。Windows ファイアウォールから 465 ポートを開きましたが、入力したときにポートが cmd 出力に表示されません
netstat -an
command.php ページに表示されるエラー: Invalid address: SMTP Error: Could not connect to SMTP host. 検索の結果、問題の正確な解決策がありません。私のコードは次のとおりです。
<?php
require_once('PHPMailer_v5.1/class.phpmailer.php');
define('GUSER', 'from@gmail.com'); // GMail username
define('GPWD', 'password'); // GMail password
function smtpmailer($to, $from, $from_name, $subject, $body) {
global $error;
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 0; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username = GUSER;
$mail->Password = GPWD;
$mail->SetFrom($from, $from_name);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
return false;
} else {
$error = 'Message sent!';
return true;
}}
if(smtpmailer('from@gmail.com', 'to@gmail.com', 'name', 'test mail message', 'Hello World!')){
echo "sent";
if (!empty($error)) echo $error;}
?>
注: サイトに localhost を使用しています