在一个线程中发送自动消息

时间:2020-09-14 23:25:39

标签: python smtp

我正在尝试通过python发送每周电子邮件(Outlook)。有没有一种方法可以将主题相似/主题相同但内容不同的那些电子邮件编译到一个电子邮件线程中?

我听说这个想法是将电子邮件固定到一个固定的message_id上。但这对我不起作用。

def send_email(email_recipient, email_subject, email_message, attachment_location = '', email_cc = ''):
    
    email_sender = 'my_email@gmail.com'
    
    msg = MIMEMultipart()
    
    msg['From'] = email_sender
    msg['To'] = email_recipient
    msg['Cc'] = email_cc
    msg['Subject'] = email_subject
    
    msg.attach(MIMEText(email_message, 'plain'))
    
    # Create thread
    my_id = make_msgid()
    msg["Message-ID"]  = my_id 
    msg["In-Reply-To"] = my_id
    msg["References"] = my_id
    #    

    if attachment_location != '':
        filename = os.path.basename(attachment_location)
        attachment = open(attachment_location, "rb")
        part = MIMEBase('application', 'octet-stream')
        part.set_payload(attachment.read())
        encoders.encode_base64(part)
        part.add_header('Content-Disposition', "attachment; filename= %s" % filename)
        msg.attach(part)
    
    try:
        server = smtplib.SMTP('smtp.office365.com', 587)
        server.ehlo()
        server.starttls()
        server.login('my_email@gmail.com', 'my_password')
        text = msg.as_string()
        server.sendmail(email_sender, email_recipient, text)
        print('email sent')
        server.quit()
    except:
        print("SMPT server connection error")
    return True

0 个答案:

没有答案