Sending mail with attachment...

M

Merrigan

Hi,

I have now eventually finished my newbie-backup script and I'm very
proud of the way it functions...

Anyways, I am looking for an easy way to use smtplib to send an email
with the output log of the script to multiple accounts. I need to use
it with a smtp server, and cannot pipe it directly to sendmail.

I have tried about 50 different ways that I have googled for in the
last 6 hours, but none of them work, I keep on getting errors.

The script runs on a Linux system.

Thanks for any help.
 
M

Merrigan

Hi Guys,

I just wanted to post the error, and then also the coda that causes
it....I get this no matter what I do to this, and I don't understand
it. Any help would be welcome. Thank you

Error
::
Traceback (most recent call last):
File "/home/merrigan/projects/Backup Script/hobbitarchive.py", line
113, in ?
mailSender()
File "/home/merrigan/projects/Backup Script/hobbitarchive.py", line
74, in mailSender
s.sendmail(fromaddy, recievelist, mesg.as_string())
File "/usr/lib/python2.4/email/Message.py", line 129, in as_string
g.flatten(self, unixfrom=unixfrom)
File "/usr/lib/python2.4/email/Generator.py", line 82, in flatten
self._write(msg)
File "/usr/lib/python2.4/email/Generator.py", line 120, in _write
self._write_headers(msg)
File "/usr/lib/python2.4/email/Generator.py", line 166, in
_write_headers
header_name=h, continuation_ws='\t').encode()
File "/usr/lib/python2.4/email/Header.py", line 395, in encode
return self._encode_chunks(newchunks, maxlinelen)
File "/usr/lib/python2.4/email/Header.py", line 355, in
_encode_chunks
_max_append(chunks, s, maxlinelen, extra)
File "/usr/lib/python2.4/email/quopriMIME.py", line 79, in
_max_append
L.append(s.lstrip())
AttributeError: 'list' object has no attribute 'lstrip'

::

Code
::
def mailSender():
openlogmsg = open(completelog, 'rb')
mesg = MIMEText(openlogmsg.read())
openlogmsg.close()
mesg['Subject'] = subject
mesg['From'] = fromaddy
mesg['To'] = recievelist
s = smtplib.SMTP(smtpserver)
# s.connect(smtpserver)
s.sendmail(fromaddy, recievelist, mesg.as_string())
s.quit()

::

Thanks a lot :)
 
T

Tim Williams

::

Code
::
def mailSender():
openlogmsg = open(completelog, 'rb')
mesg = MIMEText(openlogmsg.read())
openlogmsg.close()
mesg['Subject'] = subject
mesg['From'] = fromaddy
mesg['To'] = recievelist
s = smtplib.SMTP(smtpserver)
# s.connect(smtpserver)
s.sendmail(fromaddy, recievelist, mesg.as_string())
s.quit()

You are passing a list to the "To:" header, it should be a string.

The "To" header needs to be a text representation of some/all/None of
the recipients, or it can contain unrelated text, its a field that
the mail client uses to display the email details, it is not related
to the sending of the email.

The recievelist used during the sendmail function should contain the
list of recipient address(es), which means all the TO , CC and BCC
addresses for that email - or it can be a string of 1 address if you
are on a recent version of Python and there is only 1 recipient.

HTH :)
 
J

John Salerno

Merrigan said:
mesg['To'] = recievelist

It's already been pointed out that this is a problem, but I just wanted
to mention also that 'recieve' is misspelled, in case that affects
anything, or others who might read/use the code.
 
M

Marcelo Ramos

Merrigan escribió:
Hi,

I have now eventually finished my newbie-backup script and I'm very
proud of the way it functions...

Anyways, I am looking for an easy way to use smtplib to send an email
with the output log of the script to multiple accounts. I need to use
it with a smtp server, and cannot pipe it directly to sendmail.

I have tried about 50 different ways that I have googled for in the
last 6 hours, but none of them work, I keep on getting errors.

The script runs on a Linux system.

Thanks for any help.

I have a python script in production doing what you want using smtplib
and it works perfectly. Send us your code and the errors you are getting.


Regards.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top