python - 如何使用 python 3 发送电子邮件?

标签 python python-3.x

请帮帮我!我想在 python 3 中制作一个脚本,它可以从我的本地机器向任何人发送电子邮件。我现在是 python 的初学者,这就是为什么我尝试过的大多数脚本根本不起作用的原因。如果您还解释我需要用脚本做什么,那将是一个很大的帮助。谢谢!

最佳答案

这个短小的数字怎么样。

'''
Created on Jul 10, 2012
test email message
@author: user352472
'''
from smtplib import SMTP_SSL as SMTP
import logging
import logging.handlers
import sys
from email.mime.text import MIMEText

def send_confirmation():
    text = '''
    Hello,

    Here is your test email.

    Cheers!
    '''        
    msg = MIMEText(text, 'plain')
    msg['Subject'] = "test email" 
    me ='yourcooladdress@email.com'
    msg['To'] = me
    try:
        conn = SMTP('smtp.email.com')
        conn.set_debuglevel(True)
        conn.login('yourcooladdress', 'yoursophisticatedpassword')
        try:
            conn.sendmail(me, me, msg.as_string())
        finally:
            conn.close()

    except Exception as exc:
        logger.error("ERROR!!!")
        logger.critical(exc)
        sys.exit("Mail failed: {}".format(exc))


if __name__ == "__main__":
    logger = logging.getLogger(__name__)
    logger.setLevel(logging.DEBUG)
    ch = logging.StreamHandler()
    ch.setLevel(logging.DEBUG)
    formatter = logging.Formatter(
    '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    ch.setFormatter(formatter)
    logger.addHandler(ch)

    random_ass_condition = True

    if random_ass_condition:
        send_confirmation()

关于python - 如何使用 python 3 发送电子邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11396799/

相关文章:

python - matplotlib:分组箱线图

python - Windows 上的 WX Python 和原始输入 (WM_INPUT)

python - Tensorflow 数据集 API 中的过采样功能

python - 如果字典键等于某个值,则从列表中删除字典

python - 在 sqlalchemy、数据库、ORM 中使用标签

python - 如何在 python 中使用 SQL 时移动值

python - 为什么运行 py 和 kv 脚本时出现空白屏幕?

python - Python中的正则表达式出乎意料地慢

python-3.x - 对 Pandas DataFrame 描述输出进行排序

python - 使用 pg_upgrade 从 11.1 升级到 11.6 的 postgres 问题