使用python 3.3生成并保存.eml文件 [英] Generating and saving an .eml file with python 3.3

查看:950
本文介绍了使用python 3.3生成并保存.eml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用标准电子邮件库生成电子邮件,并将其另存为.eml文件。
我不能理解email.generator的工作原理,因为我收到错误'AttributeError:'str'对象没有属性'写'。



<$ p $从电子邮件导入生成器
从电子邮件开始导入MIMEMultipart
来自email.mime.text import MIMEText
active_dir ='c:\\'

class Gen_Emails(object):
def __init __(self):
self.EmailGen()

def EmailGen(self):
sender ='sender'
recepiant ='recipiant'
subject ='subject'

msg = MIMEMultipart('alternative')
msg ['Subject'] = subject
msg ['From'] = sender
msg ['To'] = recepiant


html =\
< ; html>
< head>< / head>
< body>
< p> hello world< / p>
< / body>
< / html>

part = MIMEText(html,'html')

msg.attach(part)

self。 SaveToFile(msg)

def SaveToFile(self,msg):
out_file = active_dir
gen = generator.Generator(out_file)
gen.flatten(msg)

任何想法?

解决方案

你应该将打开的文件(写入模式)传递给 Generator() 。目前你只传递一个字符串,这就是当它尝试在字符串上调用 .write()时失败的原因。



所以做这样的事情:

  import os 
cwd = os.getcwd()
outfile_name = os.path.join(cwd,'message.eml')

class Gen_Emails(object):

#...

def SaveToFile(self,msg):
with open(outfile_name,'w')as outfile:
gen = generator.Generator(outfile)
gen.flatten(msg)

注意 with open(outfile_name,'w'因为outfile 在写入模式下在路径 outfile_name 中打开文件,并将文件指针指向打开的文件到 outfile 。退出之后,上下文管理器也会关闭文件。



os.path.join()将以跨平台的方式加入路径,这就是为什么你应该选择手工连接的路径。



os.getcwd()将返回您当前的工作目录。如果你想把你的文件保存在别的地方,那就把它修改一下。


I am trying to generate emails using the standard email library and save them as .eml files. I must not be understanding how email.generator works because I keep getting the error 'AttributeError: 'str' object has no attribute 'write.'

from email import generator
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
active_dir = 'c:\\'

class Gen_Emails(object):
    def __init__(self):
        self.EmailGen()

    def EmailGen(self):
        sender = 'sender'
        recepiant = 'recipiant'
        subject = 'subject'

        msg = MIMEMultipart('alternative')
        msg['Subject'] = subject
        msg['From'] = sender
        msg['To'] = recepiant


        html = """\
        <html>
            <head></head>
            <body>
                <p> hello world </p>
            </body>
        </html>
        """
        part = MIMEText(html, 'html')

        msg.attach(part)

        self.SaveToFile(msg)

    def SaveToFile(self,msg):
        out_file = active_dir
        gen = generator.Generator(out_file)
        gen.flatten(msg)

Any ideas?

解决方案

You are supposed to pass an open file (in write mode) to Generator(). Currently you pass it just a string, which is why it fails when it tries to call .write() on the string.

So do something like this:

import os
cwd = os.getcwd()
outfile_name = os.path.join(cwd, 'message.eml')

class Gen_Emails(object):    

    # ...

    def SaveToFile(self,msg):
        with open(outfile_name, 'w') as outfile:
            gen = generator.Generator(outfile)
            gen.flatten(msg)

Note: with open(outfile_name, 'w') as outfile opens the file at the path outfile_name in write mode and assigns the file pointer to the open file to outfile. The context manager also takes care of closing the file for you after you exit the with block.

os.path.join() will join paths in a cross-plattform way, which is why you should prefer it over concatenating paths by hand.

os.getcwd() will return your current working directory. If you want to your file to be saved somewhere else just change it out accordingly.

这篇关于使用python 3.3生成并保存.eml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆