Send Email in Django without SMTP server. Like php mail() function does - python

As far as I read, Django only supports sending mails using SMTP.
I would like to send Emails from my script in Django, but do not want to setup SMTP server (it's too complex for me, I'm a linux newbie).
Is it possible, to send mails in Django in the same way like I do it in PHP, without providing SMTP login, password and etc?

PHP uses sendmail on UNIX system to send emails. I guess at some point when you set up the system, this is, sendmail is configured.
There is an API to sendmail for Python, maybe it helps you. But there is a SMTP server involved in any case ;)

Postfix and Exim were built to deal with all of the problems associated with forwarding email from your host to the rest of the world. Your app speaks SMTP to them, and they turn around and speak SMTP with the destination. But they're very, very good at it.
There is nothing stopping you from doing a DNS lookup for the MX records of the email address you're sending to and connecting straight to that server and speaking SMTP to it. Nothing except that nagging voice that should be asking you "Is this really easier than apt-get install exim4?"

So how does PHP do it? By magic?
If you don't have an SMTP server, sign up for a GMail account and use that.

Your hosting provider might have set up the host and any possible login credentials for all the PHP pages on their machines. This would make it seem like none are required. Your hosting provider should be more than happy to give you the information. Try searching for SMTP in their FAQ, forum, and any welcome emails they sent. If your search does not turn anything up, ask them directly.
Once you have the information, you will want to add it to your settings.py file using these email settings:
# *** settings.py ***
#EMAIL_HOST = 'host here'
#EMAIL_PORT = 587
#EMAIL_HOST_USER = 'your user here'
#EMAIL_HOST_PASSWORD = 'your password'
#EMAIL_USE_TLS = True
Uncomment and use as many of these settings as you need.

I suppose since you're using Django you're fine with Python in general. There's a new Python MTA evolving which looks very promising http://lamsonproject.org/docs/hooking_into_django.html

Related

Any suggestions for smtp server to use with Django?

I've tried to use smtpd but couldn't get it to work properly (Here's the link to the issue). I only need the server to receive emails and store in database. I've found few like MailGun, and although it looks great it is not free. Any suggestions?
There are so many options, it's an opinion question, and also related to the specific use case. So here are the main options you have.
It's mostly your decision and not "with django". The question is really choosing a mail server for a web application.
From the django point of view it's either local server(faster), or external API. As a developer, sending SMTP is probably easier than external API because django already includes the backend handling. But good email service providers usually have a good clients libraries, and it's not that complex to use their API as well.
Your own email server: Install and maintain your own mail server. Pros: cheaper, a cheap VPS will do, no limits on accounts (useful for testings) and mails. Cons: installing a mail server + auth backend etc is not easy if you didn't installed one before, and your emails can be easily marked as spam. If you select this option, Postfix is a safe choice.
External service: with API. Pros: easier to implement, and less chances to be marked as spammer. Cons: more expensive, possible specifc coding against the specific API
Your mail server + external API: the local server connection is much faster, so django sends the email quickly, and then the server handles the slower sending to an external service
Summary: If you just start something, and need a simple email facility, pick an external service that uses SMTP. You will have something up and running, using the very easy django SMTP utilities, without specific coding to the vendor API. Once you need to scale, do the research and pick a more advanced solution.
Note: usually sending email requires some async queue, that allows django to pass the email sending task and continue with the request, without waiting to the smtp connection. SMTP connections can be slow.

Openshift overrides email header 'from', 'reply-to' fields. How to send email without having to use SendGrid nor other paid email service.?

I have django 1.6 and python 2.7 deployed on Openshift. I notice that when the application sends emails out, Openshift overrides the email header by changing the 'From' field to 'no-reply#rhcloud.com' and ignore all 'Reply-to' field that has been set in the application.
I have searched around and it seems like Openshift overrides the email header and recommendation is to use their email service partner which is NOT FREE.
Is there any other way to avoid this ie. deploy Django application on Openshift while still having the application sends email as per dictated in the program. This exact program runs with no issues on test environment and localhost.
Any pointers are much appreciated. Thank you.
I myself is looking for free SMTP library to just send emails. So far not much luck.
Tried java embedded SMTP library Aspirin. I am able to send mails but not very comfortable working with it as I keep getting some unknown exceptions.
Apache James as another Java based SMTP server but don't think we can embed in the code yet.

How to avoid the "This message may not have been sent by" warning when sending email using Google App Engine?

I have a python GAE app that sends emails like in the example using the address of a registered administrator for the application as the "sender" address. When an email arrives from such an API call, here's a pic of the attached warning.
This message may not have been sent by: EmergencyButtonApp#gmail.com
Learn more Report phishing
Am I doing something wrong? None of the 3 options mentioned in the "Learn More" link are relevant I believe: the sender is a gmail address so the "SPF and DKIM authentication" solution isn't relevant, I'm not sending to a mailing list and I'm not using SMTP (I think, though maybe GAE is...).
Assuming you're seeing this in production, it's probably because you're claiming to be from a gmail address, but sending via App Engine. Use one of your app's email addresses as the sender (foo#yourapp.appspotmail.com) and it should work fine.
I think the best solution is to use Google's SMTP servers... It's the best way to not be considered as a spammer.

Linux email server, how to know a new email has arrived

I am developing an email parsing application using python POP3 library on a linux server using Dovecot email server. I have parsed the emails to get the contents and the attachments etc. using POP3 library.
Now the issue is how to notify a user or actually the application that a new email has arrived? I guess there should be some notification system on email server itself which I am missing or something on linux which we can use to implement the same.
Please suggest.
Thanks in advance.
POP3 does not have push ability. Like a regular ol' post office you need to actually go to check your e-mail. IMAP does have functionality similar to (but not exactly the same as) mail pushing. I'd suggest taking a look at it.

Integrate postfix mail into my (python)webapp

I have a postfix server listening and receiving all emails received at mywebsite.com Now I want to show these postfix emails in a customized interface and that too for each user
To be clear, all the users of mywebsite.com will be given mail addresses like someguy#mywebsite.com who receives email on my production machine but he sees them in his own console built into his dashboard at mywebsite.com.
So to make the user see the mail he received, I need to create an email replica of the postfix mail so that mywebsite(which runs on django-python) will be reflecting them readily. How do I achieve this. To be precise this is my question, how do I convert a postfix mail to a python mail object(so that my system/website)understands it?
Just to be clear I have written psuedo code to achieve what I want:
email_as_python_object = postfix_email_convertor(postfix_email)
attachments_list = email_as_python_object.attachments
body = email_as_python_object.body # be it html or whatever
And by the way I have tried default email module which comes with python but thats not handy for all the cases. And even I need to deal with mail attachments manually(which I hate). I just need a simple way to deal with cases like these(I was wondering how postfix understands a email received. ie.. how it automatically figures out different headers,attachments etc..). Please help me.
You want to have postfix deliver to a local mailbox, and then use a webmail system for people to access that stored mail.
Don't get hung up on postfix - it just a transfer agent - it takes messages from one place, and puts them somewhere else, it doesn't store messages.
So postfix will take the messages over SMTP, and put them in local mail files.
Then IMAP or some webmail system will display those messages to your users.
If you want the mail integrated in your webapp, then you should probably run an IMAP server, and use python IMAP libraries to get the messages.
First of all, Postfix mail routing rules can be very complex and your presumably preferred solution involves a lot of trickery in the wrong places. You do not want to accidentally show some user anothers mails, do you? Second, although Postfix can do almost anything, it shouldn't as it only is a MDA (mail delivery agent).
Your solution is best solved by using a POP3 or IMAP server (Cyrus IMAPd, Courier, etc). IMAP servers can have "superuser accounts" who can read mails of all users. Your web application can then connect to the users mailbox and retreive the headers and bodys.
If you only want to show the subject-line you can fetch those with a special IMAP command and very low overhead. The Python IMAP library has not the easiest to understand API though. I'll give it a shot (not checked!) with an example taken from the standard library:
import imaplib
sess = imaplib.IMAP4()
sess.login('superuser', 'password')
# Honor the mailbox syntax of your server!
sess.select('INBOX/Luke') # Or something similar.
typ, data = sess.search(None, 'ALL') # All Messages.
subjectlines = []
for num in data[0].split():
typ, msgdata = sess.fetch(num, '(RFC822.SIZE BODY[HEADER.FIELDS (SUBJECT)])')
subject = msgdata[0][1].lstrip('Subject: ').strip()
subjectlines.append(subject)
This logs into the IMAP server, selects the users mailbox, fetches all the message-ids then fetches (hopefully) only the subjectlines and appends the resulting data onto the subjectlines list.
To fetch other parts of the mail vary the line with sess.fetch. For the specific syntax of fetch have a look at RFC 2060 (Section 6.4.5).
Good luck!
I'm not sure that I understand the question.
If you want your remote web application to be able to view users' mailbox, you could install a pop or imap server and use a mail client (you should be able to find one off the shelf) to read the emails. Alternatively, you could write something to interrogate the pop/imap server using the relevant libraries that come with Python itself.
If you want to replicate the mail to another machine, you could use procmail and set up actions to do this. Postfix can be set up to invoke procmail in this wayy.

Categories