473,549 Members | 2,758 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sending mailing list with smtplib

Hi all,

I tried to send a small mailing list using python today (2036 emails).
For that purpose I looked in the doc for some code examples (I had
never done it before). And I ended up writing and running this little
script:

# list.txt is a text file container an email per line
fp=open('list.t xt','r')
list=fp.readlin es()
fp.close()

# message.txt is the plaine text message to send
textfile='messa ge.txt'
subject='[xsbar] alive and kicking'
me********@xsba r.com'

# rom and example in the doc
import smtplib

from email.MIMEText import MIMEText

fp = open(textfile, 'rb')
msg = MIMEText(fp.rea d())
fp.close()

# ... but the actual message sending in a loop to send one email each
(didn't work)
for line in list:
you=line[:-1]
msg['Subject'] = subject
msg['From'] = me
msg['To'] = you

s = smtplib.SMTP()
s.connect()
s.sendmail(me, [you], msg.as_string() )
s.close()

but it copied all the recipients in the header (To:) which I don't
understand?

Can someone help me understand what I did wrong?

Thanks,

EuGeNe

Aug 14 '06 #1
7 2349
At Monday 14/8/2006 10:22, 3KWA wrote:
># ... but the actual message sending in a loop to send one email each
(didn't work)

Specify "didn't work" at least... see
http://www.catb.org/~esr/faqs/smart-questions.html

Gabriel Genellina
Softlab SRL

_______________ _______________ _______________ _____
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas

Aug 14 '06 #2

Gabriel Genellina wrote:
>
Specify "didn't work" at least... see
http://www.catb.org/~esr/faqs/smart-questions.html
Ooops, didn't work explained (the worst is I bought and read ESR's book
:()

list.txt=
email1
email2
email3
....
emailn

email1 received from:xsbar.com to:email1 subject: [xsbar] alive and
kicking ... message

from email2 onwards it seems what happened is that the recipient list
kept growing,
email 2 received from:eu****@xsb ar.com to:email1 to:email2 subject: ...
email3 received from:eu****@xsb ar.com to:email1 to:email2 to:email3
subject:...
....
emailn received from:eu****@xsb ar.com to:email1 to:email2 to:email3 ...
to:emailn subject:...

Many didn't receive the email because the header grew too big (I
received 1257 failure notice ~50% of them for header too big or
inappropriate recipient list).

Apologize for the inaccuracy of my first post

Aug 15 '06 #3
3KWA wrote:
Gabriel Genellina wrote:
>>Specify "didn't work" at least... see
http://www.catb.org/~esr/faqs/smart-questions.html


Ooops, didn't work explained (the worst is I bought and read ESR's book
:()

list.txt=
email1
email2
email3
....
emailn

email1 received from:xsbar.com to:email1 subject: [xsbar] alive and
kicking ... message

from email2 onwards it seems what happened is that the recipient list
kept growing,
email 2 received from:eu****@xsb ar.com to:email1 to:email2 subject: ...
email3 received from:eu****@xsb ar.com to:email1 to:email2 to:email3
subject:...
....
emailn received from:eu****@xsb ar.com to:email1 to:email2 to:email3 ...
to:emailn subject:...

Many didn't receive the email because the header grew too big (I
received 1257 failure notice ~50% of them for header too big or
inappropriate recipient list).

Apologize for the inaccuracy of my first post
OK, now the problem is that you clearly aren't running the code you
posted, you are running *somethinglike* the code you posted, but that
has (an) error(s) the code you posted didn't.

So let's see the version with the "print" statements in it, not the
sanitised version you didn't copy-and-paste fro your python source :-)

For the record, it's clear that youa ren't resetting the senders to an
empty list each time but growing it as you go round the loop. If this
helps you find your error, at least confirm that you did indeed find the
problem.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Aug 15 '06 #4
Steve Holden wrote:
OK, now the problem is that you clearly aren't running the code you
posted, you are running *somethinglike* the code you posted, but that
has (an) error(s) the code you posted didn't.
All I did was changed the comments to make them more relevant to my
problem but here it is (not sanitized :P)

fp=open('list.t xt','r')
list=fp.readlin es()
fp.close()

textfile='messa ge.txt'
subject='[xsbar] alive and kicking'
me********@xsba r.com'

# Import smtplib for the actual sending function
import smtplib

# Import the email modules we'll need
from email.MIMEText import MIMEText

# Open a plain text file for reading. For this example, assume that
# the text file contains only ASCII characters.
fp = open(textfile, 'rb')
# Create a text/plain message
msg = MIMEText(fp.rea d())
fp.close()

# me == the sender's email address
# you == the recipient's email address
for line in list:
you=line[:-1]
msg['Subject'] = subject
msg['From'] = me
msg['To'] = you

# Send the message via our own SMTP server, but don't include the
# envelope header.
s = smtplib.SMTP()
s.connect()
s.sendmail(me, [you], msg.as_string() )
s.close()
print you,
So let's see the version with the "print" statements in it, not the
sanitised version you didn't copy-and-paste fro your python source :-)
Yes there was a print statement at the end so I could follow the
process
For the record, it's clear that youa ren't resetting the senders to an
empty list each time but growing it as you go round the loop. If this
helps you find your error, at least confirm that you did indeed find the
problem.
I must be punching way out of my league here but it is not clear to me.
Why do I need to reset anything when all I seem to be doing is straight
assignation:
1) msg['To']=you
2) [you] in the sendmail call

In order to try to figure it out I did:

list=['a','b','c']

from email.MIMEText import MIMEText

msg=MIMEText('m essage')

for l in list:
msg['Subject']='subject'
msg['From']='me'
msg['To']=l
print msg.as_string()

Which outputs:

Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Subject: subject
From: me
To: a

message
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Subject: subject
From: me
To: a
Subject: subject
From: me
To: b

message
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Subject: subject
From: me
To: a
Subject: subject
From: me
To: b
Subject: subject
From: me
To: c

message

I realized too late that it is what was happenig but I am afraid I
don't understand why it is happening?

What would be the best way to go about it then? Instantiate a new msg
in the loop?

I guess I must read the doc more carefully, thanks for your time (if
you can spare some more I would be grateful).

Regards,

EuGeNe

Aug 15 '06 #5
On 15 Aug 2006 13:41:53 -0700, 3KWA <eu****@boardku lture.comwrote:
What would be the best way to go about it then? Instantiate a new msg
in the loop?

I guess I must read the doc more carefully, thanks for your time (if
you can spare some more I would be grateful).
You can reuse your message object, but you need to delete the old
header before setting a new one:

<http://docs.python.org/lib/module-email.Message.h tml#l2h-3843>
--
filip salomonsson
Aug 15 '06 #6
Filip Salomonsson wrote:
>
You can reuse your message object, but you need to delete the old
header before setting a new one:

<http://docs.python.org/lib/module-email.Message.h tml#l2h-3843>
Thanks!

I am only an occasional Python programmer (love it though). I guess it
is the first time I faced a case where what I thought the code would do
from reading it didn't match what the code is effectively doing i.e.

msg['To']=email1 appending to the header instead of setting the To
field to email. The way my brain works if I wanted to have several
recipient I would have done something like msg['To'].append(email2) .

Just for education purposes (mine I guess :P) what was the idea behind
that design decision?

Thanks again for your assistance,

EuGeNe

Aug 16 '06 #7

3KWA wrote:
Just for education purposes (mine I guess :P) what was the idea behind
that design decision?
>From the doc (self education :P)
The following methods implement a mapping-like interface for accessing
the message's RFC 2822 headers. Note that there are some semantic
differences between these methods and a normal mapping (i.e.
dictionary) interface. For example, in a dictionary there are no
duplicate keys, but here there may be duplicate message headers. Also,
in dictionaries there is no guaranteed order to the keys returned by
keys(), but in a Message object, headers are always returned in the
order they appeared in the original message, or were added to the
message later. Any header deleted and then re-added are always appended
to the end of the header list.

These semantic differences are intentional and are biased toward
maximal convenience.

Aug 16 '06 #8

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

Similar topics

2
4517
by: Stephen Boulet | last post by:
Is it possible to send a file as an attachment using smtplib? -- Stephen If your desktop gets out of control easily, you probably have too much stuff on it that doesn't need to be there. Donna Smallin, "Unclutter Your Home"
3
1634
by: praba kar | last post by:
Dear All, In Php we can do all the mailing operations like sending a text as a message, adding attachment to a mail, adding additional headers and so on using Mail_mime class. I want to know like that class or modules in Python. I already gone through MimeWriter,smtplib and so on. But I can't get clear details. so if anyone know regarding...
0
522
by: praba kar | last post by:
Dear All, I have doubt regarding mail sending smtplib module. The below code is I used to send a mail. ########################################## import email.Message import email.Utils import mimetypes import os,string
9
3107
by: Leo Breebaart | last post by:
I am writing a utility in Python and I'd like to add a command-line option "--mailto <address>" that will cause an e-mail summary to be sent to <address> when the utility finishes running. My first thought was to use smtplib.sendmail(), and basically this works like a charm, except that this function expects a valid 'sender' address as a...
5
12633
by: morphex | last post by:
Hi, I have an email that's in the utf-8 encoding, and I'm getting this error message when I try to send it using smtplib: * Module smtplib, line 688, in sendmail * Module smtplib, line 485, in data * Module smtplib, line 312, in send * Module socket, line 1, in sendall
5
23773
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 = 'xxxx@xxxx.xxxx.edu'
14
9872
by: sridhar | last post by:
iam having user account on an exchangeserver. with that can i send an email using python? if iam using the following code iam getting error fromAddress = 'sridhar_kasturi@satyam.com' toAddress = 'sridhar_kasturi@satyam.com' msg = "Subject: Hello\n\nThis is the body of the message." import smtplib
2
1919
by: ornto | last post by:
Hi, I'm trying to create an application which checks a dynamic web site and on certain events sends an email to me. My problem though is with the email task. By now I made this simple test code: #prova invio email smtpserver = smtplib.SMTP(mailserver) messaggio= "Messaggio di prova" print mail print messaggio
5
3104
by: sui | last post by:
this is my code import sys, os, glob, datetime, time import smtplib ## Parameters for SMTP session port=587 SMTPserver= 'smtp.gmail.com' SMTPuser= '...@gmail.com' pw= 'fill in here' SENDER= SMTPuser
0
7444
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7954
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7467
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6039
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5085
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3497
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3478
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1054
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
755
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.