Singi

singi-blog


  • 首页

  • 分类

  • 关于

  • 归档

  • 标签

phpmailer

发表于 2016-09-19 | 分类于 php , thinkphp , phpmailer

PHPMailer

安装phpmailer

1
composer require phpmailer/phpmailer

Github官方例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user@example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('joe@example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen@example.com'); // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}

准备发送邮件的服务器,这里选择QQ STMP 服务器。

登录QQ邮箱,设置->账户->POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务,这一选项下,开通POP3/SMTP服务。需要发送短信生成授权码(0.1元/条),然后记住生成的授权码,后面需要用到。

编写自己的邮件发送函数

我使用的是thinkphp3.2.3,把phpmailer包下载下来,然后放在到Thinkphp/Library/Vendor/目录下,这时的路径为Thinkphp/Library/Vendor/phpmailer,然后在函数前面引入phpmailer,如下面代码所示,即可正常使用phpmailer。

注意:自动加载文件是引入PHPMailerAutoload.php,不是autoload.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
vendor('phpmailer.PHPMailerAutoload');
/**
* 发送邮件
* @param $subject //邮件主题
* @param $body //邮件内容
* @param $to_mail //发送到邮箱地址
* @return bool
*/
function send_mail($subject,$body,$to_mail){
date_default_timezone_set('PRC');//设置时区
$mail->CharSet = 'UTF-8'; //设置邮件的字符编码,这很重要,不然中文乱码
$mail = new \PHPMailer();
// $mail->SMTPDebug = 3; // 调试模式,有错误的情况下最好打开,便于查出错误
$mail->isSMTP(); // 使用 SMTP
$mail->Host = 'smtp.qq.com'; // 使用 QQ的 SMTP 服务器
$mail->SMTPAuth = true; // 使用 SMTP 权鉴,必须
$mail->Username = '发送邮件地址'; // SMTP username
$mail->Password = '发送邮件QQ授权码'; // SMTP password,使用qq的话,是授权码,不是邮箱登录密码!!!
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted 加密
$mail->Port = 465; // TCP port to connect to 链接端口:465或587
$mail->setFrom('服务器邮箱地址', '别名'); //服务器邮箱地址,第二个参数是别名,从服务器邮箱发送到哪
$mail->addAddress($to_mail, '别名'); // 接受邮箱地址,可以添加多个
// $mail->addReplyTo('info@example.com', 'Information');
// $mail->addCC('cc@example.com');
// $mail->addBCC('bcc@example.com');
// $mail->addAttachment('./admin.php'); // 添加附件,附件地址是服务器上的地址
// $mail->addAttachment('./1464581020.jpg', 'new.jpg'); // 可以给附件设置别名,附件地址是服务器上的地址
$mail->isHTML(true); // 发送 HTML格式的内容
$mail->Subject = $subject;//邮件主题
$mail->Body = '<p>'.$body.'</p>';//邮件内容
// $mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; //纯文本邮件内容
if(!$mail->send()) {
// return $mail->ErrorInfo;//调试的时候返回错误信息,我这里测试成功了,就不返回了。
return false;
} else {
return true;
}
}

调用函数,发送邮件

在控制器中直接调用send_mail($subject,$body,$to_mail)函数,就可以成功向指定的邮箱发送邮件了。

RowDataPacket

发表于 2016-09-07 | 分类于 nodejs , express

nodejs express mysql 操作返回的RowDataPacket处理。

1 可以在mysql的设置中返回json格式数据

1
2
3
4
5
6
7
8
{
host : '***',
user : '***',
password : '***',
database : '***',
stringifyObjects: true, //返回Json格式数据,false则返回RowDataPacket对象
dateStrings : true //日期格式化成 "Y-m-d H:i:s",false则为UTC标准时间
}

2 手动转换RowDataPacket对象,再返回渲染输出

可以用JSON.stringify(RowDataPacket)转化为json字符串,然后在返回。

3 直接返回RowDataPacket对象,渲染输出

其实也是一个二维数组,渲染的时候forEach()遍历输出就可以了,这里演示的ejs渲染输出.

后台返回数据

1
res.render('index',{count:count,page:page,rows:length,data:rows});

前台ejs渲染输出

1
2
3
4
5
6
7
<% data.forEach(function(v){ %>
<tr>
<td><%= v['id'] %></td> //可以用数组方式,适用于属性是这种变态的时候:"count(id):4",用v['count(id)']获取
<td><%= v.name %></td> //也可以用.方式
<td><%= v.skill %></td>
</tr>
<% }) %>

hexoGithub

发表于 2016-09-05 | 分类于 hexo

hexo安装博客并发布到github上 deply hexo on github

hexo 初级使用教程

1 安装hexo install hexo

1
2
3
4
5
npm install hexo-cli -g
hexo init blog
cd blog
npm install
hexo server

2 创建github仓库 create github repository

  • 一个用来存hexo项目 one for hexo project

    这里是用来写博客使用的项目,只要clone这里的项目,就可以继续以前的进度写博客。

  • 一个用来存生成的blog(这里是可以访问的博客) one for hexo blog

    这里是github提供的可访问的静态网页网址。仓库的命名需要符合规范:
    github用户名.github.io,例如我的github用户名是singi2016cn,那么我的仓库命名就是singi2016cn.github.io,直接访问http://singi2016cn.github.io就可以看到。

3 使用hexo写博客并上传至github

  1. 创建新文章
    hexo new 文章名
    1
    hexo new firstBlog //也可以使用缩写 hexo n 文章名

然后在source/_posts/下面找到刚生成的新文件,可以使用mardown工具打开进行编辑,然后保存。

  1. 生成文件

    1
    hexo ganerate //缩写 hexo g
  2. 发布文件
    在此之前需要先设置一个git
    安装 git 命令行工具

    1
    npm install hexo-deployer-git --save

然后在_config.yml文件中添加下面的配置

1
2
3
4
5
deploy:
type: git //类型为git
repo: <repository url> //github仓库地址
branch: [branch] //仓库分支,没有会自动检测
message: [message] //提交信息,默认为 `Site updated: 2016-09-05 21:58:45`

配置完成之后,执行

1
hexo deply //缩写hexo d

即可发布到github上面,然后访问之前的网址就可以看到自己的博客网站了

待探索

  1. 添加标签tag和分类category
  2. 更换更加炫酷的主题

感谢hexo带来极致的创建博客之旅

Hello World

发表于 2016-09-05 | 分类于 hexo

Welcome to Hexo! This is your very first post. Check documentation for more info. If you get any problems when using Hexo, you can find the answer in troubleshooting or you can ask me on GitHub.

Quick Start

Create a new post

1
$ hexo new "My New Post"

More info: Writing

Run server

1
$ hexo server

More info: Server

Generate static files

1
$ hexo generate

More info: Generating

Deploy to remote sites

1
$ hexo deploy

More info: Deployment

12
Singi

Singi

singi hexo blog

14 日志
12 分类
19 标签
GitHub
友情链接
  • github
  • hexo
© 2016 - 2017 Singi
由 Hexo 强力驱动
主题 - NexT.Muse