Difference between revisions of "Mail"

From wiki
Jump to navigation Jump to search
Line 22: Line 22:
 
     attachement1 = filepath
 
     attachement1 = filepath
 
     attachement2 = filepath
 
     attachement2 = filepath
     mailto = ['you@domain.tld']        # 2.7.5 takes a list, 2.7.12 a string (comma seperated addresses)
+
     mailto = ['you@domain.tld']        # 2.7.5 takes a list, 2.7.12 a string (comma separated addresses)
 +
    mailcc =  ''
 +
    mailbcc = ['secret@domain.tld']    # Only include this in the final sendmail call, not in the headers.
 
     mailfrom = 'me@domain.tld'          # Some system do not like this, just remove it from the sendmail call
 
     mailfrom = 'me@domain.tld'          # Some system do not like this, just remove it from the sendmail call
 
     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, mailfrom=mailfrom, mailsubject=mailsubject, mailtext=mailtext, attachments=[attachment1,attachment2])
+
     sendmail(mailto, mailcc=mailcc, mailbcc=mailbcc mailfrom=mailfrom, mailsubject=mailsubject, mailtext=mailtext, attachments=[attachment1,attachment2])
 
     return
 
     return
 
   
 
   
 
   
 
   
def sendmail(mailto,mailfrom = '',mailsubject = '',mailtext = '',attachments = ''):
+
def send_mail(mailto,mailcc = '',mailbcc = '',mailfrom = '',mailsubject = '',mailtext = '',attachments = ''):
 
     import smtplib
 
     import smtplib
 
     from email import encoders
 
     from email import encoders
Line 37: Line 39:
 
     from email.mime.multipart import MIMEMultipart
 
     from email.mime.multipart import MIMEMultipart
 
     from email.mime.text import MIMEText
 
     from email.mime.text import MIMEText
 
 
     outer = MIMEMultipart()
 
     outer = MIMEMultipart()
 +
    # Set the headers
 
     outer['Subject'] = mailsubject
 
     outer['Subject'] = mailsubject
 
     outer['To'] = ",".join(mailto)
 
     outer['To'] = ",".join(mailto)
 +
    outer['Cc'] = ",".join(mailcc)
 
     if mailfrom != '':
 
     if mailfrom != '':
 
         outer['From'] = mailfrom
 
         outer['From'] = mailfrom
 
     msg = MIMEText(mailtext,_subtype='plain')
 
     msg = MIMEText(mailtext,_subtype='plain')
 +
    # Attach the message
 
     outer.attach(msg)
 
     outer.attach(msg)
 +
    # Add the attachments (if any)
 
     if attachments != '':
 
     if attachments != '':
 
         for attachment in attachments:
 
         for attachment in attachments:
Line 61: Line 66:
 
     composed = outer.as_string()
 
     composed = outer.as_string()
 
     s = smtplib.SMTP('localhost')
 
     s = smtplib.SMTP('localhost')
     s.sendmail(mailfrom, mailto , composed)
+
     s.sendmail(mailfrom, mailto+list(mailcc)+list(mailbcc) , composed)
 
     s.quit()
 
     s.quit()
 
     return
 
     return
 +
  
 
main()
 
main()
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 14:01, 30 November 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
mailx -s Subject -a <attachmentfile> email@domain.tld
Send email with attachment after entering optional text and an EOT (CTRL-D).

Python email sender found in [python docs]

def main():
    attachement1 = filepath
    attachement2 = filepath
    mailto = ['you@domain.tld']         # 2.7.5 takes a list, 2.7.12 a string (comma separated addresses)
    mailcc =  ''
    mailbcc = ['secret@domain.tld']     # Only include this in the final sendmail call, not in the headers.
    mailfrom = 'me@domain.tld'          # Some system do not like this, just remove it from the sendmail call
    mailsubject = 'What this is about'
    mailtext = "Hi,\n\nI'd like to send you an email.\n\nBest Regards\n\nMe."
    sendmail(mailto, mailcc=mailcc, mailbcc=mailbcc mailfrom=mailfrom, mailsubject=mailsubject, mailtext=mailtext, attachments=[attachment1,attachment2])
    return
 
 
def send_mail(mailto,mailcc = '',mailbcc = '',mailfrom = '',mailsubject = '',mailtext = '',attachments = ''):
    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
    outer = MIMEMultipart()
    # Set the headers
    outer['Subject'] = mailsubject
    outer['To'] = ",".join(mailto)
    outer['Cc'] = ",".join(mailcc)
    if mailfrom != '':
        outer['From'] = mailfrom
    msg = MIMEText(mailtext,_subtype='plain')
    # Attach the message
    outer.attach(msg)
    # Add the attachments (if any)
    if attachments != '':
        for attachment in attachments:
            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+list(mailcc)+list(mailbcc) , composed)
    s.quit()
    return


main()