PHP邮件发送例子(已测试成功)
在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来登录QQ邮件然后利用QQ邮件向指定邮件发送一封邮件的例子,希望本例子能帮助到各位同学哦。PHP发邮件的例子,发邮件当然需要一个客户端邮箱,今天这个例子是使用QQ免费企业邮箱来做为发邮件的邮箱,
代码如下 | 复制代码 |
<?php |
调用上边的函数来发送邮件:
代码如下 | 复制代码 |
$result= send_mail("admin@163.com","12345@qq.com","test","test","",""); |
注:如果没有QQ免费邮箱的朋友,可以自己去腾讯网站注册一个,然后添加成员和DNS,如果没有DNS服务器的话,就添加二个mx记录,比如使用dnspod。
imap是一款邮件交互访问的协议了,下面我来给大家介绍利用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 |
使用的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
相关文章
- 安装curl扩展支持https是非常的重要现在许多的网站都使用了https了,下面我们来看一篇关于PHP安装curl扩展支持https例子吧。 问题: 线上运行的lamp服务器,默认yu...2016-11-25
- 本文给大家介绍的是nodejs实现使用阿里大鱼短信API发送消息的方法和代码,有需要的小伙伴可以参考下。...2016-01-20
- mail()函数的作用:连接到邮件服务器,利用smtp协议,与该服务器交互并投邮件。注意:1、mail函数不支持esmtp协议,---即,只能直投,不能登陆2、由上条,我们只能直投至最终的收件服务器地址.而该地址,又是在PHP.ini中指定的,所...2015-10-30
- 这篇文章研究的主要内容就是使用PHP来发送电子邮件,总结为以下两种方法:一、使用PHP内置的mail()函数<?php $to = "test@163.com"; //收件人 $subject = "Test"; //主题 $message = "This is a test mail!"; //正文...2015-10-30
- 这篇文章主要介绍了c# 如何实现发送邮件的功能,文中示例代码非常详细,帮助大家更好的理解和学习,感兴趣的朋友可以了解下...2020-07-07
- floor会产生小数了如果我们不希望有小数我们是可以去除小数点的了,下面一聚教程小编来为各位介绍php使用floor去掉小数点的例子,希望对各位有帮助。 float floor (...2016-11-25
- 这篇文章研究的主要内容就是使用PHP来发送电子邮件,总结为以下两种方法:一、使用PHP内置的mail()函数<?php $to = "test@163.com"; //收件人 $subject = "Test"; //主题 $message = "This is a test mail!"; //正文...2015-10-30
- mail()函数的作用:连接到邮件服务器,利用smtp协议,与该服务器交互并投邮件。注意:1、mail函数不支持esmtp协议,---即,只能直投,不能登陆2、由上条,我们只能直投至最终的收件服务器地址.而该地址,又是在PHP.ini中指定的,所...2015-10-30
- 这篇文章主要介绍了python实现企业微信定时发送文本消息的实例代码,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-11-25
node.js 基于 STMP 协议和 EWS 协议发送邮件
这篇文章主要介绍了node.js 基于 STMP 协议和 EWS 协议发送邮件的示例,帮助大家更好的理解和使用node.js,感兴趣的朋友可以了解下...2021-02-15- 下面我们来看一篇关于纯Css实现下拉菜单的简单例子,希望这篇文章能够给各位同学带来帮助,具体步骤如下. 大家可能会经常用到hover这属性,用hover实现鼠标经过的颜...2017-01-22
- 在php中日期对比用得比较多了,还有一个日期加减也用到不少,下面我拿两个例子来给大家介绍在php中日期操作方法吧,希望文章能给你带来帮助 功能需求 文章发布时段操...2016-11-25
- 本例子不是读取Excel或生成新的Excel,而是读取现有的Excel文件,然后修改Excel中的数据,就像修改mysql中数据一样的哦。 代码如下 ...2016-11-25
- 本文章来介绍人一下关于与我们不同的发送邮件的方法我们来利用php curl stmp来实现邮件的发送程序。 $ telnet 邮箱SMTP服务地址 25 Trying 邮箱服务IP地址......2016-11-25
- 这篇文章主要介绍了Python基于httpx模块实现发送请求,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-07-08
- 正则提取图片中的地址我们介绍过很多的相关文章了,下面再来给各位介绍一个可以提取内容中第一张图片的例子,希望对各位有帮助。 代码如下 复制代码 ...2016-11-25
- 最近看到博客留言的头像有点别扭,因为游客的头像都是同一个头像,看着不是很舒服。虽然现在绝大多数的主题集成了Gavatar头像功能,先不说gavatar被墙的问题,我自己现在都没...2016-11-25
- <?php // 请求 PHPmailer类 文件 require_once("class.phpmailer.php"); //发送Email函数 function smtp_mail ( $sendto_email, $subject, $body, $extra_hd...2016-11-25
- 临时性需求,研究了一下天翼开发平台的东西,用来发送验证码还是不错的,但是每日限额不多,所以很鸡肋,但是保证100%到达 买的话还是蛮贵的,代码没有做任何优化处理,只是测试是...2016-11-25
- php判断字符串是否包含另一个字符串的实现方法有许多的办法,像我们在网上一搜索可看到大量关于字符是否包含指定字符的方法,下面我把这些实用的例子整理一起与大家分享...2016-11-25