您好,欢迎访问一九零五行业门户网

linux下php配置smtp发送邮件的方法

issmtp(); // 设置使用smtp $mail->host = smtp_host; // 设置smtp服务器地址 $mail->smtpauth = true; // 打开smtp权限验证 $mail->username = smtp_mail; // smtp 用户名 $mail->password = smtp_pass; // smtp 服务器密码 $mail->from = service_mail; // 设置发送者地址
$mail->fromname = service_name; // 设置发送者名字 $mail->addaddress($email, $user); // 添加接收者地址 $mail->addreplyto(service_mail, service_name); // 设置回复地址 $mail->wordwrap = 50; // 设置显示格式
$mail->ishtml(true); // 设置邮件支持html $mail->subject = $subject; $mail->body = $body; $mail->altbody = ; // 文本类型的邮件 if(!$mail->send())
{ return $mail->errorinfo; } return true;}//开始发送测试邮件ng: fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: name or service not known in /var/www/xiehui/admin/mail/class.smtp.php on line 89
$tomail = xxxx@126.com;$user = xxxxlinux;$_mailsubject = 邮件测试示例!; // 发给用户的邮件标题小组$_mailbody = 新浪网; // 邮件内容小组sendmail($tomail,$user,$_mailsubject,$_mailbody);?>
复制代码
实验证明yahoo的smtp很好用,号称sina的其实并不好用,我卡在着好长时间。
方法四,给予socket编写的程序使用socket发送邮件的封装类:
smtp=$smtp; if(empty($welcome)) { $this->welcome=gethostbyaddr(localhost); }else $this->welcome=$welcome; $this->debug=$debug; $this->lastmessage=; $this->lastact=; $this->port=25; } //显示调试信息 function show_debug($message, $inout) { if ($this->debug) { if($inout==in){ //响应信息 $m=' }else $m='>> '; if(!ereg(\n$, $message)) $message .=
; $message=nl2br($message); echo ${m}${message}; } } //执行传递的命令 function do_command($command, $code) { $this->lastact=$command; $this->show_debug($this->lastact, out); fputs ( $this->fp, $this->lastact ); $this->lastmessage = fgets ( $this->fp, 512 ); $this->show_debug($this->lastmessage, in); if(!ereg(^$code, $this->lastmessage)) return false; else return true; } //邮件发送处理 function send( $to,$from,$subject,$message) { //连接服务器 $this->lastact=connect; $this->show_debug(连接到smtp 服务器: .$this->smtp, out); $this->fp = fsockopen ( $this->smtp, $this->port ); if ( $this->fp ) { $this->set_socket_blocking( $this->fp, true ); $this->lastmessage=fgets($this->fp,512); $this->show_debug($this->lastmessage, in); if (! ereg ( ^220, $this->lastmessage ) ) { return false; }else{ $this->lastact=helo . $this->welcome . \n; if(!$this->do_command($this->lastact, 250)){ fclose($this->fp); return false; } $this->lastact=mail from: $from . \n; if(!$this->do_command($this->lastact, 250)){ fclose($this->fp); return false; } $this->lastact=rcpt to: $to . \n; if(!$this->do_command($this->lastact, 250)){ fclose($this->fp); return false; } //开始发送邮件正文 $this->lastact=data\n; if(!$this->do_command($this->lastact, 354)){ fclose($this->fp); return false; } //开始处理邮件主题头 $head=subject: $subject\n; if(!empty($subject) && !ereg($head, $message)){ $message = $head.$message; } //开始处理邮件from头 $head=from: $from\n; if(!empty($from) && !ereg($head, $message)) { $message = $head.$message; } //开始处理邮件to头 $head=to: $to\n; if(!empty($to) && !ereg($head, $message)) { $message = $head.$message; } //处理结束串 if(!ereg(\n\.\n, $message)) $message .= \n.\n; $this->show_debug($message, out); fputs($this->fp, $message); $this->lastact=quit\n; if(!$this->do_command($this->lastact, 250)){ fclose($this->fp); return false; } } return true; }else{ $this->show_debug(连接失败!!, in); return false; } }}?>
复制代码
使用socket发送邮件示例:
send(xxxx@126.com, xxxx@126.com, 测试socket邮件, $email)) { echo 发送成功!
;}else{ echo 发送失败!
;}?>
复制代码
其它类似信息

推荐信息