How to integrate SendGrid in Web2py ?

381 views
Skip to first unread message

Krishna Bavandlapally

unread,
Apr 13, 2016, 4:59:16 AM4/13/16
to web2py-users
How to integrate (default mailing) SendGrid in Web2py ?

Leonel Câmara

unread,
Apr 13, 2016, 6:32:17 AM4/13/16
to web2py-users
If you're asking this, I'm just going to tell you the most obvious way. Just use their SMTP server. I've never used sendgrid with web2py but the config would be something like this:

mail.settings.server = 'smtp.sendgrid.net:587'
mail.settings.login = 'sendgrid_username:sengrid_password'
mail.settings.tls = True

Ron Chatterjee

unread,
Apr 13, 2016, 11:42:35 AM4/13/16
to web2py-users
On that note. Leonel, how do we create inbox using web2py that looks similar to outlook? So, when someone write a blog post and other replies or for chat application to say, we can use that outlook looking interface to present the data. Can someone reply to the mail we send with us...@myweb2pyapp.com?

Krishna Bavandlapally

unread,
Apr 13, 2016, 12:48:13 PM4/13/16
to web2py-users
Thank you Leonel Câmara.

It worked.

mail.settings.server = 'smtp.sendgrid.net:587'
mail.settings.sender = xxx...@mail.com
mail.settings.login = 'sendgrid_username:sendgrid_password'
mail.settings.tls = True

Mirek Zvolský

unread,
Apr 15, 2016, 12:41:20 PM4/15/16
to web2py-users
Why sendgrid?
I have just implemented sparkpost (rcommended from mandrill, which terminates their free services).
100000 mails/month for free, and we have sparkpost package in pypi.

Here my notes (use google translate):




Dne středa 13. dubna 2016 10:59:16 UTC+2 Krishna Bavandlapally napsal(a):

Massimo Di Pierro

unread,
Apr 16, 2016, 12:51:37 AM4/16/16
to web2py-users
I use sparkpost too and I am happy with it.

billmac...@gmail.com

unread,
Apr 21, 2016, 1:29:08 PM4/21/16
to web...@googlegroups.com
Is SparkPost is any different than to fastmail or using gmail as shown in web2py book? In otherwords, using SpartPost, we don't need to do mail.settings just an API key? How about the return email address or smsemail?

Mirek Zvolský

unread,
Apr 21, 2016, 1:55:59 PM4/21/16
to web2py-users
This is just different interface (different parameters) for SMTP server and for Sparkpost service.
Of course, you need use the code from documentation of sparkpost module (instead of code from welcome application).

Sparkpost service uses API key tu identify you as its user.
To send mails without problems, you must validate your registration email (just click link in the message) + set 2 TXT records in DNS settings for your domain. Sparkpost will give you hint for the content of these TXT records.





Dne čtvrtek 21. dubna 2016 19:29:08 UTC+2 billmac...@gmail.com napsal(a):

Michael Beller

unread,
Apr 21, 2016, 2:00:00 PM4/21/16
to web2py-users
You can use the sendgrid smtp server or their api.
https://sendgrid.com/blog/which-protocol-should-i-use-to-send-email-smtp-or-rest/

Here is my logic for using their api (I also use mailgun with I like also):

def send_email_via_sendgrid(email_to, email_subject, email_body):
   
# https://github.com/sendgrid/sendgrid-python

   
import sendgrid
   
from sendgrid import SendGridError, SendGridClientError, SendGridServerError

    sg
= sendgrid.SendGridClient('<your sendgrid account>', '<your sendgrid pwd>', raise_errors=True)

    message
= sendgrid.Mail()
    message
.add_to(email_to)
    message
.set_subject(email_subject)
    message
.set_html(email_body)
   
#message.set_text('Body')
    message
.set_from('Your Name <us...@myweb2pyapp.com>')
   
#status, msg = sg.send(message)

   
try:
        sg
.send(message)
   
# raise SendGridClientError
   
except SendGridClientError:
       
pass
   
except SendGridServerError:
       
pass
   
finally:
        return_code
= response.status
       
return locals()

Michael Beller

unread,
Apr 21, 2016, 2:06:41 PM4/21/16
to web2py-users
Hi Ron,

I'm implementing an 'outlook' like interface for some messaging at the moment which includes two way messages with attachments.  The early parts are incorporated in my scaffold app at https://mjbeller.pythonanywhere.com/starter

The 'outlook' like interface that you can use is included like this:

(my starter app does not include the nice looking mailbox page yet, just a simple list of messages tied to the message icon in the header which includes an unread count)

I'll push some updates to starter for messages in the next few days (but probably not the full integration with the mailbox.html yet, just the simple datatables list of messages already in the repo)

billmac...@gmail.com

unread,
Apr 21, 2016, 2:09:21 PM4/21/16
to web2py-users
Thank you guys. Michel, 

message.set_from('myweb2pyapp.com')

If someone response to the email, how do I retrieve it? I need to log into my sandgrid account I am guessing. Not sure why sessage.set_form is my app name.com

Michael Beller

unread,
Apr 21, 2016, 2:16:15 PM4/21/16
to web2py-users
The starter app has a 'message.py' controller that has functions for creating, receiving, and replying to messages (in addition to listing, viewing).

The starter app code uses mailgun but it's very similar to sendgrid.  You need to setup a webhook in your sendgrid or mailgun account to post the inbound message to your app and then the controller function (receive_mailgun_message in the starter app) adds it to the message table.

Ron Chatterjee

unread,
Apr 21, 2016, 2:19:23 PM4/21/16
to web...@googlegroups.com
Thank Michael. I feel special! Would love if you can add some examples. 

Mirek Zvolský

unread,
Apr 22, 2016, 2:34:54 AM4/22/16
to web2py-users
Not sure if I properly understand your question.
But..

I use this in starting web in such easy way:
- Virtual server Debian, no SMTP/POP3 installed at all,
- web2py app sends what is necessary through SparkPost,
- all incomming mails for the domain I have redirected in hosting admin settings (at forpsi) to my personal e-mail address.

Ron Chatterjee

unread,
Apr 28, 2016, 11:38:07 AM4/28/16
to web...@googlegroups.com
Have anyone used yagmail?




And regarding the outlook like mailbox interface as Michael mentioned, Michael, we can simply do this. Right?


And for the code use sandgrid or yagmail or whatever. for example if request.args(i) is 0, inbox is "unread" and it is, its been read. Mails that's been sent goes to the send folder etc... Am I wrong?

Michael Beller

unread,
Apr 29, 2016, 12:35:01 AM4/29/16
to web2py-users
I haven't used yagmail, I only looked quickly but I didn't see an advantage of yagmail over the web2py mail functions.

The bootply layout below looks great - I don't see any reason that wouldn't work.

Do you want to receive email into the app?  I didn't see how yagmail supports inboud email, that's where you need a service to route email to a webhook that you define.  Services like sendgrid, mandrill, and mailgun will also parse the headers, body, and attachments and POST them to your webhook where you can easily process and 'add' to your inbox.

They each have different methods to track related emails, e.g., the response to an outbound email so you know if somebody responded.

Ron Chatterjee

unread,
Apr 29, 2016, 7:25:01 AM4/29/16
to web2py-users
Thank you Michael. Great tips!
Reply all
Reply to author
Forward
0 new messages