使用python3每天定时女朋友发一封暖心邮件

606 阅读3分钟

每天给女朋友发送暖心邮件

废话不多说哄女朋友开心最重要,开干

准备Html模板

注意:邮件需要使用内敛样式,不然无法显示会出问题

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>邮件模版</title>
</head>
<body>
<div style="width: 375px; height: 667px; margin: 0 auto; background-repeat: no-repeat; background-size: cover; background-image: url('https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic1.win4000.com%2Fmobile%2Fd%2F53917b75357d8.jpg&refer=http%3A%2F%2Fpic1.win4000.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1617371037&t=ff552f95a530e7f78691c238739558b7');">
    <div style="padding: 10% 10% 0 10%; ">
        <div style="color: white; border:8px white solid;background-repeat: no-repeat; background-size: cover; height: 100%; background-image: url('https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic1.win4000.com%2Fmobile%2Fd%2F53917b75357d8.jpg&refer=http%3A%2F%2Fpic1.win4000.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1617371037&t=ff552f95a530e7f78691c238739558b7'); ">
            <div style="height: 20vh"></div>
            <div style="font-size: 36px; font-weight: bold;">10/30</div>
            <p style="padding: 2px 0;margin: 0;">星期五</p>
            <p style="padding: 0 0 20px 0;margin: 0;">农历九月十四</p>
        </div>
    </div>
    <div style="font-size: 14px; margin: 0 10%;padding: 20px 8px 0 8px; line-height: 20px; background-color: white; color: #16181A;">
        <p style="margin: 0; font-size: 40px; line-height: 4px;">"</p>
        你要记得那些在黑暗中抱紧你的人,逗你笑的人陪你彻夜聊天的人,坐车来看望你的人陪你哭过的人,总是以你为重的人,带着你四处游荡的人和说想念你的人,是这些人组成你生命中一点一滴的温暖,是这些温暖使你远离阴霾…,也是这些温暖使你成为一个善良的人。
        <p style="margin: 0; font-size: 40px; text-align: right;line-height: 40px;" style="margin: 0">"</p>
    </div>
</div>
</body>
</html>

准备文案和图片

为了维持好的爱情,里面的图片和文案都是看过再添加的,避免爬虫爬到不友好的文案造成误会,要是这样就很难解释了。

需要的python库

import smtplib
import importlib
import lunar_calendar
import random
import datetime
import json
import time
from email.mime.text import MIMEText
from email import encoders
from email.header import Header
from email.utils import parseaddr, formataddr
import configparser

# 定时任务
from apscheduler.schedulers.blocking import BlockingScheduler

邮件发送

    msg = MIMEText('''
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <title>Dear,洁洁 元气满满的一天开始啦,要开心噢(づ ̄ 3 ̄)づ'</title>
        </head>
        <body>
        <div style="width: 375px; 
        height: 667px; margin: 0 auto; 
        background-repeat: no-repeat; 
        background-size: cover; 
        background-image: url('{url}');">
            <div style="padding: 10% 10% 0 10%; ">
                <div style="color: white; 
                border:6px white solid;
                background-repeat: no-repeat; 
                background-size: cover; 
                height: 100%; background-image: url('{url}'); ">
                    <div style="min-height: 240px">
                        <p style="margin: 0;padding: 10px;font-size: 20px;font-weight: 500;">{to}</p>
                        <p style="margin: 0;padding:  0 10px; font-size: 14px;">新的一天开始啦,要开心噢(づ ̄ 3 ̄)づ</p>
                    </div>
                    <div style="font-size: 36px; padding-left: 10px; font-weight: bold;">{today_date}</div>
                    <p style="padding: 2px 0 2px 10px;margin: 0;">{week}</p>
                    <p style="padding: 0 0 20px 10px;margin: 0;">农历{l_date}</p>
                </div>
            </div>
            <div style="font-size: 14px;
             margin: 0 10%;
             padding: 20px 8px 0 8px; 
             line-height: 20px; 
             background-color: white; 
             color: #16181A;">
                <p style="margin: 0; font-size: 40px; line-height: 4px;">“</p>
                <p style="padding: 0 6px 0 10px; margin: 0">{content}</p>
                <p style="margin: 0; font-size: 40px; text-align: right;line-height: 40px;">”</p>
            </div>
        </div>
        </body>
        </html>
    '''.format(
        l_date=l_date,
        week=week,
        content=random_data['content'],
        url=url,
        today_date=today_date,
        to=config.get('Email', 'to')
    ), 'html', 'utf-8')
    msg['From'] = Header(config.get('Email', 'from'), 'utf-8')
    msg['To'] = Header(config.get('Email', 'to'), 'utf-8')
    msg['Subject'] = Header(config.get('Email', 'subject'), 'utf-8').encode()
    # 输入Email地址和口令:
    from_addr = config.get('Email', 'from_addr')
    password = config.get('Email', 'password')
    # 输入收件人地址:
    to_addr = email
    # 输入SMTP服务器地址:
    smtp_server = config.get('Email', 'smtp_server')

    server = smtplib.SMTP_SSL(smtp_server)
    server.ehlo(smtp_server)
    # server.starttls() # 如果是SSL,则用 587 端口,再加上这句代码就行了
    server.login(from_addr, password)  # 登录SMTP服务器
    server.sendmail(from_addr, to_addr, msg.as_string())  # 发邮件
    server.quit()

定时任务

在 6:30 19:30 7:30 运行

    scheduler = BlockingScheduler()
    # 在 6:30 19:30 7:30 运行一次
    scheduler.add_job(main, 'cron', hour='19', minute='30', args=['28373186@qq.com'])
    scheduler.add_job(main, 'cron', hour='6', minute='30', args=['28373186@qq.com'])
    scheduler.add_job(main, 'cron', hour='7', minute='30', args=['96485641@qq.com'])

    scheduler.start()

配置文件email_config.ini

在项目中有一个email_config.ini文件,需要配置邮箱的授权码

[Email]
from = 爱你的小明(*^‧^*)
to = Dear,洁洁(~^O^~)
subject = 元气满满的一天开始啦,要开心噢(づ ̄ 3 ̄)づ
from_addr = 邮箱
password = 密码(注意:不是QQ邮箱登录密码)
smtp_server = smtp.qq.com

如何获取授权码(password)

我使用的是QQ邮箱,目前介绍QQ邮箱的授权码获取

1、登录QQ邮箱点击设置

QQ截图20210312222150.png

2、选择账户->IMAP/SMTP服务将它开启

QQ截图20210312222413.png

3、生成授权码

将获取的授权码填到password就可以了

QQ截图20210312222735.png

邮件发送

运行send_email.py文件,main函数里面的参数为要发送的邮箱地址(目前没有群发功能,因为女朋友就一个->捂脸),scheduler是定时任务

if __name__ == '__main__':
    main('283731869@qq.com')
    # 定时任务
    print('开始运行脚本--( # ^^ # )')
    scheduler = BlockingScheduler()
    # 在 6:30 运行一次
    scheduler.add_job(main, 'cron', hour='19', minute='30', args=['283731869@qq.com'])
    scheduler.add_job(main, 'cron', hour='6', minute='30', args=['283731869@qq.com'])
    scheduler.add_job(main, 'cron', hour='7', minute='30', args=['964856415@qq.com'])

    scheduler.start()

部署

我使用的是Linux服务器,各位用自己服务器命令运行就好,到此就结束了,快去试试把!

git clone https://github.com/Tecode/resource_library.git

安装依赖

nohup python ./send_email.py > nohup.log 2>&1 & 

邮件预览

QQ截图20210312225407.png

QQ截图20210312221900.png

项目地址

Github