本帖最后由 你阿曦 于 2021-4-8 17:49 编辑
话不多说上代码,一共有三部分操作.
1、创建app.js 输入代码
2、创建mail.html输入代码
2、npm install安装所需要的依赖
3、node app.js运行脚本
统一回复 对象楼主不包分配 请自己new Object()
引用了一个png图片
w1.png
最终效果图
最终效果图
第一次发帖还望各位老哥不吝赐教
[md]//引用superagent包,用于服务器发送http请求
const request = require('superagent')
//导入cheerio包 解析http
const cheerio = require('cheerio')
//导入 art-template
const template = require('art-template')
//导入PATH
const path = require('path')
//导入nodemaler发送邮件的包
const nodemailer = require('nodemailer')
//导入定时任务包
var schedule = require('node-schedule')
function getDate() {
return new Promise((resolve, reject) => {
//现在的时间
const today = new Date()
//认识的时间
const meet = new Date('YYYY-MM-DD')
//认识的天数
const count = Math.floor((today - meet) / 1000 / 60 / 60 / 24)
//今天的日期
const format =
today.getFullYear() +
' / ' +
(today.getMonth() + 1) +
' / ' +
today.getDate()
const dayDate = {
count,
format,
}
resolve(dayDate)
})
}
// getDate()
//请求墨迹天气的数据
function getMojiData() {
return new Promise((resolve, reject) => {
request
.get('http://tianqi.moji.com/tommorrow/china/zhejiang/taizhou')
.end((err, res) => {
if (err) return console.log('数据请求失败')
const $ = cheerio.load(res.text)
//温度
const wendu =
$('.detail_weather em:eq(0)').text() +
'-' +
$('.detail_weather em:eq(1)').text()
//天气
const weather = $('.detail_weather span').text()
//提示
const tips = $('.detail_ware_title span').text()
//墨迹对象
const mojiData = {
weather,
wendu,
tips,
}
resolve(mojiData)
})
})
}
// getMojiData()
//请求One页面抓取数据
function getOneData() {
return new Promise((resolve, reject) => {
request.get('http://wufazhuce.com/').end((res, err) => {
if (err) return console.log('数据请求失败')
//把返回值中的数据解析成HTML
const $ = cheerio.load(res.text)
//抓取One的图片
const img = $('.carousel-inner>.item>img, .carousel-inner>.item>a>img')
.eq(0)
.attr('src')
//抓取One的文本
const text = $('.fp-one .fp-one-cita-wrapper .fp-one-cita a').eq(0).text()
const OneData = {
img,
text,
}
resolve(OneData)
})
})
}
// getOneData()
//渲染邮件
async function renderTemplate() {
//获取日期数据
const dayData = await getDate()
//获取墨迹天气数据
const mojiData = await getMojiData()
//获取One数据
const oneData = await getOneData()
return new Promise((resolve, reject) => {
const html = template(path.join(__dirname, './mail.html'), {
dayData,
mojiData,
oneData,
})
resolve({ html, mojiData })
})
}
// renderTemplate();
async function sendMail() {
const { html, mojiData } = await renderTemplate()
let transporter = nodemailer.createTransport({
host: 'smtp.126.com',
port: 465,
secure: true, // true for 465, false for other ports
auth: {
user: '12345@126.com', // 你的邮箱账号
pass: '12345', //你的邮箱密码
},
})
// send mail with defined transport object
let mailOptions = {
from: '"帅气的开发者" <12345@126.com>', // 你的邮箱发件人及地址
to: 'xxxxxx@qq.com', // 对象的邮箱地址
subject: `温度:${mojiData.wendu} 天气:${mojiData.weather}`, // 副标题
html: html, // html主体文件
}
transporter.sendMail(mailOptions, (error, info = {}) => {
if (error) {
sendMail()
}
console.log('发送成功', info.messageId)
console.log('等待下一次发送!')
})
}
// sendMail()
//定时任务,每天晚上九点.
var j = schedule.scheduleJob('0 0 21 * * *', function () {
sendMail()
console.log('定时任务执行完毕!')
})
```[/md]
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<div
style="
border: 0px solid red;
width: 100%;
margin: 40px auto;
color: gray;
text-align: center;
font-size: 20px;
"
align="center"
>
<span>我们已经在一起</span>
<span style="font-size: 24px; color: red">{{dayData.count}}</span>
<span>天</span>
</div>
<div
style="
border: 0px solid red;
width: 100%;
margin: 0 auto;
color: gray;
text-align: center;
"
>
<img src="./w1.png" style="background: royalblue" alt="天气图标" />
<b style="display: block; color: black; font-size: 24px; margin: 15px 0">
明日天气:{{mojiData.weather}}
</b>
<span
style="display: block; color: black; font-size: 22px; margin: 15px 0"
>
明日温度:{{mojiData.wendu}}
</span>
<span style="display: block; color: lightgray; font-size: 20px">
温馨提示:{{mojiData.tips}}
</span>
</div>
<div style="text-align: center; margin: 35px 0">
<!-- <span
style="display: block; margin-top: 55px; color: gray; font-size: 15px"
>
ONE 一个
</span>
-->
<span
style="
display: block;
margin-top: 25px;
color: lightgray;
font-size: 22px;
"
>
{{dayData.format}}
</span>
<!-- <img
src="{{oneData.img}}"
style="margin-top: 10px; width: 100%"
alt="One配图"
/> -->
<div style="margin: 10px auto; width: 85%; color: gray">
{{oneData.text}}
</div>
</div>
</body>
</html>
补充一个package.json吧 考虑一下小白
{
"dependencies": {
"art-template": "^4.13.2",
"cheerio": "^1.0.0-rc.5",
"node-schedule": "^2.0.0",
"nodemailer": "^6.5.0",
"superagent": "^6.1.0"
}
}
|