我用网页运行时正常,能发送,用命令运行php文件时报错,主要代码经检查无误,如下
function sendmail($setEmail, $setName, $body, $subject) {
require_once("PHPMail/phpmailer.php");
$mail = new PHPMailer ( true ); // the true param means it will throw exceptions on errors, which we need to catch
$mail->IsSMTP (); // telling the class to use SMTP
try {
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = SBT_SSL; // sets the prefix to the servier
$mail->Host = SBT_SMTP_HOST; // sets GMAIL as the SMTP server
$mail->Port = SBT_SMTP_PORT; // set the SMTP port for the GMAIL server
$mail->Username = SBT_SMTP_USER; // GMAIL username
$mail->Password = SBT_SMTP_PASS; // GMAIL password
$mail->AddReplyTo ( SBT_MAIL_FROM, SBT_MAIL_FROM_NAME );
$mail->AddAddress ( $setEmail, $setName );
$mail->SetFrom (SBT_SMTP_USER,SBT_SMTP_USER); //sender name
$mail->Subject = $subject;
$mail->MsgHTML ( $body );
if (! $mail->Send ()) {//(这边没报错)
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit ();
}
} catch ( phpmailerException $e ) {
echo $e->errorMessage (); //Pretty error messages from PHPMailer(就是这边输出错误的)
} catch ( Exception $e ) {
echo $e->getMessage (); //Boring error messages from anything else!
}
}
问下是什么原因,目的是为了可以用linux计划任务来执行该PHP文件
function sendmail($setEmail, $setName, $body, $subject) {
require_once("PHPMail/phpmailer.php");
$mail = new PHPMailer ( true ); // the true param means it will throw exceptions on errors, which we need to catch
$mail->IsSMTP (); // telling the class to use SMTP
try {
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = SBT_SSL; // sets the prefix to the servier
$mail->Host = SBT_SMTP_HOST; // sets GMAIL as the SMTP server
$mail->Port = SBT_SMTP_PORT; // set the SMTP port for the GMAIL server
$mail->Username = SBT_SMTP_USER; // GMAIL username
$mail->Password = SBT_SMTP_PASS; // GMAIL password
$mail->AddReplyTo ( SBT_MAIL_FROM, SBT_MAIL_FROM_NAME );
$mail->AddAddress ( $setEmail, $setName );
$mail->SetFrom (SBT_SMTP_USER,SBT_SMTP_USER); //sender name
$mail->Subject = $subject;
$mail->MsgHTML ( $body );
if (! $mail->Send ()) {//(这边没报错)
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit ();
}
} catch ( phpmailerException $e ) {
echo $e->errorMessage (); //Pretty error messages from PHPMailer(就是这边输出错误的)
} catch ( Exception $e ) {
echo $e->getMessage (); //Boring error messages from anything else!
}
}
问下是什么原因,目的是为了可以用linux计划任务来执行该PHP文件
$mail->Port =465
看网上说这个问题是php.ini没配好,我看了下,配的是SMTP,照他们说的改了也没用,关键是我SMTP已经重新赋值,用Gmail邮箱的服务器的啦
; For Win32 only.
SMTP = localhost
smtp_port = 25; For Win32 only.
sendmail_from = [email protected]; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
;sendmail_path = "/usr/sbin/sendmail -t -i"; Force the addition of the specified parameters to be passed as extra parameters
; to the sendmail binary. These parameters will always replace the value of
; the 5th parameter to mail(), even in safe mode.
;mail.force_extra_parameters =
有时候phpcli单独安装的话会自带一个ini文件.
C:\wamp\bin\php\php5.2.6>php --ini
Configuration FILE (php.ini) Path: C:\WINDOWS
Loaded Configuration FILE: C:\wamp\bin\php\php5.2.6\php.ini
Scan FOR additional .ini files IN: (NONE)
Additional .ini files parsed: (NONE)
你现在的主要问题是找找看为什么socket连接不了smtp服务器.
防火墙也有可能.
这个php.ini不是我目前运行的那个PHP.INI
命令:telnet smtp.gmail.com 465 ,确定能连接成功?
那么就使用php -c 命令引入你的正确ini路径
php -c ini路径 php文件
具体可以php -help查看命令参数
php.ini什么问题,请说清楚些,我也遇到这个问题了,linux环境
!
有时候phpcli单独安装的话会自带一个ini文件.
请问php --ini怎么用