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

Aay*_*jan 2 python python-3.x

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

use*_*472 5

这个短小的数字怎么样?

'''
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()
Run Code Online (Sandbox Code Playgroud)