Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python 3发送电子邮件?_Python_Python 3.x - Fatal编程技术网

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

如何使用python 3发送电子邮件?,python,python-3.x,Python,Python 3.x,请帮帮我!我想用Python3编写一个脚本,除了从我的本地机器向任何人发送电子邮件。我现在是python的初学者,这就是我尝试过的大多数脚本根本不起作用的原因。如果你也解释一下我需要在脚本中做什么,那将是一个很大的帮助。谢谢 请看以下内容: 它们很容易使用,应该对基本的电子邮件信息进行分类这个简短的数字怎么样 ''' Created on Jul 10, 2012 test email message @author: user352472 ''' from smtplib import

请帮帮我!我想用Python3编写一个脚本,除了从我的本地机器向任何人发送电子邮件。我现在是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()
直截了当地说:

from smtplib import SMTP_SSL as SMTP
from email.mime.text import MIMEText

def send_email(subject, body, toaddr="helloworld@googlegroups.com"):
    fromaddr = "johndoe@gmail.com"
    msg = MIMEText(body, 'plain')
    msg['To'] = toaddr
    msg['Subject'] = subject

    server = SMTP('smtp.gmail.com')
    server.login(fromaddr, "y0ur_p4ssw0rd")
    server.sendmail(fromaddr, toaddr, msg.as_string())
    server.quit()
所以你现在可以这样称呼它:

send_email(subject, body)
或者可以使用第三个参数指定电子邮件地址:

send_email(subject, body, "yourfriend@gmail.com")

如果初学者有一本书或教程供你学习——正如这些书和教程所示,你还应该知道,你不能从任何机器发送电子邮件,你必须使用SMTP服务器。例如,如果你有一个Gmail帐户,谷歌将允许你使用他们的SMTP服务器发送电子邮件。可能不会。我无耻地使用了一个项目中的代码,在这个项目中,它有更多的功能,而且日志记录也很方便。我想你可以把它拔出来,但我选择了留着它,以防它被再次使用。我可以在30分钟内完成一个更有用的课程,并从上面链接的例子中得到一些帮助。但是那样的话,OP就什么也学不到了。。。