我想通过电子邮件将python程序的结果发送给我

时间:2020-04-17 15:35:37

标签: python

我正在使用repl进行python编码,而我刚刚设计了一个游戏。我希望将人们提交的答案通过电子邮件发送给我。我可以获得包含所有答案的变量,但是不确定如何调用电子邮件功能。

2 个答案:

答案 0 :(得分:0)

在线上有一些有关如何执行此操作的教程。

我过去曾经使用过this one,发现它非常有用。

答案 1 :(得分:0)

我使用Gmail通过Python发送电子邮件。首先,您必须让“安全性较低的应用”访问您的帐户,如下所述: https://support.google.com/accounts/answer/6010255

然后我使用此功能发送带有主题,正文和附件的电子邮件。

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders

def send_email(to_address,
               subject,
               body,
               from_address,
               password_of_sender,
               attachment_file=None):
    """
    Send an email wtith attachment using a Gmail account as the sender.
    """    
    # instance of MIMEMultipart
    msg = MIMEMultipart()
    msg['From'] = from_address
    msg['To'] = to_address
    msg['Subject'] = subject
    # attach the body with the msg instance
    msg.attach(MIMEText(body, 'plain'))
    if attachment_file:
        # open the file to be sent
        attachment = open(attachment_file, "rb")
        # instance of MIMEBase and named as p
        p = MIMEBase('application', 'octet-stream')
        # To change the payload into encoded form
        p.set_payload((attachment).read())
        # encode into base64
        encoders.encode_base64(p)
        p.add_header('Content-Disposition',
                    'attachment; filename= %s' % attachment_file)
        # attach the instance 'p' to instance 'msg'
        msg.attach(p)

    # creates SMTP session
    s = smtplib.SMTP('smtp.gmail.com', 587, timeout=30)
    # start TLS for security
    s.starttls()
    # Authentication
    s.login(from_address, password_of_sender)
    # Converts the Multipart msg into a string
    text = msg.as_string()
    # sending the mail
    s.sendmail(from_address, to_address, text)
    # terminating the session
    s.quit()



send_email('send_to_address@gmail.com', 'SUBJECT', 'BODY   ...', 'sent_from@gmail.com', 'sent_from_password')