473,383 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

need guidance on sending emails with attachment with python.

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.
Dec 10 '06 #1
4 3065
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.
Dec 11 '06 #2
On 11 Dec 2006 04:54:04 -0800, Bernard <be***********@gmail.comwrote:
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.


Dec 11 '06 #3
krishnakant Mane wrote:
On 11 Dec 2006 04:54:04 -0800, Bernard <be***********@gmail.comwrote:
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.

Dec 11 '06 #4
ina
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.
Dec 12 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ReidarT | last post by:
I got som pretty good help with code for sending emails to 3 persons from Ken Tucker. What I miss now is how to send an attachment with the email. regards reidarT
1
by: JohnRHarlow | last post by:
Hi: I am looking for advice on the best way to set up a process to read incoming emails (from a normal unix mailbox on the same host) containing a gzipped telemetry attachment. I'd like the...
5
by: Kun | last post by:
i have the following code: ---------------------------------- import smtplib from email.MIMEText import MIMEText fp = open('confirmation.txt', 'rb') msg = MIMEText(fp.read()) From =...
7
by: Ray Booysen | last post by:
Hi all I'm sending email via ASP.NET in HTML mode. Each email has exactly one attachment and I do have full access to the SMTP server. However, if I send the email in HTML format, the...
0
by: manevski ognjen | last post by:
Hi, I have one problem with console application and threading. In my SMtp class I have method that sends emmails with attachments with html text. Program should work with, let say 10 emails,...
2
by: Robson Siqueira | last post by:
Folks, I've been experiencing a weird problem. I have a web service which sends an email after its processing. The email always has an PDF attachment on it. The PDF size vary overtime. The...
0
by: damimkader | last post by:
Hi, I'm trying to send emails using a Macro based on an Excel Sheet and the Email Client I'm using is Lotus Notes. OS used - Windows Xp. Language - VB Below is the Code I'm using for doing...
3
by: =?Utf-8?B?TWlrZQ==?= | last post by:
Hi, I'm using the bellow code to send emails in a Website that I uploaded in one of my client's server where it has SMTP installed, BUT some users are receiving email some others aren't !!?So,...
1
maliksleo
by: maliksleo | last post by:
hi i am using the following class for email sending but getting this error "Failure sending mail" Imports System.Net.Mail Public Class MailHelper ''' <summary> ''' Sends an mail...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.