日期:2010-11-06 08:48:00 来源:本站整理
php mail 发送邮件代码[网站编程]
本文“php mail 发送邮件代码[网站编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
mail 发送邮件代码
本款php发送邮件代码他操纵了php安闲的邮件发送函数mail举行邮件发送的,他会操纵二种试一是判断mail函数能否可用,假如行就操纵它来发送不然fsockopen来操作.
- */
- class email
- {
- function email()
- {
- $this->__construct();
- }
- function __construct()
- {
- @define('charset','gbk');
- $this->set(mail_server, mail_port, mail_user, mail_pwd,mail_type);
- $this->auth = 1;
- }
- function set($server, $port, $user, $password, $type = 1, $delimiter = 1, $mailusername = 0)
- {
- $this->type = $type;
- $this->server = $server;
- $this->port = $port;
- $this->user = $user;
- $this->password = $password;
- $this->delimiter = $delimiter == 1 ? "rn" : ($delimiter == 2 ? "r" : "n");
- $this->mailusername = $mailusername;
- }
- function send($email_to, $email_subject, $email_message, $email_from = '')
- {
- global $dircms;
- $email_to=iconv("utf-8", "gbk", $email_to);
- $email_subject=iconv("utf-8", "gbk", $email_subject);
- $email_message=iconv("utf-8", "gbk", $email_message);
- $email_subject = '=?'.charset.'?b?'.base64_encode(str_replace("r", '', $email_subject)).'?=';
- $email_message = str_replace("rn.", " rn..", str_replace("n", "rn", str_replace("r", "n", str_replace("rn", "n", str_replace("nr", "r", $email_message)))));
- $adminemail = $this->type == 1 ? $dircms['mail_user'] : $dircms['mail_user'];
- $email_from = $email_from == '' ? '=?'.charset.'?b?'.base64_encode($dircms['site_name'])."?= <$adminemail>" : (preg_match('/^(.+?) <(.+?)>$/',$email_from, $from) ? '=?'.charset.'?b?'.base64_encode($from[1])."?= <$from[2]>" : $email_from);
- $emails = explode(',', $email_to);
- foreach($emails as $touser)
- {
- $tousers[] = preg_match('/^(.+?) <(.+?)>$/',$touser, $to) ? ($this->mailusername ? '=?'.charset.'?b?'.base64_encode($to[1])."?= <$to[2]>" : $to[2]) : $touser;
- }
- $email_to = implode(',', $tousers);
- $headers = "from: $email_from{$this->delimiter}x-priority: 3{$this->delimiter}x-mailer: dircms {$this->delimiter}mime-version: 1.0{$this->delimiter}content-type: text/html; charset=".charset."{$this->delimiter}";
- if($this->type == 1)
- {
- return $this->smtp($email_to, $email_subject, $email_message, $email_from, $headers);
- }
- elseif($this->type == 2)
- {
- return @mail($email_to, $email_subject, $email_message, $headers);
- }
- else
- {
- ini_set('smtp', $this->server);
- ini_set('smtp_port', $this->port);
- ini_set('sendmail_from', $email_from);
- return @mail($email_to, $email_subject, $email_message, $headers);
- }
- }
- function smtp($email_to, $email_subject, $email_message, $email_from = '', $headers = '')
- {
- global $dircms;
- if(!$fp = fsockopen($this->server, $this->port, $errno, $errstr, 10))
- {
- $this->errorlog('smtp', "($this->server:$this->port) connect - unable to connect to the smtp server", 0);
- return false;
- }
- stream_set_blocking($fp, true);
- $lastmessage = fgets($fp, 512);
- if(substr($lastmessage, 0, 3) != '220')
- {
- $this->errorlog('smtp', "$this->server:$this->port connect - $lastmessage", 0);
- return false;
- }
- fputs($fp, "ehlo dircmsrn");
- $lastmessage = fgets($fp, 512);
- if(substr($lastmessage, 0, 3) != 220 && substr($lastmessage, 0, 3) != 250)
- {
- $this->errorlog('smtp', "($this->server:$this->port) helo/ehlo - $lastmessage", 0);
- return false;
- }
- while(1)
- {
- if(substr($lastmessage, 3, 1) != '-' || emptyempty($lastmessage))
- {
- break;
- }
- $lastmessage = fgets($fp, 512);
- }
- fputs($fp, "auth loginrn");
- $lastmessage = fgets($fp, 512);
- if(substr($lastmessage, 0, 3) != 334)
- {
- $this->errorlog('smtp', "($this->server:$this->port) auth login - $lastmessage", 0);
- return false;
- }
- fputs($fp, base64_encode($this->user)."rn");
- $lastmessage = fgets($fp, 512);
- if(substr($lastmessage, 0, 3) != 334)
- {
- $this->errorlog('smtp', "($this->server:$this->port) username - $lastmessage", 0);
- return false;
- }
- fputs($fp, base64_encode($this->password)."rn");
- $lastmessage = fgets($fp, 512);
- if(substr($lastmessage, 0, 3) != 235)
- {
- $this->errorlog('smtp', "($this->server:$this->port) password - $lastmessage", 0);
- return false;
- }
- fputs($fp, "mail from: <".preg_replace("/.*<(.+?)>.*/", "1", $email_from).">rn");
- $lastmessage = fgets($fp, 512);
- if(substr($lastmessage, 0, 3) != 250)
- {
- fputs($fp, "mail from: <".preg_replace("/.*<(.+?)>.*/", "1", $email_from).">rn");
- $lastmessage = fgets($fp, 512);
- if(substr($lastmessage, 0, 3) != 250)
- {
- $this->errorlog('smtp', "($this->server:$this->port) mail from - $lastmessage", 0);
- return false;
- }
- }
- $email_tos = array();
- $emails = explode(',', $email_to);
- foreach($emails as $touser)
- {
- $touser = trim($touser);
- if($touser)
- {
- fputs($fp, "rcpt to: <".preg_replace("/.*<(.+?)>.*/", "1", $touser).">rn");
- $lastmessage = fgets($fp, 512);
- if(substr($lastmessage, 0, 3) != 250)
- {
- fputs($fp, "rcpt to: <".preg_replace("/.*<(.+?)>.*/", "1", $touser).">rn");
- $lastmessage = fgets($fp, 512);
- $this->errorlog('smtp', "($this->server:$this->port) rcpt to - $lastmessage", 0);
- return false;
- }
- }
- }
- fputs($fp, "datarn");
- $lastmessage = fgets($fp, 512);
- if(substr($lastmessage, 0, 3) != 354)
- {
- $this->errorlog('smtp', "($this->server:$this->port) data - $lastmessage", 0);
- }
- $headers .= 'message-id: <'.gmdate('ymdhs').'.'.substr(md5($email_message.microtime()), 0, 6).rand(100000, 999999).'@'.$_server['http_host'].">{$this->delimiter}";
- fputs($fp, "date: ".gmdate('r')."rn");
- fputs($fp, "to: ".$email_to."rn");
- fputs($fp, "subject: ".$email_subject."rn");
- fputs($fp, $headers."rn");
- fputs($fp, "rnrn");
- fputs($fp, "$email_messagern.rn");
- fputs($fp, "quitrn");
- return true;
- }
- function errorlog($type, $message, $is)
- {
- $this->error[] = array($type, $message, $is);
- }
- }
以上是“php mail 发送邮件代码[网站编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |
评论内容只代表网友观点,与本站立场无关!
评论摘要(共 0 条,得分 0 分,平均 0 分)
查看完整评论