自动发送邮件的代码(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("(^|( ))(\.)", "\1.\3", $body);
$header .= "mime-version:1.0 ";
if($mailtype=="html"){
$header .= "content-type:text/html ";
}
$header .= "to: ".$to." ";
if ($cc != "") {
$header .= "cc: ".$cc." ";
}
$header .= "from: $from<".$from."> ";
$header .= "subject: ".$subject." ";
$header .= $additional_headers;
$header .= "date: ".date("r")." ";
$header .= "x-mailer:by redhat (php/".phpversion().") ";
list($msec, $sec) = explode(" ", microtime());
$header .= "message-id: <".date("ymdhis", $sec).".".($msec*1000000).".".$mail_from."> ";
$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." ");
$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."> ");
} else {
$this->log_write("error: cannot send email to <".$rcpt_to."> ");
$sent = false;
}
fclose($this->sock);
$this->log_write("disconnected from remote host ");
}
echo "<br>";
//echo $header;
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." ");
$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." ");
$this->log_write("error: ".$errstr." (".$errno.") ");
return false;
}
$this->log_write("connected to relay host ".$this->relay_host." ");
return true;;
}

function smtp_sockopen_mx($address)
{
$domain = ereg_replace("^.+@([^@]+)$", "\1", $address);
if (!@getmxrr($domain, $mxhosts)) {
$this->log_write("error: cannot resolve mx "".$domain."" ");
return false;
}
foreach ($mxhosts as $host) {
$this->log_write("trying to ".$host.":".$this->smtp_port." ");
$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." ");
$this->log_write("error: ".$errstr." (".$errno.") ");
continue;
}
$this->log_write("connected to mx host ".$host." ");
return true;
}
$this->log_write("error: cannot connect to any mx hosts (".implode(", ", $mxhosts).") ");
return false;
}

function smtp_message($header, $body)
{
fputs($this->sock, $header." ".$body);
$this->smtp_debug("> ".str_replace(" ", " "."> ", $header." > ".$body." > "));

return true;
}

function smtp_eom()
{
fputs($this->sock, " . ");
$this->smtp_debug(". [eom] ");

return $this->smtp_ok();
}

function smtp_ok()
{
$response = str_replace(" ", "", fgets($this->sock, 512));
$this->smtp_debug($response." ");

if (!ereg("^[23]", $response)) {
fputs($this->sock, "quit ");
fgets($this->sock, 512);
$this->log_write("error: remote host returned "".$response."" ");
return false;
}
return true;
}

function smtp_putcmd($cmd, $arg = "")
{
if ($arg != "") {
if($cmd=="") $cmd = $arg;
else $cmd = $cmd." ".$arg;
}

fputs($this->sock, $cmd." ");
$this->smtp_debug("> ".$cmd." ");

return $this->smtp_ok();
}

function smtp_error($string)
{
$this->log_write("error: error occurred while ".$string.". ");
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."" ");
return false;
}
flock($fp, lock_ex);
fputs($fp, $message);
fclose($fp);

return true;
}

function strip_comment($address)
{
$comment = "\([^()]*\)";
while (ereg($comment, $address)) {
$address = ereg_replace($comment, "", $address);
}

return $address;
}

function get_address($address)
{
$address = ereg_replace("([ ])+", "", $address);
$address = ereg_replace("^.*<(.+)>.*$", "\1", $address);

return $address;
}

function smtp_debug($message)
{
if ($this->debug) {
echo $message."<br>";
}
}

function get_attach_type($image_tag) { //

$filedata = array();

$img_file_con=fopen($image_tag,"r");
unset($image_data);
while ($tem_buffer=addslashes(fread($img_file_con,filesize($image_tag))))
$image_data.=$tem_buffer;
fclose($img_file_con);

$filedata['context'] = $image_data;
$filedata['filename']= basename($image_tag);
$extension=substr($image_tag,strrpos($image_tag,"."),strlen($image_tag)-strrpos($image_tag,"."));
switch($extension){
case ".gif":
$filedata['type'] = "image/gif";
break;
case ".gz":
$filedata['type'] = "application/x-gzip";
break;
case ".htm":
$filedata['type'] = "text/html";
break;
case ".html":
$filedata['type'] = "text/html";
break;
case ".jpg":
$filedata['type'] = "image/jpeg";
break;
case ".tar":
$filedata['type'] = "application/x-tar";
break;
case ".txt":
$filedata['type'] = "text/plain";
break;
case ".zip":
$filedata['type'] = "application/zip";
break;
default:
$filedata['type'] = "application/octet-stream";
break;
}

return $filedata;
}

}

//使用方法

 代码如下 复制代码
$smtps教程erver = "smtp.163.com";//smtp服务器
$smtpserverport =25;//smtp服务器端口
$smtpusermail = "请输入自己的网易邮箱账户";//smtp服务器的用户邮箱
$smtpemailto = $rs['email'];//发送给谁
$smtpuser = "请输入自己的网易邮箱账户名";//smtp服务器的用户帐号
$smtppass = "请输入自己的网易邮箱密码";//smtp服务器的用户密码
$mailsubject = "设置新密码";//邮件主题
$mailbody = "{$rs['username']}您好!这里是powered by 您的临时新密码是: $emailpwd" .
   " 请点击下面的网址,进入设置新密码页面,完成新密码的设置。" .
   "<a href='http://www.111cn.net'>设置新密码</a>";//邮件内容
$mailtype = "html";//邮件格式(html/txt),txt为文本邮件
##########################################
$smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证.
$smtp->debug = false;//是否显示发送的调试信息
$smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype);
$prompt_msg->p('系统已经邮件发出,请注意查收!','index.php','返回首页');

?>

时间: 2024-10-23 05:23:40

自动发送邮件的代码(php发送邮件代码)的相关文章

求教:如何在.NET服务器端用代码直接发送邮件???急

问题描述 求教:如何在.NET服务器端用代码直接发送邮件???急 解决方案 解决方案二:System.Net.Mail可以帮到你解决方案三:MailAddressfrom=newMailAddress("jane@contoso.com","Jane"+(char)0xD8+"Clayton",System.Text.Encoding.UTF8);//Setdestinationsforthee-mailmessage.MailAddressto

利用.net代码实现发送邮件_实用技巧

本文实例为大家分享了.net发送邮件的实现代码,供大家参考,具体内容如下 关键代码: 需要引用命名空间: using System.Net.Mail; using System.Net; MailMessage mailObj = new MailMessage(); mailObj.From = new MailAddress("mayun@qq.com"); //发送人邮箱地址 mailObj.To.Add("mahuateng@qq.com"); //收件人邮

php 发送邮件与pop3邮件登录代码

 代码如下 复制代码 function send_msg($to,$subject,$body) { $send_addr = 'admin@test.com';   //发送人地址 $header = "from: admin <".$send_addr."> "; //设置email头 ini_set('sendmail_from',$send_addr); mail($to,$subject,$body,$header); } pop3邮箱登录  

求一个【ASP自动发email】完整的代码!(在线等)

问题描述 求一个[ASP自动发email]完整的代码!可以直接上传空间用的!只改一下发送信箱就OK了!比如:我的email是123456______qq@163密码是:123456需要工作的内容是如:有个123.asp文件我把自动发email的代码放进去然后打开123.asp页面后自动运行这个页面里的自动发email代码.代码运行后1代码.自动获得当前网站域名2代码.自动获得当前网站真实域名路径3代码.自动获得当前IP把以上获得的信息当做email发送到423116954@qq.com信箱内!帮

unity3D基础教程:自动打卡程序 模拟鼠标按键代码

using UnityEngine; using System.Collections; using System.Diagnostics; public class NewBehaviourScript1 : MonoBehaviour { [System.Runtime.InteropServices.DllImport("user32")]//导入user32这个dll文件,其实我们用到的模拟鼠标按键已经封装在这个windows下的文件里了 private static exte

vc++ 6.0 能自动查找自己编写的程序代码的错误吗??怎么设置,回答最好带着张图片

问题描述 vc++ 6.0 能自动查找自己编写的程序代码的错误吗??怎么设置,回答最好带着张图片 解决方案 程序的错误有两种,一个是语法错误,一个是语义错误. 我们用人类语言打比方: 老虎山羊吃了. 这句话语法错误,主语后面接谓语,谓语后面接宾语,不能颠倒. 一年有356天. 这句话是语义错误,在语法上没有问题,但是显然一年不是356天而是365/366天. 编译器只能发现语法错误.在你的图上,双击最下面那个窗格中的错误信息,可以定位到错误的行,你根据提示修改就可以了. 编译器不能发现语义错误,

JS实现先显示大图后自动收起显示小图的广告代码_javascript技巧

本文实例讲述了JS实现先显示大图后自动收起显示小图的广告代码.分享给大家供大家参考.具体如下: 这是一个非常不错的JavaScript图片特效,当最初打开网页的时候,显示的是大图片,就像遮屏广告一样,停留一会后,自动缓慢收起,这时由JS控制更换广告图片,始终显示在网页顶部,在一些大门户网站我们经常见到这种效果,个人感觉挺酷哦! 先来看看运行效果截图: 在线演示地址如下: http://demo.jb51.net/js/2015/js-big-to-small-pic-adv-codes/ 具体代

js带点自动图片轮播幻灯片特效代码分享_javascript技巧

本文实例讲述了javascript带点自动图片轮播幻灯片特效.分享给大家供大家参考.具体如下: 这是一款基于javascript实现带点自动图片轮播幻灯片特效代码,实现过程很简单. 运行效果图:-------------------查看效果 下载源码------------------- 小提示:浏览器中如果不能正常运行,可以尝试切换浏览模式. (1)在head区域引入CSS样式: <link rel="stylesheet" href="css/zzsc.css&qu

php自动注册登录验证机制实现代码_php技巧

背景: 在phpwind站点后台添加一个名为"广告管家"(广告管家为CNZZ的一款广告投放的应用)的应用,整个"广告管家"的应用是通过iframe载入,载入的具体内容根据不同站点显示针对该站点的具体内容,为了提高易用性,有以下的两点要求: 1.首次进入"广告管家"页面自动注册CNZZ账户 2.以后每次进入"广告管家"页面默认为首次注册的CNZZ账户 设计: 1.尝试载入用户帐号和密码信息,此信息保存在数据库或是文本缓存中 2.