Demo entry 5862478

How to send email in python using gmail, yahoo, hotmail?


Submitted by anonymous on Aug 06, 2016 at 07:01
Language: Python. Code size: 2.0 kB.

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.MIMEText import MIMEText
from email.MIMEBase import MIMEBase
from email import Encoders

#                       chandan kumar ojha                               #
#                   Please don't change in code                          #
#                   It will reads to raise error                         #

class Email(object):

    def __init__(self,to,eFrom,subject,body,Files=[]):
        msg = MIMEMultipart()
        msg['From'] = eFrom
        msg['To'] = to
        msg['Subject'] = subject
        msg.attach(MIMEText(body, 'plain'))

        for f in Files:
            #part = ('application',"octet-stream")
            part = MIMEApplication(open(f,'rb').read())
            part.add_header('Content-Disposition', 'attachment', filename=str(os.path.basename(f)))
        self._message = msg

    def sendEmail(self,host,port,username,password):
        server = smtplib.SMTP(host, port)

# example To send email- Uncomment below line to send email
if __name__ == 'main':
    Attachmets = ['c:\\Directory Name\\sub Directory\\File Name.Ext', 'c:\\Directory Name\\sub Directory\\File2 Name.Ext']
    mail = Email("","","test-SubjectLine","this is a body Text",Attachmets)

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).