我在使用 Amazon SES 通过 SMTP 发送电子邮件时遇到问题。我的代码比较简单,看起来像这样:
require_once "Mail.php";
require_once 'Mail/mime.php';
$stmp_info= array (
'host' => 'ssl://email-smtp.us-east-1.amazonaws.com',
'port' => 465,
'auth' => true,
'username' => 'xxxxxx',
'password' => 'xxx'
);
$headers = array (
'From' => 'sender@example.com',
'To' => 'test@example.com',
'Subject' => 'Test Message'
);
$mime = new Mail_mime("\n");
$mime->setTXTBody('Hello World');
$mime->setHTMLBody('<p>Hello World</p>');
$body = $mime->get();
$headers = $mime->headers($headers);
$smtp = Mail::factory('smtp', $stmp_info);
$mail = $smtp->send('test@example.com', $headers, $body);
问题是我总是得到这个错误:
Failed to connect to email-smtp.us-east-1.amazonaws.com:465 [SMTP: Invalid response code received from server (code: -1, response: )]
但这适用于测试连接:
[root@job-server-1 ~]# $ echo quit | nc -v email-smtp.us-east-1.amazonaws.com 25
-bash: $: command not found
Connection to email-smtp.us-east-1.amazonaws.com 25 port [tcp/smtp] succeeded!
220 email-smtp.amazonaws.com ESMTP SimpleEmailService-376766033
但是,如果我 ping 地址,它不起作用。我已经为 tls 和 ssl 尝试了端口 25、465 和 587。奇怪的是它可以在我的本地主机上运行,并且仅当我尝试从我的机架空间实例连接时才会出现此错误。有没有人知道是什么原因造成的?