Email Smtp邮件发送错误
我正在尝试从本地主机(wamp)通过smtp服务器(gmail)发送邮件。运行该程序时,它将显示致命错误:超过了最长30秒的执行时间。。我更改了超时增益,它将产生相同的错误Email Smtp邮件发送错误,email,php,Email,Php,我正在尝试从本地主机(wamp)通过smtp服务器(gmail)发送邮件。运行该程序时,它将显示致命错误:超过了最长30秒的执行时间。。我更改了超时增益,它将产生相同的错误 <?php $to = "email@gmail.com"; $nameto = "Who To"; $from = "from@fast2host.com"; $namefrom = "Who From"; $subject = "Hello World Again!"; $message = "World, Hel
<?php
$to = "email@gmail.com";
$nameto = "Who To";
$from = "from@fast2host.com";
$namefrom = "Who From";
$subject = "Hello World Again!";
$message = "World, Hello!";
authSendEmail($from, $namefrom, $to, $nameto, $subject, $message);
function authSendEmail($from, $namefrom, $to, $nameto, $subject, $message)
{
//SMTP + SERVER DETAILS
/* * * * CONFIGURATION START * * * */
$smtpServer = "smtp.gmail.com";
$port = "25";
$timeout = "30";
$username = "username";
$password = "password";
$localhost = "27.107.106.163";
$newLine = "\r\n";
/* * * * CONFIGURATION END * * * * */
//Connect to the host on the specified port
$smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);
$smtpResponse = fgets($smtpConnect, 515);
if(empty($smtpConnect))
{
$output = "Failed to connect: $smtpResponse";
return $output;
}
else
{
$logArray['connection'] = "Connected: $smtpResponse";
}
//Request Auth Login
fputs($smtpConnect,"AUTH LOGIN" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['authrequest'] = "$smtpResponse";
//Send username
fputs($smtpConnect, base64_encode($username) . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['authusername'] = "$smtpResponse";
//Send password
fputs($smtpConnect, base64_encode($password) . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['authpassword'] = "$smtpResponse";
//Say Hello to SMTP
fputs($smtpConnect, "HELO $localhost" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['heloresponse'] = "$smtpResponse";
//Email From
fputs($smtpConnect, "MAIL FROM: $from" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['mailfromresponse'] = "$smtpResponse";
//Email To
fputs($smtpConnect, "RCPT TO: $to" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['mailtoresponse'] = "$smtpResponse";
//The Email
fputs($smtpConnect, "DATA" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['data1response'] = "$smtpResponse";
//Construct Headers
$headers = "MIME-Version: 1.0" . $newLine;
$headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;
$headers .= "To: $nameto <$to>" . $newLine;
$headers .= "From: $namefrom <$from>" . $newLine;
fputs($smtpConnect, "To: $to\nFrom: $from\nSubject: $subject\n$headers\n\n$message \n.\n");
$smtpResponse = fgets($smtpConnect, 515);
$logArray['data2response'] = "$smtpResponse";
// Say Bye to SMTP
fputs($smtpConnect,"QUIT" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$logArray['quitresponse'] = "$smtpResponse";
}
?>
请帮助我调试错误您有太多冗余代码,这是您应该做的:
<?php
require_once 'Mail.php';
require_once 'Mail/mime.php';
// set all of the parameters
$to = "Who To <email@gmail.com>";
$from = "Who From <from@fast2host.com>";
$subject = "Hello World Again!";
$message = "World, Hello!";
$text = strip_tags($message);
// create the headers
$headers = array(
'Subject' => $subject,
'From' => $from,
'To' => $to,
'MIME-Version' => '1.0',
'Date' => date('r'),
'Message-ID' => '<'.sha1(microtime(true)).'@mydomain.com>',
'Content-Type' => 'text/html',
'Content-Transfer-Encoding' => 'quoted-printable',
); // end $headers
// create the message
$mime = new Mail_mime("\n");
$mime->setTXTBody($text);
$mime->setHTMLBody($message);
// always call these methods in this order
$body = $mime->get();
$headers = $mime->headers($headers);
// create the smtp mail object
$smtp_params = array(
'host' => 'smtp.gmail.com',
'auth' => true,
'username' => 'myUserName',
'password' => 'myPassWord',
); // end $smtp_params
$smtp = Mail::factory('smtp', $smtp_params);
// send the message
$recipients = array($to);
$mail = $smtp->send($recipients, $headers, $body);
?>
PS.“auth”=>true;在ssl连接的smtp_参数数组标志中。非常明显。。。在PHP超时30秒内,您试图连接到的任何服务器都无法访问,因此脚本被终止。您的内部超时时间为100秒,PHP的超时时间为30秒,因此您永远不会看到自己的错误。SMTP服务器可能很糟糕。如果在
退出后运行fclose
,而不是最后的fgets
,会发生什么情况?您真的打算建立SSL连接吗?我看不到证书设置,只是普通的fsockopen。端口465仅用于SSL上的SMTP;您希望普通SMTP使用25,邮件提交使用587。@konrad:您仍然可以打开ssl端口,而无需执行任何ssl操作。问题是OP实际上没有向服务器发送任何内容,SMTP服务器在您发送之前不会开始说话,因此连接只是闲置的。请说明如何编辑代码。。。