我正在尝试发送自动电子邮件,以便从我的网页发送通知。
这是我的代码:
include 'Mail.php';
include 'Mail/mime.php' ;
$text = 'Text version of email';
$html = '<html><body>HTML version of email</body></html>';
$file = 'bg.jpg';
$crlf = "\n";
$hdrs = array(
'From' => 'myuser@myhost.com',
'Subject' => 'Test mime message function'
);
$mime = new Mail_mime($crlf);
$mime->setTXTBody($text);
$mime->setHTMLBody($html);
$mime->addAttachment($file, 'image/jpeg');
$isi = $mime->get();
$hdrs = $mime->headers($hdrs);
$host = "myhost.com";
$port = "587";
$SMTPSecure="tls";
$username = "myuser@myhost.com";
$password = "mypass";
$smtp = Mail::factory('smtp', array('host' => $host,'port' => $port, 'auth' => true, 'debug' => true,
'username' => $username, 'password' => $password, 'SMTPSecure'=>$SMTPSecure));
$mail = $smtp->send('myOtherUser@myhost.com', $hdrs, $isi);
if (PEAR::isError($mail)) {
$msg="<p>error = " . $mail->getMessage() . "</p>";
}
当我在我的localhost( XAMPP for Windows 1.7.3,PHP version 5.3.1 )中测试它时,它的工作正常。 但是在我上传到我的服务器( XAMPP for Linux 1.6.5a,PHP 5.2.5 )后,它将无法正常工作。 这是调试结果:
DEBUG: Recv: 220 SR-HO-MX01.myhost.com Microsoft ESMTP MAIL Service ready at Wed, 4 Mar 2015 15:57:26 +0700
DEBUG: Send: EHLO localhost
DEBUG: Recv: 250-SR-HO-MX01.myhost.com Hello [114.4.68.107]
DEBUG: Recv: 250-SIZE 10485760
DEBUG: Recv: 250-PIPELINING
DEBUG: Recv: 250-DSN
DEBUG: Recv: 250-ENHANCEDSTATUSCODES
DEBUG: Recv: 250-STARTTLS
DEBUG: Recv: 250-AUTH GSSAPI NTLM
DEBUG: Recv: 250-8BITMIME
DEBUG: Recv: 250-BINARYMIME
DEBUG: Recv: 250 CHUNKING
我尝试将端口更改为465并将$SMTPSecure
设置为 ssl ,但仍然无效,并且调试结果相同。
我应该检查什么来修复它?
我的服务器中的OpenSSL支持设置已启用。
这是我的localhost
的调试结果DEBUG: Recv: 220 SR-HO-MX02.myhost.com Microsoft ESMTP MAIL Service ready at Wed, 4 Mar 2015 21:26:09 +0700
DEBUG: Send: EHLO localhost
DEBUG: Recv: 250-SR-HO-MX02.myhost.com Hello [223.255.230.52]
DEBUG: Recv: 250-SIZE 20971520
DEBUG: Recv: 250-PIPELINING
DEBUG: Recv: 250-DSN
DEBUG: Recv: 250-ENHANCEDSTATUSCODES
DEBUG: Recv: 250-STARTTLS
DEBUG: Recv: 250-AUTH GSSAPI NTLM
DEBUG: Recv: 250-8BITMIME
DEBUG: Recv: 250-BINARYMIME
DEBUG: Recv: 250 CHUNKING
DEBUG: Send: STARTTLS
DEBUG: Recv: 220 2.0.0 SMTP server ready
DEBUG: Send: EHLO localhost
DEBUG: Recv: 250-SR-HO-MX02.myhost.com Hello [223.255.230.52]
DEBUG: Recv: 250-SIZE 20971520
DEBUG: Recv: 250-PIPELINING
DEBUG: Recv: 250-DSN
DEBUG: Recv: 250-ENHANCEDSTATUSCODES
DEBUG: Recv: 250-AUTH GSSAPI NTLM LOGIN
DEBUG: Recv: 250-8BITMIME
DEBUG: Recv: 250-BINARYMIME
DEBUG: Recv: 250 CHUNKING
DEBUG: Send: AUTH LOGIN
DEBUG: Recv: 334 VXNlcm5hbWU6
DEBUG: Send: redacted
DEBUG: Recv: 334 redacted
DEBUG: Send: redacted
DEBUG: Recv: 235 2.7.0 Authentication successful
DEBUG: Send: MAIL FROM:<myuser@myhost.com>
DEBUG: Recv: 250 2.1.0 Sender OK
DEBUG: Send: RCPT TO:<myuser@myhost.com>
DEBUG: Recv: 250 2.1.5 Recipient OK
DEBUG: Send: DATA
DEBUG: Recv: 354 Start mail input; end with <CRLF>.<CRLF>
DEBUG: Send: MIME-Version: 1.0
From: myuser@myhost.com
Subject: Test mime message function
Content-Type: multipart/mixed;
boundary="=_b2d99c906044c6425cf57f0c2c27b840"
--=_b2d99c906044c6425cf57f0c2c27b840
Content-Type: multipart/alternative;
boundary="=_ec04c97876af188028fad4b88c471128"
--=_ec04c97876af188028fad4b88c471128
Content-Transfer-Encoding: 7bit
Content-Type: text/plain; charset="ISO-8859-1"
Text version of email
--=_ec04c97876af188028fad4b88c471128
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html; charset="ISO-8859-1"
<html><body>HTML version of email</body></html>
--=_ec04c97876af188028fad4b88c471128--
--=_b2d99c906044c6425cf57f0c2c27b840
Content-Transfer-Encoding: base64
Content-Type: image/jpeg;
name="bg.jpg";
Content-Disposition: attachment;
filename="bg.jpg";
/9j/4AAQSkZJRgABAgAAZABkAAD/7AARRHVja3kAAQAEAAAAZAAA/+4ADkFkb2JlAGTAAAAAAf/b
[....]
--
.
dc
DEBUG: Recv: 250 2.6.0 <98090985-136c-4479-9cdc-63b43c6f5042@SR-HO-MX01.sariroti.com> [InternalId=47017006989399] Queued mail for delivery
DEBUG: Send: QUIT
DEBUG: Recv: 221 2.0.0 Service closing transmission channel
更新: 我试图从我的服务器使用gmail用户发送电子邮件,但它确实有效。所以我认为问题在于我的主机和端口。但我怎么检查呢?
答案 0 :(得分:0)
Net/SMTP.php
仅在您使用PHP 5.1.0+(您是)并且加载了openssl扩展程序时才支持STARTTLS:
if ($tls && version_compare(PHP_VERSION, '5.1.0', '>=') &&
extension_loaded('openssl') && isset($this->_esmtp['STARTTLS']) &&
strncasecmp($this->host, 'ssl://', 6) !== 0) {
您说SSL已启用,但请在您的服务器上运行包含以下内容的脚本:
<?php var_dump(extension_loaded('openssl')); ?>
以与开始邮件发送相同的方式使用它。它应该输出boolean(true)
。
答案 1 :(得分:0)
所以,最后我使用PHPMailer改变了我的方法,一切都可以正常工作..虽然还不知道PEAR的问题..