Python smtplib.SMTP_SSL() 登录邮箱并发送邮件

本文介绍了如何使用Python的smtplib.SMTP_SSL()模块实现从txt文件读取内容,自动登录邮箱并发送邮件。虽然目前功能较为基础,未来计划扩展到上传下载文件、接收文件提醒及阅读文件内容等更复杂的操作。
部署运行你感兴趣的模型镜像

由于个人比较懒,每次写邮件时总要去打开邮箱地址然后登录,有的时候还容易忘记密码,感觉很不方便,于是就想写一个在txt文件中写内容,然后直接读取,执行脚本文件,直接发送内容。

还有很多需要改进的地方,如上传下载文件等,接收文件提醒,阅读文件内容等。还有一些配置内容也不是很方便。

py文件如下:

# !/usr/bin/python
# -*-coding:utf-8-*-
import smtplib
from email.mime.text import MIMEText

def send_email(host, username, passwd, send_to, subject, content):
    msg = MIMEText(content)
    my_email = username+"<"+username+">"
    msg['From'] = my_email
    msg['Subject'] = subject
    msg['To'] = ",".join(send_to)

    try:
        # server = smtplib.SMTP()
        # server.connect(host)
        server = smtplib.SMTP_SSL(host,465)
        server.ehlo()
        # server.starttls()
        server.login(username, passwd)
###第一种方法写的时候遇到了困难,问题没有是有strip(),因为此处使用了 msg.as_string(),应该是将内容转化成了str,然后又进行的处理
        server.sendmail(username, send_to, msg.as_string())
        server.close()
        return 'sucessfully'
    except Exception as e:
        print 'Exception: send email failed', e
        return 'failed to send mail'

if __name__ == '__main__':
    filename = "emails_txt"
    f = open(filename)
    lines = f.readlines()
    # file = f.read()
#######################方法1############
###此种方法是在txt文件中配置内容和写文件内容
    host ="smtp.qq.com"
    username = lines[1].split(':')[-1].strip()
    # # host = "smtp." + str(username.split('@')[-1])
    passwd = lines[2].split(':')[-1].strip()
    to_list = lines[3].split(':')[-1].strip().split(',')
    subject = lines[4].split(':')[-1].strip()
    content = lines[5].split(':')[-1]
    # print type(passwd), passwd
    # print type(to_list), to_list
    # print type(subject), subject
    # print type(content), content
    send_email(host, username, passwd, to_list, subject, content)
#######################方法2############
###下面这种方法是在程序中直接写内容,一旦文件内容比较多的时候,就很不适合使用了
    """
    host = 'smtp.qq.com'
    username = '1663462979@qq.com'
    #此password为进入QQ邮箱设置页面,开启SMTP服务,发短信获取的授权码,而非通常我们使用的password
    passwd = 'zcgmkptvlzqgfcef'
    to_list = ['1663462979@qq.com','zgducas@163.com']
    subject = "txt文件发送邮件"
    content = "用python发送邮件"
    content = file
    # print type(passwd), passwd
    # print type(to_list), to_list
    # print type(subject), subject
    # print type(content), content
    if send_email(host, username, passwd, to_list, subject, content):
        print "done!"
    else:
        print "failed!"
    """

txt文件

host:smtp.qq.com
username:1663462997@qq.com
passwd:zcgmkptvzlqgfcff
to_list:1663462997@qq.com,zgd_ucas@163.com
subject:这个邮件
content:试试能行不

参考学习链接:https://www.programcreek.com/python/example/6443/smtplib.SMTP_SSL。

                       https://blog.youkuaiyun.com/zniahfag/article/details/51387996。

                      

您可能感兴趣的与本文相关的镜像

Python3.10

Python3.10

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值