You are not logged in.

#1 2013-10-12 23:39:44

ParanoidAndroid
Member
Registered: 2012-10-14
Posts: 114

email app in python - composing email

I'm writing a simple python script to generate email in a similar manner to mailx, i.e.

compmail.py -s subject_line -a file_to_attach recipient@example.com

I'm debating one of several ways of doing this.

1. get the subject, recipient, and attachment from the command line and create a text file to be edited in vim and then interpreted by msmtp and sent as mail.

2. do as above, but generate an email message object that will be sent using libmtp

The only benefit of the former approach is that it means I'll be able to see the whole email - the headers and body together - in vim and I'll be able to edit them easily. Essentially, such a script would pull the headers from the command line and then create a text file to be sent that included them. Even simpler, I could have it merely create a text file with empty To, From, etc. headers and fill them in with vim, and then call 'cat message_file | msmtp -a <account>' to send it. The problem with that is how to deal with attachments. How would I create a multipart MIME message with encoded attachments in such a fashion?

The second option brings up another question: as far as I can tell, one cannot simply tell python to attach a file to a message object and be done with it. Instead, one must call email.mime.MIMEx where  x is audio, text, etc. Would it be possible to just tell python to encode a file as an attachment, rather than be forced to choose a specific type of attachment?

Which approach seems best, and how would I go about solving the questions relating to that approach as listed above?

Offline

Board footer

Powered by FluxBB