PHP利用imap收件箱的例子
列出所有目录:
代码如下 | 复制代码 |
$host = '{imap.mail.yahoo.com:993/ssl}'; 结果: Array 重新打开指定的目录: imap_reopen($inbox, $host.'Bulk Mail'); |
windows安装imap
注意在windows中我们需要开启php.ini中的一个imap模板了,在php中找到php_imap.dll扩展然后开启,同时如果你看extensions没有关php_imap.dll需要复制一个过去。
linux中安装imap
最后完整的编译 imap 模块参数如下:
代码如下 | 复制代码 |
./configure --with-php-config=/usr/local/webserver/php/bin/php-config --with-kerberos=/usr --with-imap-ssl=/usr make make install |
在win下,
利用PHP的mail函数来发送邮件
mail()函数的作用:
是连接到邮件服务器
利用smtp协议,与该服务器交互
并投邮件
注意:
1:mail函数不支持esmtp协议,---即,只能直投,不能登陆
2: 由上条,我们只能直投至最终的收件服务器地址.
而该地址,又是在PHP.ini中指定的.
所以我们想用mail()函数往 aseoev@163.com发信的话
我们要---
1: 查询163邮件服务器的地址
2: 把该地址写到php.ini里去
代码如下 | 复制代码 |
SMTP = 163mx02.mxmail.netease.com var_dump(mail('597417106@qq.com','from php mail function','very intresting')); |
但是使用php自带的mail函数发送邮件我们需要在linux中安装一个sendmail组件才可以否则无法使用。
如果你没有这个sendmail组件我们可以使用phpmailer函数来操作
例子
代码如下 | 复制代码 |
<?php require('./PHPMailer/class.phpmailer.php'); $phpmailer = new PHPMailer(); $phpmailer->IsSMTP(); $phpmailer->Host = 'smtp.163.com'; $phpmailer->SMTPAuth = true; $phpmailer->Username = ''; $phpmailer->Password = ''; $phpmailer->CharSet = 'utf-8'; $phpmailer->From = ''; $phpmailer->FromName = ''; $phpmailer->Subject = ''; $phpmailer->Body = ''; $phpmailer->AddAddress('never_kiss@163.com','Aseoe'); echo $phpmailer->send()?'发送成功':'发送失败'; ?> |
上面不带内容,下面看个带内容的
<?php
/**
用PHPMailer类来发信
代码如下 | 复制代码 |
步骤: **/ require('./PHPMailer/class.phpmailer.php'); $phpmailer = new PHPMailer();
$phpmailer->IsSMTP(); // 用smtp协议来发 $phpmailer->Host = 'smtp.163.com'; // 可以发信了 //设置收信人 // 发信 echo $phpmailer->send()?'ok':'fail'; |
补充一个使用上面例子的方法
直接将phpmailer压缩包解压 放到根目录即可运行
直接把文件 放到本地wamp 根目录 ,运行02.php 邮件即可发出(前提 php文件可执行)-(不行的话 在根目录建一个文件夹 重复操作一次)
http://localhost/02.php
我们在使用PHPMailer使用Gmail来发送邮件的连接smtp服务器错误提示:smtp error could not connect to smtp host !了,这个是因为extension=php_openssl.dll未开启导致的哦。使用的PHPMailer版本:5.2.1
以下是PHPMailer的example文件夹里给出的:test_gamil_basic.php的部分代码。
代码如下 | 复制代码 |
$mail = new PHPMailer(); $body = file_get_contents('contents.html'); //$body = $_POST['body']; $body = eregi_replace("[]",'',$body); $mail->IsSMTP(); // telling the class to use SMTP $mail->SMTPDebug = 2; // enables SMTP debug information (for testing) // 1 = errors and messages // 2 = messages only $mail->SMTPAuth = true; // enable SMTP authentication $mail->SMTPSecure = "ssl"; // sets the prefix to the servier $mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server or ssl://smtp.gmail.com $mail->Port = 465; // set the SMTP port for the GMAIL server $mail->Username = "yourusername@gmail.com"; // GMAIL username $mail->Password = "yourpassword"; // GMAIL password $mail->SetFrom('name@yourdomain.com', 'First Last'); $mail->AddReplyTo("name@yourdomain.com","First Last"); $mail->Subject = "PHPMailer Test Subject via smtp (Gmail), basic"; $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test $mail->MsgHTML($body); $address = "whoto@otherdomain.com"; $mail->AddAddress($address, "John Doe"); $mail->AddAttachment("images/phpmailer.gif"); // attachment $mail->AddAttachment("images/phpmailer_mini.gif"); // attachment if(!$mail->Send()) { echo "Mailer Error: " . $mail->ErrorInfo; } else { echo "Message sent!"; } |
按照这个例子给出的代码操作,我遇到了以下错误:
www.111cn.net提示您 smtp error could not connect to smtp host !
的错误提示,google了下,发现是需要开启PHP的openssl扩展:
extension=php_openssl.dll //去掉最前面的分号,重启apache或nginx服务器。
HoHo~成功发送。
smtp发送邮箱个人觉得比php mail函数要实用的多了,mail函数不是随便可以发邮箱的需要进行相关配置哦,下面我们来看一个关于smtp类发送邮箱与问题解决方法。当你还在纠结php内置的mail()函数不能发送邮件时,那么你现在很幸运,此时的这篇文章可以帮助到你!
php利用smtp类来发邮件真是屡试不爽,我用过很久了,基本上没出过问题。本博客后台,当博主回复留言时候,会自动给网友发一封有新回复提示的邮件也是用的本文这个方法实现的。
smtp类发送邮件的方法其实很简单,也很稳定,类是别人已经写好的了,你只需要调用就行了。几行简单的配置就能发邮件,是不是很期待的试一试呢!
email.class.php文件
代码如下 | 复制代码 |
{ /* 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 */ /* 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->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("(^|(rn))(.)", "1.3", $body); $header = "MIME-Version:1.0rn"; if($mailtype=="HTML"){ $header .= "Content-Type:text/htmlrn"; } $header .= "To: ".$to."rn"; if ($cc != "") { $header .= "Cc: ".$cc."rn"; } $header .= "From: $from<".$from.">rn"; $header .= "Subject: ".$subject."rn"; $header .= $additional_headers; $header .= "Date: ".date("r")."rn"; $header .= "X-Mailer:By Redhat (PHP/".phpversion().")rn"; list($msec, $sec) = explode(" ", microtime()); $header .= "Message-ID: <".date("YmdHis", $sec).".".($msec*1000000).".".$mail_from.">rn"; $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 hostn"); } 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 = ereg_replace("^.+@([^@]+)$", "1", $address); 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."rn".$body); $this->smtp_debug("> ".str_replace("rn", "n"."> ", $header."n> ".$body."n> ")); return TRUE; } function smtp_eom() { fputs($this->sock, "rn.rn"); $this->smtp_debug(". [EOM]n"); return $this->smtp_ok(); } function smtp_ok() { $response = str_replace("rn", "", fgets($this->sock, 512)); $this->smtp_debug($response."n"); if (!ereg("^[23]", $response)) { fputs($this->sock, "QUITrn"); 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."rn"); $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);
}
{ $comment = "([^()]*)"; while (ereg($comment, $address)) { $address = ereg_replace($comment, "", $address); }
}
{ $address = ereg_replace("([ trn])+", "", $address); $address = ereg_replace("^.*<(.+)>.*$", "1", $address); return $address; } function smtp_debug($message) { if ($this->debug) { echo $message; } } } ?> |
发送邮箱的php文件
代码如下 | 复制代码 |
<?php require_once "email.class.php"; //******************** 配置信息 ******************************** $smtpserver = "smtp.126.com";//SMTP服务器 $smtpserverport =25;//SMTP服务器端口 $smtpusermail = "new2008oh@126.com";//SMTP服务器的用户邮箱 $smtpemailto = $_POST['toemail'];//发送给谁 $smtpuser = "new2008oh";//SMTP服务器的用户帐号 $smtppass = "您的邮箱密码";//SMTP服务器的用户密码 $mailtitle = $_POST['title'];//邮件主题 $mailcontent = "<h1>".$_POST['content']."</h1>";//邮件内容 $mailtype = "HTML";//邮件格式(HTML/TXT),TXT为文本邮件 //************************ 配置信息 **************************** $smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证. $smtp->debug = false;//是否显示发送的调试信息 $state = $smtp->sendmail($smtpemailto, $smtpusermail, $mailtitle, $mailcontent, $mailtype);
if($state==""){ echo "对不起,邮件发送失败!请检查邮箱填写是否有误。"; echo "<a href='index.html'>点此返回</a>"; exit(); } echo "恭喜!邮件发送成功!!"; echo "<a href='index.html'>点此返回</a>"; echo "</div>"; ?> |
效果截图欣赏:
填写收件人,标题和内容后提交
返回成功的提示
瞬间便接收到了邮件
smtp类无法发送邮件解决方法
偶然发现我网站后台自动发送邮件功能不能用了,报这个错误:
Trying to smtp.126.com:25 Error: Cannot connenct to relay host smtp.126.com Error: () Error: Cannot send email to web@daixiaorui.com state
大概意思是:无法connenct中继主机smtp.126.com 错误:()错误:无法发送电子邮件给web@daixiaorui.com
上网找了n多资料后终于找到了解决方案,不是smtp类的问题,而就是linux配置的问题。原来是服务器的php.ini 禁用了fsockopen函数。
打开空间下的php.ini文件,linux空间一般都可以自定义php.ini,所以根目录下面一般会有这个文件。
有两个地方可能禁用此函数:
1. allow_url_fopen = On 查看等于后面是否为 ON,如果为OFF时函数将被禁用
2. disable_functions = fsockopen pfsockopen (我的就是这样)这里应该去掉前面的“fsockopen”。使之变成:disable_functions = pfsockopen
改过之后,保存,再重新刷新页面,就发现在linux下能成功利用smtp类发送电子邮件了。感谢网友分享的方法,问题终于得到了解决。
php中使用smtp类轻松的发送电子邮件示例下载:http://file.111cn.net/upload/2013/12/13751999009017.zip
HPMailer 是一个专门用于php语言的邮件发送类,功能十分地强大,丰富了 PHP 本身单一的 mail() 函数。支持 SMTP 等、附件等。 PHPMailer 遵守 LGPL 授权,可以免费下载。
下载地址:http://sourceforge.net/projects/phpmailer/files/phpmailer%20for%20php5_6/PHPMailer%20v5.1/PHPMailer_v5.1.zip/download
使用方法(只说法邮件,其收邮件的功能飘过):
代码如下 | 复制代码 |
IsSMTP(); $mail->SMTPAuth = true; //smtp要求身份验证 $mail->SMTPSecure = 'ssl'; //smtp验证方式 $mail->Port = 465 ; //smtp端口号 $mail->Username = "username@gmail.com"; // 注册了的gmail用户邮箱(用你的gmail邮箱就可以) $mail->Password = "password"; //密码 $mail->Host = 'smtp.gmail.com'; // 发邮件的服务器 $mail->IsHTML(true); // 是否使支持 HTML 邮件的发送,默认为 false , $mail->From ='username@gmail.com'; // 发信人的邮箱,用你的gmail邮箱就可以 $mail->FromName = 'Mailer'; // 你的姓名 $mail->Subject = '邮件标题'; // 邮件标题 $mail->Body ='Hello world'; // 邮件内容,支持HTML代码 $mail->AddAddress('name@anydomain.com', 'First Last'); // 收件人邮箱和姓名 if (!$mail->Send()) { echo $mail->ErrorInfo; } |
本来一般是照上面的配置就可以通过指定的smtp服务器发送邮件了,但是经个人测试上面的设置只能用163,126的smtp发送邮件,像qq,gmail等都发送不出去! 在google上搜了N久,终于找到了下面的方法可以成功通过gmail发送邮件了,当然还包括:qq、sina、sohu等(雅虎的不能通过,貌似雅虎默认smtp没有开通,得开通他们的一个叫‘邮件提醒’功能才给你开通smtp/pop3)
下面是解决方法: 只需要在 class.phpmailer.php 中查找到下面的代码(可能由于下载的版本不同而稍微有点区别):
代码如下 | 复制代码 |
if(eregi(’^(.+):([0-9]+)$‘, $hosts[$index], $hostinfo)) { $host = $hostinfo[1]; $port = $hostinfo[2]; } else { $host = $hosts[$index]; $port = $this->Port; } |
更改为( 不任什么版本都改成下面的内容):
代码如下 | 复制代码 |
if(eregi(’^#(([a-z]+://)?[^:]+):(d+)#i‘, $hosts[$index], $hostinfo)) { $host = $hostinfo[1]; $port = $hostinfo[3]; } else { $host = $hosts[$index]; $port = $this->Port; } |
几经修改希望你也可以顺利的通过gmail的smtp发送邮件了(当然要在win下面要能发邮件,还得在IIS中安装smtp以及在php中有OpenSSL扩展的支持)。 最后经测试,这种修改后能顺利通过 126163qqgmailsinasohu的smtp发送邮件
相关文章
Java8 实现stream将对象集合list中抽取属性集合转化为map或list
这篇文章主要介绍了Java8 实现stream将对象集合list中抽取属性集合转化为map或list的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-02-05- 这篇文章主要介绍了源码分析系列之json_encode()如何转化一个对象,对json_encode()感兴趣的同学,可以参考下...2021-04-22
- PHP去除html、css样式、js格式的方法很多,但发现,它们基本都有一个弊端:空格往往清除不了 经过不断的研究,最终找到了一个理想的去除html包括空格css样式、js 的PHP函数。...2013-08-02
- 安装curl扩展支持https是非常的重要现在许多的网站都使用了https了,下面我们来看一篇关于PHP安装curl扩展支持https例子吧。 问题: 线上运行的lamp服务器,默认yu...2016-11-25
JavaScript中的数组遍历forEach()与map()方法以及兼容写法介绍
下面小编就为大家带来一篇JavaScript中的数组遍历forEach()与map()方法以及兼容写法介绍。小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2016-05-20- index.php怎么打开?初学者可能不知道如何打开index.php,不会的同学可以参考一下本篇教程 打开编辑:右键->打开方式->经文本方式打开打开运行:首先你要有个支持运行PH...2017-07-06
PHP中func_get_args(),func_get_arg(),func_num_args()的区别
复制代码 代码如下:<?php function jb51(){ print_r(func_get_args()); echo "<br>"; echo func_get_arg(1); echo "<br>"; echo func_num_args(); } jb51("www","j...2013-10-04- 这篇文章主要介绍了PHP编程 SSO详细介绍及简单实例的相关资料,这里介绍了三种模式跨子域单点登陆、完全跨单点域登陆、站群共享身份认证,需要的朋友可以参考下...2017-01-25
- 这篇文章主要介绍了PHP实现创建以太坊钱包转账等功能,对以太坊感兴趣的同学,可以参考下...2021-04-20
- 这篇文章主要介绍了JavaScript WeakMap使用的详细介绍,帮助大家更好的理解和使用JavaScript,感兴趣的朋友可以了解下...2021-02-06
- map容器是C++ STL中的重要一员,删除map容器中value为指定元素的问题是我们经常与遇到的一个问题,下面这篇文章主要给大家介绍了关于利用C++如何删除map容器中指定值的元素的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。...2020-04-25
- floor会产生小数了如果我们不希望有小数我们是可以去除小数点的了,下面一聚教程小编来为各位介绍php使用floor去掉小数点的例子,希望对各位有帮助。 float floor (...2016-11-25
java中JSONObject转换为HashMap(方法+main方法调用实例)
这篇文章主要介绍了java中JSONObject转换为HashMap(方法+main方法调用实例),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-11-14- 这篇文章主要介绍了解决jmap命令打印JVM堆信息异常的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-12-04
- 这篇文章主要介绍了mybatis Map查询结果下划线转驼峰的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-09-24
- 这篇文章主要为大家详细介绍了php微信公众账号开发之五个坑,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2016-10-02
ThinkPHP使用心得分享-ThinkPHP + Ajax 实现2级联动下拉菜单
首先是数据库的设计。分类表叫cate.我做的是分类数据的二级联动,数据需要的字段有:id,name(中文名),pid(父id). 父id的设置: 若数据没有上一级,则父id为0,若有上级,则父id为上一级的id。数据库有内容后,就可以开始写代码,进...2014-05-31- 这篇文章主要介绍了PHP如何通过date() 函数格式化显示时间,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-11-13
- 相信大家现在都知道微信小程序吧,下面这篇文章主要给大家介绍了微信小程序开发之map地图的相关资料,分享出来供大家参考学习,文中通过示例代码介绍的非常详细,需要的朋友们下面跟着小编来一起学习学习吧。...2017-06-15
- 今天我给大家分享的是在不刷新页面的前提下,使用PHP+jQuery+Ajax实现多图片上传的效果。用户只需要点击选择要上传的图片,然后图片自动上传到服务器上并展示在页面上。...2015-03-15