Sending mail with attachment...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Merrigan

    #1

    Sending mail with attachment...

    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.

  • Merrigan

    #2
    Re: Sending mail with attachment...

    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.p y", line
    113, in ?
    mailSender()
    File "/home/merrigan/projects/Backup Script/hobbitarchive.p y", line
    74, in mailSender
    s.sendmail(from addy, recievelist, mesg.as_string( ))
    File "/usr/lib/python2.4/email/Message.py", line 129, in as_string
    g.flatten(self, unixfrom=unixfr om)
    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_hea ders(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_ch unks(newchunks, maxlinelen)
    File "/usr/lib/python2.4/email/Header.py", line 355, in
    _encode_chunks
    _max_append(chu nks, s, maxlinelen, extra)
    File "/usr/lib/python2.4/email/quopriMIME.py", line 79, in
    _max_append
    L.append(s.lstr ip())
    AttributeError: 'list' object has no attribute 'lstrip'

    ::

    Code
    ::
    def mailSender():
    openlogmsg = open(completelo g, 'rb')
    mesg = MIMEText(openlo gmsg.read())
    openlogmsg.clos e()
    mesg['Subject'] = subject
    mesg['From'] = fromaddy
    mesg['To'] = recievelist
    s = smtplib.SMTP(sm tpserver)
    # s.connect(smtps erver)
    s.sendmail(from addy, recievelist, mesg.as_string( ))
    s.quit()

    ::

    Thanks a lot :)

    Comment

    • Tim Williams

      #3
      Re: Sending mail with attachment...

      On 13 May 2006 03:13:33 -0700, Merrigan <charl.loubser@ gmail.com> wrote:
      [color=blue]
      > ::
      >
      > Code
      > ::
      > def mailSender():
      > openlogmsg = open(completelo g, 'rb')
      > mesg = MIMEText(openlo gmsg.read())
      > openlogmsg.clos e()
      > mesg['Subject'] = subject
      > mesg['From'] = fromaddy
      > mesg['To'] = recievelist
      > s = smtplib.SMTP(sm tpserver)
      > # s.connect(smtps erver)
      > s.sendmail(from addy, recievelist, mesg.as_string( ))
      > s.quit()[/color]

      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 :)

      Comment

      • rory.brandybuck@gmail.com

        #4
        Re: Sending mail with attachment...

        Replace:
        mesg['To'] = recievelist
        By:
        mesg['To'] = ', '.join(recievel ist)

        Comment

        • John Salerno

          #5
          Re: Sending mail with attachment...

          Merrigan wrote:
          [color=blue]
          > mesg['To'] = recievelist[/color]

          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.

          Comment

          • Marcelo Ramos

            #6
            Re: Sending mail with attachment...

            Merrigan escribió:[color=blue]
            > 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.
            >
            >[/color]

            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.

            --
            Marcelo Ramos
            Fedora Core 5 | 2.6.16
            Socio UYLUG Nro 125

            Comment

            Working...