Skip to content

Instantly share code, notes, and snippets.

@rajajawahar
Last active March 13, 2019 09:03
Show Gist options
  • Save rajajawahar/b1251a914e75de1c7ed956f74ee9e65d to your computer and use it in GitHub Desktop.
Save rajajawahar/b1251a914e75de1c7ed956f74ee9e65d to your computer and use it in GitHub Desktop.

Revisions

  1. rajajawahar revised this gist Mar 13, 2019. 1 changed file with 39 additions and 13 deletions.
    52 changes: 39 additions & 13 deletions sendmail.py
    Original file line number Diff line number Diff line change
    @@ -5,25 +5,51 @@
    from email.utils import formatdate
    from email import encoders

    def send_mail(send_from,send_to,subject,text,files,server,port,username,password,isTls=True):
    def send_mail(fromaddress,toaddress,subject,text,files):

    # instance of MIMEMultipart
    msg = MIMEMultipart()
    msg['From'] = send_from
    msg['To'] = ','.join(send_to)
    # storing the senders email address
    msg['From'] = fromaddress
    # storing the receivers email address
    msg['To'] = toaddress
    # storing the date
    msg['Date'] = formatdate(localtime = True)
    # storing the subject
    msg['Subject'] = subject
    # attach the body with the msg instance
    msg.attach(MIMEText(text))

    # open the file to be sent
    filename = "User Activity.xlsx"
    attachment = open(files, "rb")

    # instance of MIMEBase and named as part
    part = MIMEBase('application', "octet-stream")
    part.set_payload(open(files, "rb").read())
    # To change the payload into encoded form
    part.set_payload((attachment).read())

    # encode into base64
    encoders.encode_base64(part)
    part.add_header('Content-Disposition', 'attachment; filename="User Activity.xlsx"')

    part.add_header('Content-Disposition', "attachment; filename= %s" % filename)

    msg.attach(part)

    #context = ssl.SSLContext(ssl.PROTOCOL_SSLv3)
    #SSL connection only working on Python 3+
    smtp = smtplib.SMTP(server, port)
    if isTls:
    smtp.starttls()
    smtp.login(username,password)
    smtp.sendmail(send_from, send_to, msg.as_string())
    smtp.quit()
    # creates SMTP session
    s = smtplib.SMTP('smtp.gmail.com', 587)

    # start TLS for security
    s.starttls()

    # Authentication
    s.login(fromaddress, "Your Password")

    # Converts the Multipart msg into a string
    text = msg.as_string()

    # sending the mail
    s.sendmail(fromaddress, toaddress, text)

    # terminating the session
    s.quit()
  2. rajajawahar created this gist Mar 4, 2019.
    29 changes: 29 additions & 0 deletions sendmail.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    import smtplib,ssl
    from email.mime.multipart import MIMEMultipart
    from email.mime.base import MIMEBase
    from email.mime.text import MIMEText
    from email.utils import formatdate
    from email import encoders

    def send_mail(send_from,send_to,subject,text,files,server,port,username,password,isTls=True):
    msg = MIMEMultipart()
    msg['From'] = send_from
    msg['To'] = ','.join(send_to)
    msg['Date'] = formatdate(localtime = True)
    msg['Subject'] = subject
    msg.attach(MIMEText(text))

    part = MIMEBase('application', "octet-stream")
    part.set_payload(open(files, "rb").read())
    encoders.encode_base64(part)
    part.add_header('Content-Disposition', 'attachment; filename="User Activity.xlsx"')
    msg.attach(part)

    #context = ssl.SSLContext(ssl.PROTOCOL_SSLv3)
    #SSL connection only working on Python 3+
    smtp = smtplib.SMTP(server, port)
    if isTls:
    smtp.starttls()
    smtp.login(username,password)
    smtp.sendmail(send_from, send_to, msg.as_string())
    smtp.quit()