2015-10-02 13 views
2

我有以下用Python 3编写的电子邮件代码... 我想根据接收客户端以HTML或纯文本形式发送电子邮件。然而,hotmail和gmail(我使用后者发送)接收零换行/回车字符,使纯文本显示在一行上。我的问题是如何在接收方的纯文本电子邮件中获得换行/回车? Linux上的Thunderbird是我的首选客户端,但我也注意到Microsoft的webmail hotmail也存在同样的问题。如何以纯文本模式将电子邮件线路订阅源作为带有UTF8的MIME替代品发送?

#!/usr/bin/env python3 
# encoding: utf-8 
""" 
python_3_email_with_attachment.py 
Created by Robert Dempsey on 12/6/14; edited by Oliver Ernster. 
Copyright (c) 2014 Robert Dempsey. Use at your own peril. 

This script works with Python 3.x 
""" 

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

COMMASPACE = ', ' 

def send_email(user, pwd, recipient, subject, bodyhtml, bodytext): 
    sender = user 
    gmail_password = pwd 
    recipients = recipient if type(recipient) is list else [recipient] 

    # Create the enclosing (outer) message 
    outer = MIMEMultipart('alternative') 
    outer['Subject'] = subject 
    outer['To'] = COMMASPACE.join(recipients) 
    outer['From'] = sender 
    outer.preamble = 'You will not see this in a MIME-aware mail reader.\n' 

    # List of attachments 
    attachments = [] 

    # Add the attachments to the message 
    for file in attachments: 
     try: 
      with open(file, 'rb') as fp: 
       msg = MIMEBase('application', "octet-stream") 
       msg.set_payload(fp.read()) 
      encoders.encode_base64(msg) 
      msg.add_header('Content-Disposition', 'attachment', filename=os.path.basename(file)) 
      outer.attach(msg) 
     except: 
      print("Unable to open one of the attachments. Error: ", sys.exc_info()[0]) 
      raise 

    part1 = MIMEText(bodytext, 'plain', 'utf-8') 
    part2 = MIMEText(bodyhtml, 'html', 'utf-8') 

    outer.attach(part1) 
    outer.attach(part2) 

    composed = outer.as_string() 

    # Send the email 
    try: 
     with smtplib.SMTP('smtp.gmail.com', 587) as s: 
      s.ehlo() 
      s.starttls() 
      s.ehlo() 
      s.login(sender, gmail_password) 
      s.sendmail(sender, recipients, composed) 
      s.close() 
     print("Email sent!") 
    except: 
     print("Unable to send the email. Error: ", sys.exc_info()[0]) 
     raise 

下面是测试代码:

def test_example(): 

some_param = 'test' 
another_param = '123456' 
yet_another_param = '12345' 

complete_html = pre_written_safe_and_working_html 

email_text = "Please see :" + '\r\n' \ 
       + "stuff: " + '\r\n' + some_param + '\r\n' \ 
       + "more stuff: " + '\r\n' + another_param + '\r\n' \ 
       + "stuff again: " + '\r\n' + yet_another_param, + '\r\n' 

recipients = ['[email protected]'] 

send_email('[email protected]', \ 
      'password', \ 
      recipients, \ 
      'Title', \ 
      email_text, \ 
      complete_html) 

,因为它似乎在互联网上每个人的任何建议将是最受欢迎的仅使用HTML电子邮件。这很好,但我希望能够回到我们这些不想使用HTML的人的纯文本。

感谢,

回答

0

我真的可以推荐您使用yagmail(全免责声明:我是开发者/维护者)。

它的主要目的之一是使附件发送和发送HTML代码变得非常简单。

默认文本/内联图像等实际上是用HTML包装的(所以默认情况下,电子邮件以HTML格式发送)。

文件名会在内容上被猜到并被添加。

只是将其全部内容填入内容,而yagmail将做到这一点。

但是,这对您来说显然非常重要,它也已经设置了“替代”mimeparts。没有人提出要求,所以如果您发现任何问题,可以真正使用您的帮助来提及github跟踪器上的错误。

import yagmail 
yag = yagmail.SMTP('[email protected]', 'password') 

contents = ['/local/file.png', 'some text', 
      '<b><a href="https://github.com/kootenpv/yagmail">a link</a></b>'] 

yag.send(to='[email protected]', subject='Title', contents = contents) 

您还可以将其设置为无密码,那么你就可以登录,而无需在脚本的用户名/密码,即

yag = yagmail.SMTP() 

希望能有帮助!

大多数问题都可以通过阅读GitHub上的自述来回答:https://github.com/kootenpv/yagmail

+0

感谢您认真的努力来帮助PascalvKooten。不幸的是,尽管你解决了我在纯文本邮件上回车/换行的问题,但yagmail至少有3个错误(我尽可能在github上报告了他们),我发现哪些阻止我以我想要的方式使用它只是还没有。 – Rodent

+0

仅供参考,我现在使用yagmail进行纯文本电子邮件,并且效果很好;谢谢。现在我已经放弃了对mime/alternate的支持,所以这对我来说不再是一个悬而未决的问题。 – Rodent

相关问题