Sending email with Python

108 views
Skip to first unread message

Ram Rachum

unread,
Jun 30, 2012, 11:58:10 AM6/30/12
to pyweb-il
Hi everyone,

Is there a package that is to email what `requests` is to HTTP?


Thanks,
Ram.

oz katz

unread,
Jun 30, 2012, 12:11:54 PM6/30/12
to pywe...@googlegroups.com
What exactly are your needs?


(from the author of "requests")
But again, you should probably be more specific..

Oz
--
You received this message because you are subscribed to the Google Groups "PyWeb-IL" group.
To post to this group, send email to pywe...@googlegroups.com.
To unsubscribe from this group, send email to pyweb-il+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/pyweb-il?hl=en.

Ram Rachum

unread,
Jun 30, 2012, 3:05:00 PM6/30/12
to pywe...@googlegroups.com
Thanks Oz. It's good to know about inbox.py, but that doesn't help here; I'm interested in sending emails, not receiving them.

I basically want a function `send_mail` that takes an email address, a subject line, and a content, and sends.

I know that Django provides one, but using Django would entail having a "site" and a settings file, and I don't want any of those for this project.


Look at the first example here:


Under "First, let’s see how to create and send a simple text message"

I don't know whether this should be considered sad or funny. That example code does everything except licking a stamp in order to put it on the email. This is the exact opposite of what I'm looking for :)

Shai Berger

unread,
Jun 30, 2012, 4:05:48 PM6/30/12
to pywe...@googlegroups.com
On Saturday 30 June 2012, Ram Rachum wrote:
>
> I basically want a function `send_mail` that takes an email address, a
> subject line, and a content, and sends.
>

email doesn't work this way. You need a sender, and most mail servers for
receivers will not talk to you if you don't have an MX record in DNS; which
means, in practice, you need some "recognized" mail server to send through.

So, your "send mail" function can only work after you've told it, in some way,
who's the sender, where's the mail server, and (usually) how to log in to it.

> I know that Django provides one, but using Django would entail having a
> "site" and a settings file, and I don't want any of those for this project.
>

The little email I did out of Django was using Lamson (lamsonproject.org) --
but again, that is more on the receiving end, and it's a whole framework.
Perhaps you can get something more to your taste out of that.

HTH,
Shai.

Ram Rachum

unread,
Jun 30, 2012, 4:15:52 PM6/30/12
to pywe...@googlegroups.com
On Sat, Jun 30, 2012 at 11:05 PM, Shai Berger <sh...@platonix.com> wrote:
On Saturday 30 June 2012, Ram Rachum wrote:
>
> I basically want a function `send_mail` that takes an email address, a
> subject line, and a content, and sends.
>

email doesn't work this way. You need a sender, and most mail servers for
receivers will not talk to you if you don't have an MX record in DNS; which
means, in practice, you need some "recognized" mail server to send through.

So, your "send mail" function can only work after you've told it, in some way,
who's the sender, where's the mail server, and (usually) how to log in to it.

Sorry, I should have specified that I'm okay with specifying SMTP settings. So: Import, specify STMP settings, send mail. Anything that does that?

Basically my approach is "simple things should be simple, complex things should be possible." I want a package that makes the basic tasks as simple as possible, without even having to read the documentation beyond the "quick examples" section, and people who have more complex needs could dig in to the documentation and the code, instantiate classes and call their methods, etc. 
 

> I know that Django provides one, but using Django would entail having a
> "site" and a settings file, and I don't want any of those for this project.
>

The little email I did out of Django was using Lamson (lamsonproject.org) --
but again, that is more on the receiving end, and it's a whole framework.
Perhaps you can get something more to your taste out of that.

HTH,
       Shai.

Yuval Adam

unread,
Jun 30, 2012, 5:31:46 PM6/30/12
to pywe...@googlegroups.com

Sorry, I should have specified that I'm okay with specifying SMTP settings. So: Import, specify STMP settings, send mail. Anything that does that?


Yeah, the Django builtin send_mail(). I'm not sure why you think it's complicated.

Basic Settings (Google as example):

EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'fr...@example.com'
EMAIL_HOST_PASSWORD = 'secretPASS'
EMAIL_PORT = 587
EMAIL_USE_TLS = True

And then it's just:

send_mail('subject', 'content', 'fr...@example.com', ['t...@example.com'])

That's it. What's all the fuss about?

--
Yuval Adam
http://y3xz.com


Ram Rachum

unread,
Jun 30, 2012, 5:52:45 PM6/30/12
to pywe...@googlegroups.com
It's almost good enough, the problem is that you need to have a Django settings file.

Let me give you an example of usage, of something I wanted to do today.

Today, as many of you have probably noticed, half the Internet was down due to electrical storms in the East Coast. Okay, not really half, but many big sites. (Heroku, AWS, Pinterest, NetFlix...)

Our website was one of the casualties. It was down, and I wanted to write a short Python script that would try to visit it every 2 minutes, and when it finally succeeds, shoot me a mail telling me the site is back up.

Now, that script is so simple, I intended to write it in the REPL, not even saving it to a file, and just keep it running in a `screen` session.

I wanted something to the effect of:

>>> while 'Error' in requests.get('http://oursite.com').content:
...     time.sleep(120)
... send_mail('r...@rachum.com', 'Site is back up!')

Now, if I had to introduce a Django `settings.py` file into this, it would have been a lot more cumbersome.



Yuval Adam

unread,
Jun 30, 2012, 6:06:09 PM6/30/12
to pywe...@googlegroups.com
Well, as far as I remember the smtplib API is reasonable.
Something along the lines:

import smtplib
smtp = smtplib.SMTP('server', 'some', 'other', 'conn', 'params')
smtp.sendmail('subject', 'content', 'to', 'from')

Don't copy-paste, obviously, but real use shouldn't be much different.

Though I definitely agree it would be nice to see an SMTP equivalent for requests.

Ram Rachum

unread,
Jun 30, 2012, 6:23:21 PM6/30/12
to pywe...@googlegroups.com
I was almost convinced that `smtplib` is good enough, but then I saw the example here:


Ignore the `raw_input` parts which take the bulk of the example, but look at the construction of `msg`. You have to add the `From` and `To` headers manually in the text. Lame.

And actually a bigger issue: You need to authenticate to the SMTP server, and that's a separate line, calling `SMTP.login`.

Ram Rachum

unread,
Jul 4, 2012, 4:26:10 AM7/4/12
to pywe...@googlegroups.com
Attached is the makeshift module I made for myself now. It's very very basic. This way I could send email to myself in simple scripts.

To use it, replace 'your.auxil...@gmail.com', 'your.auxiliary.password' and 'your...@email.com' with real values.

Hope this is useful for someone.
lame_smtp.py

Ori Peleg

unread,
Jul 4, 2012, 5:08:01 AM7/4/12
to pywe...@googlegroups.com
I also think smtplib is great, it's what I'd use for simply sending emails to an upstream SMTP server.

If you want more intelligent email processing, Zed Shaw's Lamson is really cool: http://lamsonproject.org/
Check out my blog: http://orip.org
Reply all
Reply to author
Forward
0 new messages