简体   繁体   中英

how to create a mbox email file using python

I have a mysql database with email addresses and other data. I need to create specific emails which could be sent to the addresses in the db. The type of email will vary depending on the fields on the db. Each email message will have a pdf attachment taken from a path field in the db. As the emails need to be sent from another computer I have thought of creating an mbox file containing the messages and attachments as base64 ASCII text, which can then be opened in an email program such as evolution and sent from the other computer. I need to get advice and possible examples of how this can be done using python and mysql. Examples of how to convert a email address, message body text and pdf to a ASCII text mbox file would be ideal. Thanks.

要处理 mbox 文件,您可以使用mailbox模块: http : //docs.python.org/2/library/mailbox.html但您的问题不够精确,似乎您只需要发送电子邮件 - 为此,创建mbox 文件不是必需的,为了让您的生活更轻松,您可以使用以下内容: http : //www.mailgun.com/

To create and manage .mbox files, you can try to use a third-party library Aspose.Email for Python .

Look at the example below. This approach can help you create .mbox files.

# Initialize MboxStorageWriter and pass the above stream to it
writer = MboxrdStorageWriter(dataDir + "ExampleMBox_out.mbox", False)

# Prepare a new message using the MailMessage class
message = MailMessage("from@domain.com", "to@domain.com", "Eml generated for Mbox", "added from Aspose.Email for Python")
message.is_draft = False

# Add this message to storage
writer.write_message(message)

# Close all related streams
writer.dispose();

To see more examples, you can go to the official repository .

I am a Developer Evangelist at Aspose.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM