how to get the message-id of a send mail

567 views
Skip to first unread message

selecta

unread,
Nov 22, 2010, 7:02:47 AM11/22/10
to web2py-users
I was wondering if there is a proper way to get the message-id of a
send mail.
mail.send just returns true or false
So far I tried to send the mail also to my own account and guess from
comparing header and body that this is the mail that I send out. This
however gives me quite some headaches with all the different character
encodings ...

Is there a better way to get the message-id of a send mail?

mdipierro

unread,
Nov 22, 2010, 8:39:42 AM11/22/10
to web2py-users
What is the "message-id"?

Bruno Rocha

unread,
Nov 22, 2010, 8:52:57 AM11/22/10
to web...@googlegroups.com
I guess you have to create table to store your sent messages, and a method which sends and store the send message.

db.define_table('sentmail',Field('to'),Field('from'),Field('body'))

def mail_sender(to,body):
    try:
        #send the mail
        .....
        .....
        return dict(status='sent',mail_id=db.sentmail.insert(to='',from='',body=''))
    except Exception, e:
        #report the failure
        #stores the message for send later
        return dict(status='fail',fail_id=db.unsentmails.insert())





2010/11/22 selecta <gr...@delarue-berlin.de>

selecta

unread,
Nov 22, 2010, 9:02:17 AM11/22/10
to web2py-users
If you look into the header of an email message, you will find a key
called message-id, the value looks something like
<4CEA5D16...@biologie.hu-berlin.de> this message id is for
example used when you reply to a mail
then the header contains something like In-Reply-To:
<4CEA5D16...@biologie.hu-berlin.de>

I am currently working on a mailing-list plugin and for that I need
the message ids of the outgoing mails to see if incoming mails are
answers to resend incoming mails. This way I can build up a nice
treaded/tree view for all email communication.

Btw wouldn't it be nice to have a completely web2py based platform for
all web2py communication ;-)

Bruno Rocha

unread,
Nov 22, 2010, 9:12:16 AM11/22/10
to web...@googlegroups.com
Is there a result dictionary in Mail class, method send.


But I dont know it this got message_id from the SMTP server.

2010/11/22 selecta <gr...@delarue-berlin.de>

mdipierro

unread,
Nov 22, 2010, 9:20:00 AM11/22/10
to web2py-users
Perhaps this:

result = server.sendmail(self.settings.sender, to,
payload.as_string())
server.quit()
except Exception, e:
logger.warn('Mail.send failure:%s' % e)
self.result = result
self.error = e
return False
self.result = result
self.error = None
return True

should be

result = server.sendmail(self.settings.sender, to,
payload.as_string())
server.quit()
except Exception, e:
logger.warn('Mail.send failure:%s' % e)
self.result = result
self.error = e
return False
self.result = result
self.error = None
return result

Any objection to change it?

On Nov 22, 8:12 am, Bruno Rocha <rochacbr...@gmail.com> wrote:
> Is there a result dictionary in Mail class, method send.
>
> http://code.google.com/p/web2py/source/browse/gluon/tools.py#549
>
> But I dont know it this got message_id from the SMTP server.
>
> 2010/11/22 selecta <gr...@delarue-berlin.de>
>
>
>
> > If you look into the header of an email message, you will find a key
> > called message-id, the value looks something like
> > <4CEA5D16.6010...@biologie.hu-berlin.de> this message id is for
> > example used when you reply to a mail
> > then the header contains something like In-Reply-To:
> > <4CEA5D16.6010...@biologie.hu-berlin.de>

Bruno Rocha

unread,
Nov 22, 2010, 9:25:03 AM11/22/10
to web...@googlegroups.com
Returning "result" will not break the compatibility?

if someone has made a system which test  "if mail.send(...): ...." or "if mail.send(...) == True"   ?

2010/11/22 mdipierro <mdip...@cs.depaul.edu>

Bruno Rocha

unread,
Nov 22, 2010, 9:35:04 AM11/22/10
to web...@googlegroups.com
Using this:

import smtplib
02 fromaddr = 'seue...@gmail.com'
03 toaddrs = 'destin...@gmail.com'
04 msg = 'Mensagem enviada pelo Python utilizando Gmail'
05  
06 #provide gmail user name and password
07 username = 'seu email'
08 password = 'sua senha'
09  
10 # functions to send an email
11 server = smtplib.SMTP('smtp.gmail.com:587')
12 server.ehlo()
13 server.starttls()
14 server.ehlo()
15 server.login(username,password)
16 server.sendmail(fromaddr, toaddrs, msg)
17 server.quit()


I got an empty result on shell

>>> result = server.sendmail(fromaddr,toaddr,msg)
>>> result
{}
>>> 


2010/11/22 Bruno Rocha <rocha...@gmail.com>

Bruno Rocha

unread,
Nov 22, 2010, 9:41:32 AM11/22/10
to web...@googlegroups.com
Mey be having

mail.settings.returning_method = 'boolean' | 'dict'

??

2010/11/22 mdipierro <mdip...@cs.depaul.edu>

mdipierro

unread,
Nov 22, 2010, 9:49:55 AM11/22/10
to web2py-users
Returning a dict evaluates as True therefore I would not consider it a
breaking of backward compatibility. Unless objections I would not make
this more complex that needs to be. Moreover I am not sure what GAE
returns for sendmail.

Massimo

On Nov 22, 8:41 am, Bruno Rocha <rochacbr...@gmail.com> wrote:
> Mey be having
>
> mail.settings.returning_method = 'boolean' | 'dict'
>
> ??
>
> 2010/11/22 mdipierro <mdipie...@cs.depaul.edu>

Bruno Rocha

unread,
Nov 22, 2010, 12:15:46 PM11/22/10
to web...@googlegroups.com
smtplib will return something just in case the server refuses a recipient, I guess you have to define the message-ID and send this in the mail header.

In fact, smtplib doesn't include any headers automatically, but just sends the text that you give it as a raw message

message = 'From: m...@example.com\nMessage-ID:<base64>\nSubject: [PGS]: Results\n\nBlaBlaBla'

I tested here, even GAE or smtplib returns a dict with invalid recipients when server refuse, when succed the dict returned is empty.


Guess you can generate an Message-ID with random or uiid


2010/11/22 mdipierro <mdip...@cs.depaul.edu>

Bruno Rocha

unread,
Nov 22, 2010, 12:21:27 PM11/22/10
to web...@googlegroups.com

You can utilize the email.message.Message class, and use it to generate mime headers, includingfrom:to: and subject. Send the as_string() result via SMTP.

>>> from email import message
>>> m1=message.Message()
>>> m1.add_header('from','m...@no.where')
>>> m1.add_header('to','mys...@some.where')
>>> m1.add_header('subject','test')
>>>m1.add_header('Message-ID',<base64ID>)

>>> m1.set_payload('test\n')
>>> m1.as_string()
'from: m...@no.where\nto: mys...@some.where\nsubject: test\n\ntest\nMessage-ID:<base64ID>'
>>>

2010/11/22 Bruno Rocha <rocha...@gmail.com>
Reply all
Reply to author
Forward
0 new messages