Re: pyramid_mailer exception handling

400 views
Skip to first unread message

Michael Merickel

unread,
Jun 9, 2012, 1:52:05 AM6/9/12
to pylons-...@googlegroups.com
mailer.send() does not send immediately. As per the docs it uses the
transaction manager and sends the mail at the end of the request. You
can create an exception view for this exception, or call
mailer.send_immediately() instead. The transaction manager helps to
avoid sending emails when you get an exception in some part of your
code.

On Fri, Jun 8, 2012 at 6:42 PM, Alejandro Jatib <aja...@gmail.com> wrote:
> i have problems capturing exceptions with pyramid_mailer.
> When i send a mail using a server with smtp authentication activated and
> that fail, python raise a SMTPAuthenticationError: (535, '#5.7.0
> Authentication failed') and i try to catch like that:
>
>        try:
>             mailer = Mailer(host='smtpserver',
>                 port=25,
>                 username='ma...@mail.com',
>                 password='pass')
>         except:
>             return Response('error', content_type='text/plain',
> status_int=500)
> or
>
>        try:
>             mailer.send(message)
>         except:
>             return Response('error', content_type='text/plain',
> status_int=500)
>
> I try to use SMTPAuthenticationError, SMTPResponseException, SMTPException
> imported from smtplib in "except" but a automatic debug page from pyramid
> appear:
>
>
> smtplib.SMTPAuthenticationError
>
> SMTPAuthenticationError: (535, '#5.7.0 Authentication failed')
>
>
> What i doing wrong???
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "pylons-discuss" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/pylons-discuss/-/zGBYENv2eC8J.
> To post to this group, send email to pylons-...@googlegroups.com.
> To unsubscribe from this group, send email to
> pylons-discus...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/pylons-discuss?hl=en.

Alejandro Jatib

unread,
Jun 26, 2012, 10:08:03 AM6/26/12
to pylons-...@googlegroups.com
Thanks. I try your suggestion but i can't create an exception view. Some help with that? (i write some code that i see in the pyramid docs, but nothing) 

El sábado, 9 de junio de 2012 02:52:05 UTC-3, Michael Merickel escribió:
mailer.send() does not send immediately. As per the docs it uses the
transaction manager and sends the mail at the end of the request. You
can create an exception view for this exception, or call
mailer.send_immediately() instead. The transaction manager helps to
avoid sending emails when you get an exception in some part of your
code.

> To post to this group, send email to pylons-discuss@googlegroups.com.
> To unsubscribe from this group, send email to

Antonio Beamud Montero

unread,
Jun 27, 2012, 12:49:19 PM6/27/12
to pylons-...@googlegroups.com
El 09/06/12 07:52, Michael Merickel escribi�:
> mailer.send() does not send immediately. As per the docs it uses the
> transaction manager and sends the mail at the end of the request. You
> can create an exception view for this exception, or call
> mailer.send_immediately() instead. The transaction manager helps to
> avoid sending emails when you get an exception in some part of your
> code.

I have the same problem, in my case is managing a register form. The
user fill the form, the system processes the data and send a mail to
that user to confirm the data.
In my case, (I'm using pyramid_tm and pyramid_mailer), if the smtp
server is down, a exeception is raised and I cannot handle it:

2012-06-27 18:06:50,990 CRITI [txn.-1247151296][Dummy-2] A storage error
occurred during the second phase of the two-phase commit. Resources may
be in an inconsistent state.
2012-06-27 18:06:50,990 ERROR [txn.-1247151296][Dummy-2] Error in
tpc_abort() on manager <zope.sqlalchemy.datamanager.SessionDataManager
object at 0xb332c0ac>
...
File "/usr/lib/python2.7/smtplib.py", line 284, in _get_socket
return socket.create_connection((port, host), timeout)
File "/usr/lib/python2.7/socket.py", line 571, in create_connection
raise err
error: [Errno 111] Connection refused

The problem is the user data is commited to the database, but the mail
is not sent.
I want, if the mail cannot be sent, abort the transaction, rollback the
data flushed to the database and show a message to the user in a ordered
way...

How I can do this?

Greetings

Jason

unread,
Jun 27, 2012, 3:18:27 PM6/27/12
to pylons-...@googlegroups.com


On Wednesday, June 27, 2012 12:49:19 PM UTC-4, antonio wrote:


The problem is the user data is commited to the database, but the mail
is not sent.
I want, if the mail cannot be sent, abort the transaction, rollback the
data flushed to the database and show a message to the user in a ordered
way...

How I can do this?

Greetings

Then you will run into the problem of having the mail already sent out if the database transaction fails to commit.

-- Jason
 

Antonio Beamud Montero

unread,
Jun 28, 2012, 6:27:40 AM6/28/12
to pylons-...@googlegroups.com
El 27/06/12 21:18, Jason escribió:
I've solved with the next code:
    ...
    try:
        mailer = get_mailer(self.request)
        message = Message(subject=subject,
                          sender=sender,
                          recipients=[user.email],
                          body=body % confirm_url)
        mailer.send_immediately(message)
    except:
        log.error('sending mail error')
        transaction.doom()
    ...
Perhaps the best approach is use the send_to_queue, and not send the mail in the request.

Greetings.

-- Jason
 
--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To view this discussion on the web visit https://groups.google.com/d/msg/pylons-discuss/-/yaCyxfafY3QJ.

Robert Forkel

unread,
Jun 28, 2012, 7:06:15 AM6/28/12
to pylons-...@googlegroups.com
yes, i think send_to_queue would be better, because this allows to
retry sending the message.
regards
robert
Reply all
Reply to author
Forward
0 new messages