Django: Should sendgrid emails always be sent from Celery? - python

I'm using django-sendgrid-v5 and I read somewhere that it isn't good to send emails from the main webserver. Should I process emails from Celery? Or is it fine to call from the main app since I'm using an external service like Sendgrid anyways?

I don't know in which context you've read that, but I would guess it has something to do with reliability, spam and security in general.
Short answer: Yes, this should be fully okay as you are using an external email service.
Another option is to set up a Smart host on your webserver and let your main email server deliver it to the final recipients.
Long answer: Nowadays sending emails from a (web)server, which is not fully set up as an email server might be difficult in means of reliably sending emails.
Due to the massive amounts of spam and malware sent, most (or at least a lot) receiving email servers (Mail Exchangers) are trying to check if the emails they should deliver to their users, are legit.
This is done by several settings mostly on the server itself. To name only a few: RDNS, DKIM, Greylisting, etc.
In general a (web) server whos main purpose is not sending emails, does not have all these settings. This might result in difficulties to reach certain email addresses.

Related

GAE Python - Receive incoming email on #mydomain.com address

I am setting up a GAE app that triggers certain tasks by an email from the user. I've set up the app using the information on this page: https://developers.google.com/appengine/docs/python/mail/receivingmail. Everything works as expected, however, I would like to send the emails to a #mydomain.com email address, instead of a #myapp.appspotmail.com email address.
Currently I am using an automatic forward on the email address to get around this, however once this scales I expect to reach the daily limit on forwarded messages at some point (10.000 according to https://support.google.com/a/answer/166852?hl=en).
Is there any way that I can integrate my app with the #mydomain.com email address to prevent this from happening? Or would you know another solution for this issue?
Probably, you should check recently implemented GAE integration with SendGrid, I haven't used it myself (use the same solution you've described with email forwarding), but looks like it can help.
As LA_ mentioned, SendGrid offers a webhook for this. I ended up using MailGun, which has a similar functionality and calls a HTTP POST to my app whenever an email comes in on my #mydomain.com address.

Writing an email sending program in Python

I want to write an email sending script in Python. Most email programs have to connect to existing servers, like Gmail or Hotmail. I want my script to work independantly of those servers and just be able to send email itself (without having to logon anywhere else). The reason for this is because most email servers (like Yahoo) limit what you can do, such as controlling the sender address or sending certain types of files. So I wanted to write my own script to get around that. So what do I do? Where do I begin learning how to do this? Do I have to write my own server? If I do, how is that done?
You need an MTA (a.k.a. mail transfer agent), whether it's local or remote. SMTP servers talk to each other to deliver the mail through — if you don't want to connect to the remote one, you need to run a local one. Look into Postfix or exim — just be careful not to allow random people to connect to it. Go to Server Fault if you need help with configuring them.
This is language-agnostic, BTW.
The email-module of Python should give you a good starting point. Btw this was the first hit when googling Python email.
You'll essentially need to set up your own mail server. I prefer postfix but there are several alternatives, you'll have to google this one.
Once you can send email through your own server, look into smtplib or email-library for sending email with Python

Best way to do email scheduling on a python web application?

I have a Pyramid web application that needs to send emails such as confirmation emails after registration, newsletters and so forth. I know how to send emails using smtplib in python and I decided on an smtp service (I think sendgrid will do the trick).
The real problem is the scheduling and delay sending of the emails - for example, when a user registers, the email is to be sent on the form post view. But, I don't want to block the request, and therefore would like to "schedule" the email in a non-blocking way.
Other than implementing this myself (probably with a DB and a worker), is there an existing solution to email queue and scheduling?
Thanks!
The existing solution to which you refer is to run your own SMTP server on the machine, bound only to localhost to prevent any other machines from connecting to it. Since you're the only one using it, submitting a message to it should be close to instantaneous, and the server will handle queuing, retries, etc. If you are running on a UNIX/Linux box, there's probably already such a server installed.
In my app, I insert emails in DB table, and I have python script running under cron that checks this table and sends email updating record as sent.
You can also use Redis Lists as a Queue to send emails. Create couple of worker processes which listens a Redis List, and
publish a Email job via RPUSH or LPUSH
receive the job at your worker via LPOP or RPOP
so that your web app worker process won't be affected, or not even feel the overhead for email sending operations.
This design allows you not to care how long does it take to send an email. The email service might be local or a external email service, however you want.

Testing email sending? - Python/Django

I have a live site, but would like to run a periodic task that would periodically check if various pages and email sending work.
e.g. register page, forgot password page, etc...
I'm having quite a problem figuring out how to do this...
Any ideas? :)
At my workplace we use nagios to monitor, among many things, that the smtp service and web apps are running as it should. For crucial pages, we use the check_http nagios plugin to ensure that the pages are accessible and contain specific keywords.
You can set each check to run as frequently as you like, and have nagios send you an email if anything goes amiss.
As for checking whether email sending from django is working, that's a little tricky. None of my sites are mission critical, so I'm happy to assume that if my code base doesn't change and if the web apps and SMTP servers are working, then emails from django should be ok.
If it is critical that you check that django can send emails, you could have a view (restricted by source IP and perhaps a password in POST/GET) that triggers an outgoing email. You can then write your own plugin to access said view and check that the email is sent.
Here's a nice write-up on how to check your email service using nagios: http://www.linuxjournal.com/content/monitoring-email-nagios

Email integration

I was wondering if someone could help me out. In some web application, the app will send out emails, say when a new message has been posted. Then instead of signing into the application to post a reply you can just simply reply to the email and it will automatically update the web app with your response.
My question is, how is this done and what is it called?
Thanks
Generally:
1) Set up a dedicated email account for the purpose.
2) Have a programm monitor the mailbox (let's say fetchmail, since that's what I do).
3) When an email arrives at the account, fetchmail downloads the email, writes it to disk, and calls script or program you have written with the email file as an argument.
4) Your script or program parses the email and takes an appropriate action.
The part that's usually mysterious to people is the fetchmail part (#2).
Specifically on Mail Servers (iff you control the mailserver enough to redirect emails to scripts):
1-3) Configure an address to be piped to a script you have written.
4) Same as above.
You should take a look at Lamson; it'll enable you do what you've described, and more besides.
From your tags, I'll assume you're wanting to do this in Django.
There's an app out there called jutda-helpdesk that does exactly what you're looking for using poplib, which means you just have to set up a POP3 compatible email address.
Take a look at their get_email.py to see how they do it. You just run this script from cron.
This is an area where the Rails-world is ahead: Rails has built-in support for receiving emails. The mail sever configuration though is probably just the same.
To see a working example on how to receive emails in python and process then using django, check this: http://code.google.com/p/jutda-helpdesk/
A common tool used for this purpose is procmail.
You need to set up dedicated email address (which is the "from_email" address in your outgoing email). Then your MTA, such as postfix or qmail, will deliver mail to that address to procmail instead of an actual mailbox.
Procmail can then pass the email on to your python script that can do updates in the app. See standalone django scripts by James Bennett on how to code python scripts that can work with your app.

Categories

Resources