need guidance on sending emails with attachment with python.

Discussion in 'Python' started by krishnakant Mane, Dec 10, 2006.

  1. hello,
    I am a bit confused.
    I want to make a program that will take some data from a database and
    make a string of text. and send it to the respective email id of a
    person.
    next I also want to send an attachment of a photo along with the email.
    I will be required to make this program run on windows xp.
    can some one guide me as to how I can achieve this?
    what I will need on windows to send emails?
    I believe yahoo, gmail, rediff and msn supports pop3 and smtp?
    correct me if I am wrong on this.
    secondly what library is used on windows to do this?
    I know there must be python libraries to do this,
    but apart from that I don't know what all I will need on my windows
    machine to send emails to mostly yahoo, gmail and msn.
    Please give me some idea about it.
    Krishnakant.
    krishnakant Mane, Dec 10, 2006
    #1
    1. Advertising

  2. krishnakant Mane

    Bernard Guest

    here's the function I've been using for while :p

    import smtplib
    from email.MIMEMultipart import MIMEMultipart
    from email.MIMEBase import MIMEBase
    from email.MIMEText import MIMEText
    from email.Utils import COMMASPACE, formatdate
    from email import Encoders

    def sendMail(arrRecipients, sender, subject, message, files=[]):
    """ Sends email with attachements """
    # SMTP address
    smtpserver = '' # provide a smtp here in string format
    # authentification section
    AUTHREQUIRED = 0 # if you need to use SMTP AUTH set to 1
    smtpuser = '' # for SMTP AUTH, set SMTP username here
    smtppass = '' # for SMTP AUTH, set SMTP password here

    # Building the body of the email
    mssg = MIMEMultipart()
    mssg['From'] = sender
    mssg['To'] = COMMASPACE.join(arrRecipients)
    mssg['Date'] = formatdate(localtime=True)
    mssg['Subject'] = subject
    mssg.attach( MIMEText(message) )

    # attachments
    for file in files:
    part = MIMEBase('application', "octet-stream")
    part.set_payload( open(file,"rb").read() )
    Encoders.encode_base64(part)
    part.add_header('Content-Disposition', 'attachment;
    filename="%s"' % os.path.basename(file))
    mssg.attach(part)

    session = smtplib.SMTP(smtpserver)
    if AUTHREQUIRED:
    session.login(smtpuser, smtppass)
    smtpresult = session.sendmail(sender, arrRecipients,
    mssg.as_string())

    if smtpresult:
    errstr = ""
    for recip in smtpresult.keys():
    errstr = """Could not delivery mail to: %s

    Server said: %s
    %s

    %s""" % (recip, smtpresult[recip][0], smtpresult[recip][1], errstr)
    raise smtplib.SMTPException, errstr

    krishnakant Mane a écrit :

    > hello,
    > I am a bit confused.
    > I want to make a program that will take some data from a database and
    > make a string of text. and send it to the respective email id of a
    > person.
    > next I also want to send an attachment of a photo along with the email.
    > I will be required to make this program run on windows xp.
    > can some one guide me as to how I can achieve this?
    > what I will need on windows to send emails?
    > I believe yahoo, gmail, rediff and msn supports pop3 and smtp?
    > correct me if I am wrong on this.
    > secondly what library is used on windows to do this?
    > I know there must be python libraries to do this,
    > but apart from that I don't know what all I will need on my windows
    > machine to send emails to mostly yahoo, gmail and msn.
    > Please give me some idea about it.
    > Krishnakant.
    Bernard, Dec 11, 2006
    #2
    1. Advertising

  3. On 11 Dec 2006 04:54:04 -0800, Bernard <> wrote:
    > here's the function I've been using for while :p
    >

    thanks indeed for the function Bernard.
    I will have a windows machine up tomorrow. but do I need to install
    any thing over and above the python libraries needed?
    is it necessary that I install outlook or do I need to install any
    server/ client on the machine where your function is to be used?
    > import smtplib
    > from email.MIMEMultipart import MIMEMultipart
    > from email.MIMEBase import MIMEBase
    > from email.MIMEText import MIMEText
    > from email.Utils import COMMASPACE, formatdate
    > from email import Encoders
    >
    > def sendMail(arrRecipients, sender, subject, message, files=[]):
    > """ Sends email with attachements """
    > # SMTP address
    > smtpserver = '' # provide a smtp here in string format
    > # authentification section
    > AUTHREQUIRED = 0 # if you need to use SMTP AUTH set to 1
    > smtpuser = '' # for SMTP AUTH, set SMTP username here
    > smtppass = '' # for SMTP AUTH, set SMTP password here
    >
    > # Building the body of the email
    > mssg = MIMEMultipart()
    > mssg['From'] = sender
    > mssg['To'] = COMMASPACE.join(arrRecipients)
    > mssg['Date'] = formatdate(localtime=True)
    > mssg['Subject'] = subject
    > mssg.attach( MIMEText(message) )
    >
    > # attachments
    > for file in files:
    > part = MIMEBase('application', "octet-stream")
    > part.set_payload( open(file,"rb").read() )
    > Encoders.encode_base64(part)
    > part.add_header('Content-Disposition', 'attachment;
    > filename="%s"' % os.path.basename(file))
    > mssg.attach(part)
    >
    > session = smtplib.SMTP(smtpserver)
    > if AUTHREQUIRED:
    > session.login(smtpuser, smtppass)
    > smtpresult = session.sendmail(sender, arrRecipients,
    > mssg.as_string())
    >
    > if smtpresult:
    > errstr = ""
    > for recip in smtpresult.keys():
    > errstr = """Could not delivery mail to: %s
    >
    > Server said: %s
    > %s
    >
    > %s""" % (recip, smtpresult[recip][0], smtpresult[recip][1], errstr)
    > raise smtplib.SMTPException, errstr
    >
    > krishnakant Mane a écrit :
    >
    > > hello,
    > > I am a bit confused.
    > > I want to make a program that will take some data from a database and
    > > make a string of text. and send it to the respective email id of a
    > > person.
    > > next I also want to send an attachment of a photo along with the email.
    > > I will be required to make this program run on windows xp.
    > > can some one guide me as to how I can achieve this?
    > > what I will need on windows to send emails?
    > > I believe yahoo, gmail, rediff and msn supports pop3 and smtp?
    > > correct me if I am wrong on this.
    > > secondly what library is used on windows to do this?
    > > I know there must be python libraries to do this,
    > > but apart from that I don't know what all I will need on my windows
    > > machine to send emails to mostly yahoo, gmail and msn.
    > > Please give me some idea about it.
    > > Krishnakant.

    >
    >
    >
    krishnakant Mane, Dec 11, 2006
    #3
  4. krishnakant Mane wrote:
    > On 11 Dec 2006 04:54:04 -0800, Bernard <> wrote:
    > > here's the function I've been using for while :p
    > >

    > thanks indeed for the function Bernard.
    > I will have a windows machine up tomorrow. but do I need to install
    > any thing over and above the python libraries needed?
    > is it necessary that I install outlook or do I need to install any
    > server/ client on the machine where your function is to be used?
    > > import smtplib
    > > from email.MIMEMultipart import MIMEMultipart
    > > from email.MIMEBase import MIMEBase
    > > from email.MIMEText import MIMEText
    > > from email.Utils import COMMASPACE, formatdate
    > > from email import Encoders
    > >
    > > def sendMail(arrRecipients, sender, subject, message, files=[]):
    > > """ Sends email with attachements """
    > > # SMTP address
    > > smtpserver = '' # provide a smtp here in string format
    > > # authentification section
    > > AUTHREQUIRED = 0 # if you need to use SMTP AUTH set to 1
    > > smtpuser = '' # for SMTP AUTH, set SMTP username here
    > > smtppass = '' # for SMTP AUTH, set SMTP password here
    > >
    > > # Building the body of the email
    > > mssg = MIMEMultipart()
    > > mssg['From'] = sender
    > > mssg['To'] = COMMASPACE.join(arrRecipients)
    > > mssg['Date'] = formatdate(localtime=True)
    > > mssg['Subject'] = subject
    > > mssg.attach( MIMEText(message) )
    > >
    > > # attachments
    > > for file in files:
    > > part = MIMEBase('application', "octet-stream")
    > > part.set_payload( open(file,"rb").read() )
    > > Encoders.encode_base64(part)
    > > part.add_header('Content-Disposition', 'attachment;
    > > filename="%s"' % os.path.basename(file))
    > > mssg.attach(part)
    > >
    > > session = smtplib.SMTP(smtpserver)
    > > if AUTHREQUIRED:
    > > session.login(smtpuser, smtppass)
    > > smtpresult = session.sendmail(sender, arrRecipients,
    > > mssg.as_string())
    > >
    > > if smtpresult:
    > > errstr = ""
    > > for recip in smtpresult.keys():
    > > errstr = """Could not delivery mail to: %s
    > >
    > > Server said: %s
    > > %s
    > >
    > > %s""" % (recip, smtpresult[recip][0], smtpresult[recip][1], errstr)
    > > raise smtplib.SMTPException, errstr
    > >
    > > krishnakant Mane a écrit :
    > >
    > > > hello,
    > > > I am a bit confused.
    > > > I want to make a program that will take some data from a database and
    > > > make a string of text. and send it to the respective email id of a
    > > > person.
    > > > next I also want to send an attachment of a photo along with the email.
    > > > I will be required to make this program run on windows xp.
    > > > can some one guide me as to how I can achieve this?
    > > > what I will need on windows to send emails?
    > > > I believe yahoo, gmail, rediff and msn supports pop3 and smtp?
    > > > correct me if I am wrong on this.
    > > > secondly what library is used on windows to do this?
    > > > I know there must be python libraries to do this,
    > > > but apart from that I don't know what all I will need on my windows
    > > > machine to send emails to mostly yahoo, gmail and msn.
    > > > Please give me some idea about it.
    > > > Krishnakant.

    > >
    > >
    > >


    You will need some working email account.
    It could be a server on your local machine or it could be a gmail
    account.
    If you don't have an email account that you want to send from, then you
    will have to either obtain one or create your own server.
    Richard Charts, Dec 11, 2006
    #4
  5. krishnakant Mane

    ina Guest

    I put to gether a class to deal with this along time ago it might be of
    help to you.
    You can find it at
    http://phlik.ishpeck.net/index.php?P=b1114201575phlik.

    krishnakant Mane wrote:
    > hello,
    > I am a bit confused.
    > I want to make a program that will take some data from a database and
    > make a string of text. and send it to the respective email id of a
    > person.
    > next I also want to send an attachment of a photo along with the email.
    > I will be required to make this program run on windows xp.
    > can some one guide me as to how I can achieve this?
    > what I will need on windows to send emails?
    > I believe yahoo, gmail, rediff and msn supports pop3 and smtp?
    > correct me if I am wrong on this.
    > secondly what library is used on windows to do this?
    > I know there must be python libraries to do this,
    > but apart from that I don't know what all I will need on my windows
    > machine to send emails to mostly yahoo, gmail and msn.
    > Please give me some idea about it.
    > Krishnakant.
    ina, Dec 12, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Natty Gur
    Replies:
    0
    Views:
    370
    Natty Gur
    Aug 19, 2003
  2. Wim Hollebrandse

    Re: Sending multiple emails (1000+) using dotnet

    Wim Hollebrandse, Aug 19, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    398
    Wim Hollebrandse
    Aug 19, 2003
  3. sridhar

    sending emails using python

    sridhar, Sep 7, 2006, in forum: Python
    Replies:
    14
    Views:
    1,108
    Tim Williams
    Sep 14, 2006
  4. Brendon Towle

    Re: sending emails using python

    Brendon Towle, Sep 7, 2006, in forum: Python
    Replies:
    1
    Views:
    450
    sridhar
    Sep 14, 2006
  5. NetNoise
    Replies:
    6
    Views:
    365
    NetNoise
    Jan 10, 2005
Loading...

Share This Page