Mail: Difference between revisions
Jump to navigation
Jump to search
| Line 1: | Line 1: | ||
[[Category:shell]] | |||
[[Category:python]] | |||
==Sending mail with sendmail== | ==Sending mail with sendmail== | ||
;<code>sendmail -t < <messagefile></code> | ;<code>sendmail -t < <messagefile></code> | ||
| Line 11: | Line 13: | ||
Python email sender found in [[https://docs.python.org/2/library/email-examples.html|the python docs]] | Python email sender found in [[https://docs.python.org/2/library/email-examples.html|the python docs]] | ||
<syntaxhighlight lang=python> | <syntaxhighlight lang=python> | ||
import smtplib | import smtplib | ||
| Line 25: | Line 28: | ||
mailsubject = 'What this is about' | mailsubject = 'What this is about' | ||
mailtext = "Hi,\n\nI'd like to send you an email.\n\nBest Regards\n\nMe." | mailtext = "Hi,\n\nI'd like to send you an email.\n\nBest Regards\n\nMe." | ||
sendmail(mailto,mailsubject,mailtext,attachment) | #sendmail(mailto,mailfrom,mailsubject,mailtext,attachment) | ||
sendmail(mailto, mailfrom=mailfrom, mailfrom=mailfrom, mailtext=mailtext, attachment=attachment) | |||
return | return | ||
def sendmail(mailto,mailsubject,mailtext,attachment | def sendmail(mailto,mailfrom = '',mailsubject = '',mailtext = '',attachment = ''): | ||
outer = MIMEMultipart() | outer = MIMEMultipart() | ||
outer['Subject'] = mailsubject | outer['Subject'] = mailsubject | ||
outer['To'] = mailto | outer['To'] = mailto | ||
if mailfrom != '': | |||
outer['From'] = mailfrom | |||
msg = MIMEText(mailtext,_subtype='plain') | msg = MIMEText(mailtext,_subtype='plain') | ||
outer.attach(msg) | outer.attach(msg) | ||
ctype = 'application/octet-stream' | if attachment != '': | ||
filename = attachment.split('/')[-1] | |||
ctype = 'application/octet-stream' | |||
maintype, subtype = ctype.split('/', 1) | |||
fp = open(attachment, 'rb') | |||
msg = MIMEBase(maintype, subtype) | |||
msg.set_payload(fp.read()) | |||
fp.close() | |||
# Encode the payload using Base64 | |||
encoders.encode_base64(msg) | |||
# Set the filename parameter | |||
msg.add_header('Content-Disposition', 'attachment', filename=filename) | |||
outer.attach(msg) | |||
composed = outer.as_string() | composed = outer.as_string() | ||
s = smtplib.SMTP('localhost') | s = smtplib.SMTP('localhost') | ||
s.sendmail( | s.sendmail(mailfrom, mailto , composed) | ||
s.quit() | s.quit() | ||
return | return | ||
Revision as of 20:07, 11 September 2018
Sending mail with sendmail
sendmail -t < <messagefile>- Send the email as in <messagefile>
The email headers are in <messagefile>. That should look like:
From: me@mydomain.tld
To: user@domain.tld
Subject: Subject of your mail
email text
Python email sender found in [python docs]
import smtplib
from email import encoders
from email.message import Message
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def main():
attachement = fileapath
mailto = you@domain.tld
mailsubject = 'What this is about'
mailtext = "Hi,\n\nI'd like to send you an email.\n\nBest Regards\n\nMe."
#sendmail(mailto,mailfrom,mailsubject,mailtext,attachment)
sendmail(mailto, mailfrom=mailfrom, mailfrom=mailfrom, mailtext=mailtext, attachment=attachment)
return
def sendmail(mailto,mailfrom = '',mailsubject = '',mailtext = '',attachment = ''):
outer = MIMEMultipart()
outer['Subject'] = mailsubject
outer['To'] = mailto
if mailfrom != '':
outer['From'] = mailfrom
msg = MIMEText(mailtext,_subtype='plain')
outer.attach(msg)
if attachment != '':
filename = attachment.split('/')[-1]
ctype = 'application/octet-stream'
maintype, subtype = ctype.split('/', 1)
fp = open(attachment, 'rb')
msg = MIMEBase(maintype, subtype)
msg.set_payload(fp.read())
fp.close()
# Encode the payload using Base64
encoders.encode_base64(msg)
# Set the filename parameter
msg.add_header('Content-Disposition', 'attachment', filename=filename)
outer.attach(msg)
composed = outer.as_string()
s = smtplib.SMTP('localhost')
s.sendmail(mailfrom, mailto , composed)
s.quit()
return
main()