PHP发送邮件功能实现

2018-03-10  本文已影响0人  哥本哈登_sketch

第一步

我用的是163邮箱发送邮件,做一个尝试,在尝试之前,需要要开启163邮箱的授权码如图所示,请记住您的授权码,将在之后的步骤中用到 image

第二步

需要下载一个类PHPMailer,我有这个资源已经上传了,免费的哦亲,连接在这http://download.csdn.NET/detail/s371795639/9693417

下载后,解压后此文件夹放在Vendor目录下,Vendor目录下有个PHPMailer文件夹,那就对了~

第三步

咱们该写代码了

html代码:

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(57, 57, 57); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(250, 247, 239); text-decoration-style: initial; text-decoration-color: initial;"><body>
<form action="{:U('home/login/changepwd')}" method="post" enctype="multipart/form-data">
邮箱:<input type="text" id="mail" name="mail"/>
标题:<input type="text" id="title" name="title"/>
内容<input type="text" id="content" name="content"/>
<input class="button" type="submit" name="submit" value="发送" style="margin: 0 auto;display: block;"/>
</form>
</body></pre>

对应的Controller的PHP代码:

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(57, 57, 57); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(250, 247, 239); text-decoration-style: initial; text-decoration-color: initial;">public function changepwd()//发送邮件来修改密码

{

**if**(**isset**($_POST['submit']))
{

    **if**(SendMail($_POST['mail'],$_POST['title'],$_POST['content']))
        $this->success('发送成功!');
   **else** $this->error('发送失败');

}
$this->display();

}</pre>

第四步

这是关键

在Common下建立function.PHP输入代码如下

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(57, 57, 57); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(250, 247, 239); text-decoration-style: initial; text-decoration-color: initial;"><?php /*

在Conf下的config.php输入配置如下

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(57, 57, 57); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(250, 247, 239); text-decoration-style: initial; text-decoration-color: initial;"><?php return array(
//'配置项'=>'配置值'
'MAIL_HOST' =>'smtp.163.com',//smtp服务器的名称
'MAIL_SMTPAUTH' =>TRUE, //启用smtp认证
'MAIL_USERNAME' =>'s371795639@163.com',//你的邮箱名
'MAIL_FROM' =>'s371795639@163.com',//发件人地址
'MAIL_FROMNAME'=>'尘中客',//发件人姓名
'MAIL_PASSWORD' =>'*******',//邮箱授权码
'MAIL_CHARSET' =>'utf-8',//设置邮件编码
'MAIL_ISHTML' =>TRUE, // 是否HTML格式邮件
);</pre>

然后应该就没有问题了,至少我的是这样。如果用QQ邮箱发送貌似一直失败,修改上面的配置也是不行。也不知道为啥。

上一篇 下一篇

猜你喜欢

热点阅读