我使用以下代码发送电子邮件。
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'smtp.sendgrid.net',
'smtp_user' => 'username',
'smtp_pass' => 'password',
'smtp_port' => 587,
'crlf' => "\r\n",
'newline' => "\r\n",
'mailtype' => "html"
);
$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->from($email);
$this->email->to($to);
$this->email->subject($subject);
$this->email->message($message);
此代码在我的localhost中正常工作,但每次从cPanel运行此代码时都会收到错误消息。
错误:
A PHP Error was encountered
Severity: Warning
Message: fsockopen() [function.fsockopen]: unable to connect to smtp.sendgrid.net:587 (Connection timed out)
Filename: libraries/Email.php
Line Number: 1689
A PHP Error was encountered
Severity: Warning
Message: fwrite(): supplied argument is not a valid stream resource
Filename: libraries/Email.php
Line Number: 1846
A PHP Error was encountered
Severity: Warning
Message: fgets(): supplied argument is not a valid stream resource
Filename: libraries/Email.php
Line Number: 1869
A PHP Error was encountered
Severity: Warning
Message: fwrite(): supplied argument is not a valid stream resource
Filename: libraries/Email.php
Line Number: 1846
A PHP Error was encountered
Severity: Warning
Message: fgets(): supplied argument is not a valid stream resource
Filename: libraries/Email.php
Line Number: 1869
A PHP Error was encountered
Severity: Warning
Message: fwrite(): supplied argument is not a valid stream resource
Filename: libraries/Email.php
Line Number: 1846
A PHP Error was encountered
Severity: Warning
Message: fgets(): supplied argument is not a valid stream resource
Filename: libraries/Email.php
Line Number: 1869
A PHP Error was encountered
Severity: Warning
Message: fwrite(): supplied argument is not a valid stream resource
Filename: libraries/Email.php
Line Number: 1846
A PHP Error was encountered
Severity: Warning
Message: fgets(): supplied argument is not a valid stream resource
Filename: libraries/Email.php
Line Number: 1869
A PHP Error was encountered
Severity: Warning
Message: fwrite(): supplied argument is not a valid stream resource
Filename: libraries/Email.php
Line Number: 1846
A PHP Error was encountered
Severity: Warning
Message: fwrite(): supplied argument is not a valid stream resource
Filename: libraries/Email.php
Line Number: 1846
A PHP Error was encountered
Severity: Warning
Message: fgets(): supplied argument is not a valid stream resource
Filename: libraries/Email.php
Line Number: 1869
A PHP Error was encountered
Severity: Warning
Message: Cannot modify header information - headers already sent by (output started at /home3/pinesof1/public_html/flyannapurna.com/system/core/Exceptions.php:185)
Filename: libraries/Session.php
Line Number: 688
A PHP Error was encountered
Severity: Warning
Message: Cannot modify header information - headers already sent by (output started at /home3/pinesof1/public_html/flyannapurna.com/system/core/Exceptions.php:185)
Filename: libraries/Session.php
Line Number: 688
A PHP Error was encountered
Severity: Warning
Message: Cannot modify header information - headers already sent by (output started at /home3/pinesof1/public_html/flyannapurna.com/system/core/Exceptions.php:185)
Filename: libraries/Session.php
Line Number: 688
A PHP Error was encountered
Severity: Warning
Message: Cannot modify header information - headers already sent by (output started at /home3/pinesof1/public_html/flyannapurna.com/system/core/Exceptions.php:185)
Filename: libraries/Session.php
Line Number: 688
A PHP Error was encountered
Severity: Warning
Message: Cannot modify header information - headers already sent by (output started at /home3/pinesof1/public_html/flyannapurna.com/system/core/Exceptions.php:185)
Filename: libraries/Session.php
Line Number: 688
A PHP Error was encountered
Severity: Warning
Message: Cannot modify header information - headers already sent by (output started at /home3/pinesof1/public_html/flyannapurna.com/system/core/Exceptions.php:185)
Filename: helpers/url_helper.php
Line Number: 542
谁能告诉我应该怎么做?
答案 0 :(得分:0)
尝试将最长执行时间设置为更大的数字,并查看它是否适合您。
<?php
ini_set('max_execution_time', 108000);
?>
OR
你可以改变你的php.ini文件
答案 1 :(得分:0)
我不知道你的代码没有用,但当然,这是一个应该每次都能运行的最简单的解决方案,我用它:
$to = "yourTarget@someDomain.com";
$subject = "Your subject";
$message = "<html><body>Your message</body></html>";
$headers = "MIME-Version: 1.0" ."\r\n"
."From: theMailYouWantToUse@domain.com" ."\r\n"
."Content-type:text/html;charset=UTF-8" ."\r\n"
."Reply: reply@yourDomain.com" ."\r\n"
."X-Mailer: PHP/" .phpversion();
$mail_success = mail($to,$subject,$message,$headers);
注意:你需要所有这些标题,否则邮件就会发送到垃圾邮件。
我认为最好有一个包含邮件的文件,而且您经常需要修改一些内容。只需在发送之前使用以下代码:
$message = file_get_contents("file.html");
$message = str_replace("%param1%",$param1,$message);
$message = str_replace("%param2%",$param2,$message);
答案 2 :(得分:0)
第一个错误Message: fsockopen() [function.fsockopen]: unable to connect to smtp.sendgrid.net:587 (Connection timed out)
说了很多关于你的问题。这意味着您无法访问端口587。它可能由以下几个原因发生:
据我所知,584端口在smtp.sendgrid.net以及25端口上为我打开。尝试25端口,看看你是否有同样的问题。如果您仍然看到此错误,请让您的托管公司或系统管理员检查服务器防火墙是否为587端口。您还可以尝试telnet <DOMAIN_NAME> <PORT_NUMBER>
查看是否可以从服务器连接到特定端口,这可能有助于找到解决此错误的原因。