Search code examples
pythonemailgmailweb2py

mail.send() feature in web2py for bounced email/ email address that doesnt exist


So I made a small app that sends emails from web2py, connected to the gmail smtp server. The problem here is that it always gives me a success message! Even when the email-id is invalid. I have tested the bounce back feature of gmail by sending mails from gmail to invalid email ids to which gmail says 'Your message has been sent', but on going to the sent folder gives the mailer daemon with a time lag(this differs for different mailing services). I think the mail.send() feature of web2py is unable to capture that and takes the 'Your message has been sent' as confirmation. How can one capture mails that haven't been sent using web2py?

if mail.send(session.email,subject=session.subject,message= session.message):
            response.flash = 'email sent sucessfully.'
else:
            response.flash = 'fail to send email sorry!'

Solution

  • mail.send() is only passing the message to the SMTP server, the response simply states that the server successfully received/accepted the message for further processing. To check this disconnect your computer from the net or intentionally break the mail config and try again - mail.send() should fail since it can't connect to the SMTP server to hand it the message.

    The actual message processing (including destination address check) happens later on the SMTP server and other servers on the mail delivery path. Processing failures typically cause error emails to be sent back (bouncing) - this is a different level in the email exchange protocol stack than the one at which mail.send() operates.