librelist archives

« back to archive

Re: append function problems

Re: append function problems

From:
Robert Pearce
Date:
2011-08-25 @ 09:06
Hello.

 I'm really struggling to use the append function in the API, is it
 possible to show a working example of this function ? All i have is a
 text string (the email contents) the to and from addresses, and the
 folder name i want the mail saved in.

 any help would be useful, currently im getting errors that the
 function requires 5 inputs, but the documentation seems to only
 mention 4.

Re: [imapclient] Re: append function problems

From:
Menno Smits
Date:
2011-08-25 @ 11:46
Hi Robert,

On 2011-08-25 10:06, Robert Pearce wrote:
>   I'm really struggling to use the append function in the API, is it
>   possible to show a working example of this function ? All i have is a
>   text string (the email contents) the to and from addresses, and the
>   folder name i want the mail saved in.
>
>   any help would be useful, currently im getting errors that the
>   function requires 5 inputs, but the documentation seems to only
>   mention 4.

append() takes self, folder, msg, flags and msg_time (5). The last 2 are 
optional. This means you need to pass at least 2 arguments when calling 
append (that is, folder and msg).

 From your problem description, it sounds like you might need to turn 
the parts of the email you have (the to, from and body) into an RFC822 
style email string before passing it to append. For simple emails you 
can do this yourself but for anything more complicated it's best to use 
the email package in the standard library to do this.

Here's an (untested) example:

-------------------------------------------------------------
from_addr = 'Sender <sender@somewhere.com>'
to_addr = '"Some One" <someone@somewhere.com>'
body = 'The email body'

msg = '''\
From: %(from)s
To: %(to)s
Subject: Important

%(body)s
''' % dict(to=to_addr, from=from_addr, body=body)

imap = IMAPClient(....)
imap.login(...)
print imap.append('folder', msg)
-------------------------------------------------------------

Does that help? If you're still having trouble, please send an example 
of what you're trying to do.

Regards,
Menno

Re: [imapclient] Re: append function problems

From:
Menno Smits
Date:
2011-08-25 @ 14:34
On 2011-08-25 14:19, Robert Pearce wrote:
 > im trying to save a file to the drafts folder of googlemail, looks
 > like it's still doing the same thing.
 >
 > from_addr = 'Sender<sender@somewhere.com>'
 > to_addr = '"Some One"<someone@somewhere.com>'
 > body = 'The email body'
 > msg = '''\
 > From: %(frm)s
 > To: %(to)s
 > Subject: Important
 >
 > %(body)s
 > ''' % dict(to=to_addr, frm=from_addr, body=body)
 >
 > print srv.append('[Google Mail]/Drafts', msg)
 >
 > --------------------------------------------------------------
 >
>
> Traceback (most recent call last):
>    File "./imap-signer.py", line 49, in<module>
>      print srv.append('[Google Mail]/Drafts', msg)
> TypeError: append() takes exactly 5 arguments (3 given)

Very odd!

I've just done the same thing with a test Gmail account using IMAPClient 
trunk and it works.

   In [3]: print msg
   ------> print(msg)
   From: Sender <sender@somewhere.com>
   To: "Some One" <someone@somewhere.com>
   Subject: Important

   The email body


   In [4]: c.append('[Google Mail]/Drafts', msg)
   Out[4]: '[APPENDUID 631062299 1] (Success)'


Could you check the version of IMAPClient that you're using? Do 
something like:

   $ python
   Python 2.6 (r26:66714, May  5 2010, 14:02:39)
   [GCC 4.3.4 [gcc-4_3-branch revision 152973]] on linux2
   Type "help", "copyright", "credits" or "license" for more information.
   >>> import imapclint
   >>> imapclient.__version__
   '0.7'

Is it possible that srv in your example is an imaplib.IMAP4 instance 
(from the standard library) and not an IMAPClient instance? The reason I 
ask is that IMAP4.append requires 5 arguments.

Regards,
Menno

Re: [imapclient] Re: append function problems

From:
Robert Pearce
Date:
2011-08-25 @ 14:39
hah ! yes thats it.

I'd somehome managed to bodge together two totally diffrent tutorials
on imap with python, not realising one is using imapclient and one is
using imaplib. Somehow all the code to extract messages etc all worked
fine, it was only when i got to the fetch that it broke.

sigh.


thanks for the help there :-)

- R

On 25 August 2011 15:34, Menno Smits <menno@freshfoo.com> wrote:
> On 2011-08-25 14:19, Robert Pearce wrote:
>  > im trying to save a file to the drafts folder of googlemail, looks
>  > like it's still doing the same thing.
>  >
>  > from_addr = 'Sender<sender@somewhere.com>'
>  > to_addr = '"Some One"<someone@somewhere.com>'
>  > body = 'The email body'
>  > msg = '''\
>  > From: %(frm)s
>  > To: %(to)s
>  > Subject: Important
>  >
>  > %(body)s
>  > ''' % dict(to=to_addr, frm=from_addr, body=body)
>  >
>  > print srv.append('[Google Mail]/Drafts', msg)
>  >
>  > --------------------------------------------------------------
>  >
>>
>> Traceback (most recent call last):
>>    File "./imap-signer.py", line 49, in<module>
>>      print srv.append('[Google Mail]/Drafts', msg)
>> TypeError: append() takes exactly 5 arguments (3 given)
>
> Very odd!
>
> I've just done the same thing with a test Gmail account using IMAPClient
> trunk and it works.
>
>   In [3]: print msg
>   ------> print(msg)
>   From: Sender <sender@somewhere.com>
>   To: "Some One" <someone@somewhere.com>
>   Subject: Important
>
>   The email body
>
>
>   In [4]: c.append('[Google Mail]/Drafts', msg)
>   Out[4]: '[APPENDUID 631062299 1] (Success)'
>
>
> Could you check the version of IMAPClient that you're using? Do
> something like:
>
>   $ python
>   Python 2.6 (r26:66714, May  5 2010, 14:02:39)
>   [GCC 4.3.4 [gcc-4_3-branch revision 152973]] on linux2
>   Type "help", "copyright", "credits" or "license" for more information.
>   >>> import imapclint
>   >>> imapclient.__version__
>   '0.7'
>
> Is it possible that srv in your example is an imaplib.IMAP4 instance
> (from the standard library) and not an IMAPClient instance? The reason I
> ask is that IMAP4.append requires 5 arguments.
>
> Regards,
> Menno
>