Edw*_*val 4 php sockets email smtp
我编写了下一个自定义PHP函数来通过SMTP邮件服务器发送邮件.
function send($format = 'text'){
$smtpServer = 'mail.mymailserver.com.mx';
$port = '25';
$timeout = '60';
$username = 'myuser';
$password = 'mypassword';
$localhost = 'www.mydomain.com.mx';
$newLine = "\r\n";
$smtpConnect = fsockopen( $smtpServer, $port, $errno, $errstr, $timeout );
fputs( $smtpConnect,'AUTH LOGIN'.$newLine );
fputs( $smtpConnect, base64_encode( $username ) . $newLine );
fputs( $smtpConnect, base64_encode( $password ) . $newLine );
fputs( $smtpConnect, 'HELO ' . $localhost . $newLine );
fputs( $smtpConnect, 'MAIL FROM: ' . $this->from . $newLine );
fputs( $smtpConnect, 'RCPT TO: ' . $this->to . $newLine );
if( !empty( $this->cc ) ){
fputs( $smtpConnect, 'RCPT TO: ' . $this->cc . $newLine );
}
if( !empty( $this->bcc ) ){
fputs( $smtpConnect, 'RCPT TO: ' . $this->bcc . $newLine );
}
fputs( $smtpConnect, 'DATA' . $newLine );
fflush( $smtpConnect );
$raw = "";
$raw = @fread( $smtpConnect, 255 ) . "@";
$raw .= @fread( $smtpConnect, 255 );
fputs( $smtpConnect, 'To: ' . $this->to . $newLine );
fputs( $smtpConnect, 'From: <' . $this->from .'>' . $newLine );
fputs( $smtpConnect, 'Subject:' . $this->subject . $newLine );
$format = 'html';
if( $format == 'text' ){
$headers = "Content-Type: text/plain; charset=\"iso-8859-1\"" . "\r\n";
$headers .= "Content-Transfer-Encoding: 7bit" . "\r\n";
$message = $this->bodyText();
fputs( $smtpConnect, $headers . $newLine . $newLine );
fputs( $smtpConnect, $message . $newLine . '.' . $newLine );
}else{
$random_hash = md5(date('r', time()));
$headers = "Content-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"\r\n";
$headers .= "--PHP-alt-" . $random_hash . "\r\n";
$headers .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . "\r\n";
$headers .= "Content-Transfer-Encoding: 7bit" . "\r\n";
$message = $this->bodyText();
fputs( $smtpConnect, $headers . $newLine );
fputs( $smtpConnect, $message . $newLine );
$headers = "--PHP-alt-" . $random_hash . "\r\n";
$headers .= "Content-Type: text/html; charset=\"iso-8859-1\"" . "\r\n";
$headers .= "Content-Transfer-Encoding: 7bit" . "\r\n";
$message = $this->bodyHtml();
fputs( $smtpConnect, $headers . $newLine );
fputs( $smtpConnect, $message . $newLine );
$headers = "--PHP-alt-" . $random_hash . "--\r\n";
fputs( $smtpConnect, $headers . '.' . $newLine );
}
fputs( $smtpConnect,'QUIT' . $newLine );
return true;
}
Run Code Online (Sandbox Code Playgroud)
该功能运行良好,但在最后几天我收到了nexts php错误:
注意:fputs()[function.fputs]:发送8192个字节失败,错误为errno = 32第165行/cakeapp/trunk/app/controllers/components/email.php中的管道损坏
注意:fputs()[function.fputs]:发送49个字节失败,errno = 32第169行/cakeapp/trunk/app/controllers/components/email.php中的管道损坏
注意:fputs()[function.fputs]:发送6个字节失败,errno = 32在第182行的/cakeapp/trunk/app/controllers/components/email.php中断管
我在谷歌搜索了一些建议,但我找到的信息,谈到了与连接超时相关的问题!
任何人都可以提出解决这个问题的方法吗?
我有同样的问题,并通过将协议设置为'邮件'找到解决方案(当然,这不是总是你需要使用"邮件"作为协议的情况,我在另一个网站使用"smtp"作为协议,并且工作正常,但我为其他网站复制和粘贴的相同代码并没有工作,所以我不得不将其更改为"邮件").我的示例配置如下所示(用您自己的凭据替换大写单词):
'protocol' => 'mail',
'smtp_host' => 'mail.YOUR_WEBSITE_DOMAIN.com.au',
'smtp_port' => 25,
'smtp_user' => 'YOUR_EMAIL_ACCOUNT@YOUR_WEBSITE_DOMAIN.com.au',
'smtp_pass' => 'YOUR_PASSWORD_FOR_YOUR_EMAIL_ACCOUNT',
'smtp_timeout' => 5,// The default value
'mailtype' => 'html' //default: text
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13732 次 |
| 最近记录: |