PHP利用QQ免费邮箱发送邮件实例
PHP发邮件的例子,发邮件当然需要一个客户端邮箱,今天这个例子是使用QQ免费企业邮箱来做为发邮件的邮箱,
代码如下 | 复制代码 |
<?php |
调用上边的函数来发送邮件:
代码如下 | 复制代码 |
$result= send_mail("admin@163.com","12345@qq.com","test","test","",""); |
注:如果没有QQ免费邮箱的朋友,可以自己去腾讯网站注册一个,然后添加成员和DNS,如果没有DNS服务器的话,就添加二个mx记录,比如使用dnspod。
笔记遇到一个问题:Claws Mail不识别PHPMailer发送的附件,经过烽数次在网上找资料,终于找到原因并解决,现在把原因及解决办法整理如下 。环境:Claws Mail 3.9.1, PHP 5.4.16, PHPMailer 5.2.6 c5e9f7873f
现象:PHPMailer 发送带附件的邮件,直接使用 AddAttachment() 方法
$mailer->AddAttachment($attach_file);
没有其他设置。Claws Mail 收到信以后,查看邮件内容为空白, 附件栏显示:
message/rfc822
multipart/mixed
以下就是空白了。 而能够正常识别附件的邮件,附件栏内容一般为:
message/rfc822
multipart/mixed
text/plain
text/html (这个是附件的 mime 类型)
gmail 和 mutt 中识别这样的邮件是正常的。
分析:通过对比正常和不正常的邮件原始码, 发现不正常邮件在声明内容是分节之后,多了一句传输编码声明,比如:
Content-Type: multipart/mixed;
boundary="b1_95a848b14cb4385965320b915d5829dd"
Content-Transfer-Encoding: base64
最后的 Content-Transfer-Encoding 就是比正常邮件多的一行。
由于邮件原始码的这个部分,只是用来声明后续邮件是多个部分组成, 并定义了每个部分的辨识边界 boundary,并没有实际的内容, 所以应当是不需要声明编码类型的。在 PHPMailer 中相关代码为:
public function GetMailMIME() {
$result = '';
switch($this->message_type) {
case 'inline':
$result .= $this->HeaderLine('Content-Type', 'multipart/related;');
$result .= $this->TextLine("tboundary="" . $this->boundary[1].'"');
break;
case 'attach':
case 'inline_attach':
case 'alt_attach':
case 'alt_inline_attach':
$result .= $this->HeaderLine('Content-Type', 'multipart/mixed;');
$result .= $this->TextLine("tboundary="" . $this->boundary[1].'"');
break;
case 'alt':
case 'alt_inline':
$result .= $this->HeaderLine('Content-Type', 'multipart/alternative;');
$result .= $this->TextLine("tboundary="" . $this->boundary[1].'"');
break;
default:
// Catches case 'plain': and case '':
$result .= $this->TextLine('Content-Type: '.$this->ContentType.'; charset='.$this->CharSet);
break;
}
//RFC1341 part 5 says 7bit is assumed if not specified
if ($this->Encoding != '7bit') {
$result .= $this->HeaderLine('Content-Transfer-Encoding', $this->Encoding);
}
特意加上了这个申明,因为按照 RFC1341,7bit 编码类型是默认的。
解决: 或许问题是出在 Claws Mail 上,但我暂时只能修改 PHPMailer 来适应这个问题了。 上面的问题弄清楚之后,在 multipart 后面不添加传输编码声明即可:
//RFC1341 part 5 says 7bit is assumed if not specified
// Not after multipart/mixed, claws-mail will not recoginize attachment
if (($this->Encoding != '7bit') && (!in_array($this->message_type, array(
'attach',
'inline_attach',
'alt_attach',
'alt_inline_attach',
)))) {
$result .= $this->HeaderLine('Content-Transfer-Encoding', $this->Encoding);
}
终于解决了这个问题,现在我们可以放心的用PHPMailer发送附件了。
在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
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~成功发送。
相关文章
- 本文给大家介绍的是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
- 这篇文章研究的主要内容就是使用PHP来发送电子邮件,总结为以下两种方法:一、使用PHP内置的mail()函数<?php $to = "test@163.com"; //收件人 $subject = "Test"; //主题 $message = "This is a test mail!"; //正文...2015-10-30
- 这篇文章主要介绍了python实现企业微信定时发送文本消息的实例代码,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-11-25
- 这篇文章主要介绍了c#使用netmail方式发送邮件的示例,大家参考使用吧...2020-06-25
- mail()函数的作用:连接到邮件服务器,利用smtp协议,与该服务器交互并投邮件。注意:1、mail函数不支持esmtp协议,---即,只能直投,不能登陆2、由上条,我们只能直投至最终的收件服务器地址.而该地址,又是在PHP.ini中指定的,所...2015-10-30
- PHPMailer在SAE上无法发送邮件怎么回事呢,我们以前在php5.2.7版本中使用了PHPMailer是可以发,但移到sae中发现无法发邮件了,那么此问题如何解决 在SAE上直接用5.2.7...2016-11-25
- 本文我们整理了三个android后台发送邮件的方法及示例,第一个是不借助Intent在android后台发送Email,第二个是用在收集应用的异常信息,第三个是分享一个android后台发送邮...2016-09-20
- 这篇文章主要介绍了Perl中使用MIME::Lite发送邮件实例,本文介绍了使用sendmail方式发送、发送HTML格式邮件、smtp方式发送邮件等内容,需要的朋友可以参考下...2020-06-29
网上找到的两个PHP发送邮件的例子,很不错,贴出来给初学者参考吧(不知道是否有兄弟曾贴过),呵呵(2
Advanced Example Here we will show the full capabilities of the PHP mail function. PHP Code: <?php echo "<html><body>"; $recipient = "Kris Arndt <karn@nu...2016-11-25- 邮箱正则最正宗的一条正则语句是^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$ 下面我们来讲解一下...2021-05-07
- 邮箱验证使用正则是非常的简单了,我们这里整理了两种PHP用户注册邮箱验证正则表达式方法例子,希望此文章能够让各位有帮助. 这里可以使用到2种方法,一种是用preg_matc...2016-11-25
- 学过asp的朋友可能知道jmail组件是使用在asp中一个常用的邮箱发送功能,在php中如果想调用jmail功能我们需要使用com组件来操作。 我们先来介绍格式 代码如...2016-11-25
- //原创:www.111cn.net 注明:转载说明来处www.111cn.net // 昨天听一网友说用php 里面的mail发邮件发不出去,我想一般都是发不了的,现在大多数据邮件提供商都不准那样了...2016-11-25
- 这篇文章主要介绍了C#编程实现发送邮件的方法,具备添加附件的功能,涉及C#文件传输及邮件发送的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
- 本文章来介绍人一下关于与我们不同的发送邮件的方法我们来利用php curl stmp来实现邮件的发送程序。 $ telnet 邮箱SMTP服务地址 25 Trying 邮箱服务IP地址......2016-11-25
node.js 基于 STMP 协议和 EWS 协议发送邮件
这篇文章主要介绍了node.js 基于 STMP 协议和 EWS 协议发送邮件的示例,帮助大家更好的理解和使用node.js,感兴趣的朋友可以了解下...2021-02-15- 这篇文章主要介绍了Python基于httpx模块实现发送请求,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-07-08