自动发邮件 使用了这个类http://bbs.php100.com/read-htm-tid-121431.html

因他用的php版本较老,用到了函数ereg_replace() 和 ereg()


解决方法

1、对于ereg_replace() 函数,替换为 strtr() 函数

语法不同点如下:

详情http://www.php.net/manual/zh/function.ereg-replace.php

详情http://www.w3school.com.cn/php/func_string_strtr.asp

2、对于ereg()  函数,替换为 preg_match() 函数

链接http://blog.163.com/yanli_xu/blog/static/1363672912011420101820393/

preg_match() 函数语法http://www.php.net/manual/zh/function.preg-match.php

修改完毕后,完整代码如下

【邮件类】sm.class.php

 <?php
 class smtp

 {

 /* Public Variables */

 var $smtp_port;

 var $time_out;

 var $host_name;

 var $log_file;

 var $relay_host;

 var $debug;

 var $auth;

 var $user;

 var $pass;

 /* Private Variables */
 var $sock;

 /* Constractor */

 function smtp($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass)

 {

 $this->debug = FALSE;

 $this->smtp_port = $smtp_port;

 $this->relay_host = $relay_host;

 $this->time_out = 30; //is used in fsockopen()
 #

 $this->auth = $auth;//auth

 $this->user = $user;

 $this->pass = $pass;

 #

 $this->host_name = "localhost"; //is used in HELO command
 $this->log_file = "";

 $this->sock = FALSE;

 }

 /* Main Function */

 function sendmail($to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "")

 {

 $mail_from = $this->get_address($this->strip_comment($from));

 //$body = ereg_replace("(^|(\r\n))(\.)", "\1.\3", $body);
 $body = strtr($body, "(^|(\r\n))(\.)", "\1.\3");
 $header .= "MIME-Version:1.0\r\n";

 if($mailtype=="HTML"){

 $header .= "Content-Type:text/html\r\n";

 }

 $header .= "To: ".$to."\r\n";

 if ($cc != "") {

 $header .= "Cc: ".$cc."\r\n";

 }

 $header .= "From: $from<".$from.">\r\n";

 $header .= "Subject: ".$subject."\r\n";

 $header .= $additional_headers;

 $header .= "Date: ".date("r")."\r\n";

 $header .= "X-Mailer:By Redhat (PHP/".phpversion().")\r\n";

 list($msec, $sec) = explode(" ", microtime());

 $header .= "Message-ID: <".date("YmdHis", $sec).".".($msec*1000000).".".$mail_from.">\r\n";

 $TO = explode(",", $this->strip_comment($to));

 if ($cc != "") {

 $TO = array_merge($TO, explode(",", $this->strip_comment($cc)));

 }

 if ($bcc != "") {

 $TO = array_merge($TO, explode(",", $this->strip_comment($bcc)));

 }

 $sent = TRUE;

 foreach ($TO as $rcpt_to) {

 $rcpt_to = $this->get_address($rcpt_to);

 if (!$this->smtp_sockopen($rcpt_to)) {

 $this->log_write("Error: Cannot send email to ".$rcpt_to."\n");

 $sent = FALSE;

 continue;

 }

 if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) {

 $this->log_write("E-mail has been sent to <".$rcpt_to.">\n");

 } else {

 $this->log_write("Error: Cannot send email to <".$rcpt_to.">\n");

 $sent = FALSE;

 }

 fclose($this->sock);

 $this->log_write("Disconnected from remote host\n");

 }

 return $sent;

 }

 /* Private Functions */

 function smtp_send($helo, $from, $to, $header, $body = "")

 {

 if (!$this->smtp_putcmd("HELO", $helo)) {

 return $this->smtp_error("sending HELO command");

 }

 #auth

 if($this->auth){

 if (!$this->smtp_putcmd("AUTH LOGIN", base64_encode($this->user))) {

 return $this->smtp_error("sending HELO command");

 }

 if (!$this->smtp_putcmd("", base64_encode($this->pass))) {

 return $this->smtp_error("sending HELO command");

 }

 }

 #

 if (!$this->smtp_putcmd("MAIL", "FROM:<".$from.">")) {

 return $this->smtp_error("sending MAIL FROM command");

 }

 if (!$this->smtp_putcmd("RCPT", "TO:<".$to.">")) {

 return $this->smtp_error("sending RCPT TO command");

 }

 if (!$this->smtp_putcmd("DATA")) {

 return $this->smtp_error("sending DATA command");

 }

 if (!$this->smtp_message($header, $body)) {

 return $this->smtp_error("sending message");

 }

 if (!$this->smtp_eom()) {

 return $this->smtp_error("sending <CR><LF>.<CR><LF> [EOM]");

 }

 if (!$this->smtp_putcmd("QUIT")) {

 return $this->smtp_error("sending QUIT command");

 }

 return TRUE;

 }

 function smtp_sockopen($address)

 {

 if ($this->relay_host == "") {

 return $this->smtp_sockopen_mx($address);

 } else {

 return $this->smtp_sockopen_relay();

 }

 }

 function smtp_sockopen_relay()

 {

 $this->log_write("Trying to ".$this->relay_host.":".$this->smtp_port."\n");

 $this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);

 if (!($this->sock && $this->smtp_ok())) {

 $this->log_write("Error: Cannot connenct to relay host ".$this->relay_host."\n");

 $this->log_write("Error: ".$errstr." (".$errno.")\n");

 return FALSE;

 }

 $this->log_write("Connected to relay host ".$this->relay_host."\n");

 return TRUE;;

 }

 function smtp_sockopen_mx($address)

 {

 $domain = strtr($address, "^.+@([^@]+)$", "\1");

 if (!@getmxrr($domain, $MXHOSTS)) {

 $this->log_write("Error: Cannot resolve MX \"".$domain."\"\n");

 return FALSE;

 }

 foreach ($MXHOSTS as $host) {

 $this->log_write("Trying to ".$host.":".$this->smtp_port."\n");

 $this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);

 if (!($this->sock && $this->smtp_ok())) {

 $this->log_write("Warning: Cannot connect to mx host ".$host."\n");

 $this->log_write("Error: ".$errstr." (".$errno.")\n");

 continue;

 }

 $this->log_write("Connected to mx host ".$host."\n");

 return TRUE;

 }

 $this->log_write("Error: Cannot connect to any mx hosts (".implode(", ", $MXHOSTS).")\n");

 return FALSE;

 }

 function smtp_message($header, $body)

 {

 fputs($this->sock, $header."\r\n".$body);

 $this->smtp_debug("> ".str_replace("\r\n", "\n"."> ", $header."\n> ".$body."\n> "));

 return TRUE;

 }

 function smtp_eom()

 {

 fputs($this->sock, "\r\n.\r\n");

 $this->smtp_debug(". [EOM]\n");

 return $this->smtp_ok();

 }

 function smtp_ok()

 {

 $response = str_replace("\r\n", "", fgets($this->sock, 512));

 $this->smtp_debug($response."\n");

 if (!preg_match("/^[23]/", $response)) {

 fputs($this->sock, "QUIT\r\n");

 fgets($this->sock, 512);

 $this->log_write("Error: Remote host returned \"".$response."\"\n");

 return FALSE;

 }

 return TRUE;

 }

 function smtp_putcmd($cmd, $arg = "")

 {

 if ($arg != "") {

 if($cmd=="") $cmd = $arg;

 else $cmd = $cmd." ".$arg;

 }

 fputs($this->sock, $cmd."\r\n");

 $this->smtp_debug("> ".$cmd."\n");

 return $this->smtp_ok();

 }

 function smtp_error($string)

 {

 $this->log_write("Error: Error occurred while ".$string.".\n");

 return FALSE;

 }

 function log_write($message)

 {

 $this->smtp_debug($message);

 if ($this->log_file == "") {

 return TRUE;

 }

 $message = date("M d H:i:s ").get_current_user()."[".getmypid()."]: ".$message;

 if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))) {

 $this->smtp_debug("Warning: Cannot open log file \"".$this->log_file."\"\n");

 return FALSE;;

 }

 flock($fp, LOCK_EX);

 fputs($fp, $message);

 fclose($fp);

 return TRUE;

 }

 function strip_comment($address)

 {

 $comment = "/\([^()]*\)/";

 while (preg_match($comment, $address)) {

 $address = strtr($address, $comment, "");

 }

 return $address;

 }

 function get_address($address)

 {

 $address = strtr($address, "([ \t\r\n])+", "");

 $address = strtr($address, "^.*<(.+)>.*$", "\1");

 return $address;

 }

 function smtp_debug($message)

 {

 if ($this->debug) {

 echo $message;

 }

 }

 }

 ?>

sm.class.php

【发邮件】sm.php

 <?php
 /*
 这是一个测试程序!!!
 请按照说明设置好以下的参数
 */
 require("sm.class.php");
 ##########################################
 $smtpserver = "smtp.qq.com";//SMTP服务器
 $smtpserverport =25;//SMTP服务器端口
 $smtpusermail = "你的邮箱";//SMTP服务器的用户邮箱
 $smtpemailto = "1477299439@qq.com";//发送给谁
 $smtpuser = "你的账号";//SMTP服务器的用户帐号(邮箱地址)
 $smtppass = "你的密码";//SMTP服务器的用户密码(邮箱密码)
 $mailsubject = "项目提醒";//邮件主题
 $mailbody = "<h1>有新项目了</h1>";//邮件内容
 $mailtype = "HTML";//邮件格式(HTML/TXT),TXT为文本邮件
 ##########################################
 $smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证.
 $smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype);
 ?>

sm.php

PHP自动发邮件的更多相关文章

  1. python自动发邮件库yagmail

    #### 一般发邮件方法 我以前在通过Python实现自动化邮件功能的时候是这样的: import smtplib from email.mime.text import MIMEText from ...

  2. java windows自动化-mail自动发邮件

    本文旨在让测试人员了解如何发邮件 发邮件的话,最简单的事是直接手动发邮件,但是在自动化测试中,应做到让机器或者代码来自动发送邮件,笔者大概了解以下几种方法,总有一款口味适合你:1java代码来做下面即 ...

  3. jira6.3.6创建问题不自动发邮件通知的问题

    装完jira6.3.6后,设置好邮件服务器,测试没有问题.但是创建问题不自动发邮件提示用户.折腾了大半天,请教了一位大神,终于搞定.步骤是: agile->系统->用户界面->用户缺 ...

  4. redmine创建新闻,自动发邮件给项目组所有成员

    redmine创建新闻,自动发邮件给项目组所有成员: 1.添加用户至公共项目内 2.配置系统邮件推送配置 3.检查用户接受推送配置 3.使用管理员账户发布新闻(不能自己发送自己) 4.查看邮件接受邮件

  5. python自动发邮件库yagmail(转)

    一般发邮件方法 我以前在通过Python实现自动化邮件功能的时候是这样的: import smtplib from email.mime.text import MIMEText from email ...

  6. 【python】python 自动发邮件

    一.一般发邮件的方法 Python对SMTP支持有smtplib和email两个模块,email负责构造邮件,smtplib负责发送邮件. 注意到构造MIMETEXT对象时,第一个参数就是邮件正文,第 ...

  7. Selenium 2自动化测试实战38(整合自动发邮件功能)

    整合自动发邮件功能 解决了前面的问题后,现在就可以将自动发邮件功能集成到自动化测试项目中了.下面重新编辑runtest.py文件 #runtest.py #coding:utf-8 from HTML ...

  8. Selenium 2自动化测试实战37(自动发邮件功能)

    自动发邮件功能 例如,如果想在自动化脚本运行完成之后,邮箱就可以收到最新的测试报告结果.SMTP(Simple Mail Transfer Protocol)是简单邮件传输协议,它是一组用于由源地址到 ...

  9. python连接数据库自动发邮件

    python连接数据库实现自动发邮件 1.运行环境 redhat6 + python3.6 + crontab + Oracle客户端 2.用到的模块  3.操作步骤 (1)安装python3.6参考 ...

随机推荐

  1. 面向对象php基本格式、构造、析构、访问修饰符

    <?php//面向对象//1.类//由众多的对象抽象出来的 //2.对象//一切皆对象//由类实例化出来的 //基本形式class 类名{ 成员变量 成员方法 } $对象名 =new 类名 $对 ...

  2. 10. 星际争霸之php设计模式--原型模式

    题记==============================================================================本php设计模式专辑来源于博客(jymo ...

  3. 《zw版·Halcon-delphi系列原创教程》 只有2行代码的超市收款单ocr脚本

    <zw版·Halcon-delphi系列原创教程> 只有2行代码的超市收款单ocr脚本只有2行代码的超市收款单ocr脚本 发了这么多教程,有网友问,为什么没有ocr的.      的确,在 ...

  4. jsp通过jQuery返回json数据到页面

    1.首先要导入json的包,自己去网站找,总共6个jar包! 2. 在servlet里的写法(仅共参考) JSONObject json = new JSONObject(); String a = ...

  5. Qt5程序参数包含中文GBK编码的问题

    1.背景 Qt5程序(WeekReport.exe)的main函数里有如下代码: //only for test int main(int argc, char *argv[]) { QCoreApp ...

  6. Linux下的PostgreSQL简单安装手册

    1. 安装环境     linux版本: CentOS release 6.2 (Final)     pg版本    : postgresql-9.5.0   2. pg数据库下载地址 --http ...

  7. UltraISO向U盘写入镜像特别慢

    电脑:Dell INSPIRON 1416 系统:WIN7旗舰版32位 U盘:金士顿8G 镜像:CentOS7 ×86_64 问题: 开始使用"写入"功能,写入速度72k/s 后来 ...

  8. jade报错:unexpected token

    背景:项目在执行gulp命令构建的时候报了jade错误,错误位置指向的是一个空白行,而这个空白行的上下文都是一些注释,错误信息显示unexpected token "pipeless-tex ...

  9. Excel公式错误提示啥意思?

    1.#####!返回的结果超出了单元格的宽度:或者单元格的日期时间公式产生了一个负值. 2.#VALUE!使用了错误的参数或运算对象类型. 3.#DIV/O!当公式被零除时产生此错误. 4.#NAME ...

  10. web.xml配置详情 - 简要介绍

    <!--web.xml 元素简介--> <?xml version="1.0" encoding="UTF-8"?><web-ap ...