Sending email with Python


2 questions here:

  1. How do you set your server to display error messages from python scripts? Currently when I get an error with a script it will just say “Internal Server Error” and not give me any details of what the problem is.

  2. I am trying to write a python script that sends an email. I am using smtplib and my code is basically like this:

conn = smtplib.SMTP(‘’)
conn.login(‘m1234567’, ‘password’)
conn.sendmail(email, ‘’, msg)

And it gives me “Internal Server Error” (see question 1). Is there anything in particular I need to set up in order to send mail in this way?


You can look in the error.log and see if there is an error message there. But sometimes I end up debugging the script from the shell. A common issue is that the script is not outputting the correct HTTP headers. If that’s corrected, then it’s possible to wrap the program in an exception handler and print out any exceptions that occur, which will show up in the browser.

Regarding sending mail, I was able to get your code fragment working. But one thing I forgot the first time was the #! /usr/bin/env python line at the top.

Got it working now. Thanks!

I did have #!/usr/bin/env python. But I did forget the HTTP header. And the main problem was that I had mixed up the ‘to’ and ‘from’ addresses. Doh!

Thanks again,

Having problems with creating an SMTP object. The statement below times out:

session = smtplib.SMTP( “”, 25)

Any ideas?

session = smtplib.SMTP('smtpServer') server.sendmail(fromAddr, toAddrs, msg) server.quit()

Maximum Cash Discount on any plan with MAXCASH

How To Install PHP.INI / ionCube on DreamHost

Tried that, still timing out.

I wonder if “” is the correct format for the server?

[quote]session = smtplib.SMTP( “”, 25)

Any ideas?[/quote]

  1. Login to shell and use telnet to attempt to connect, eg

[code]> telnet 25
< Connected to
< Escape character is ‘^]’.
< 220 ESMTP


< 221 Bye[/code]2. Try port 587 instead

:cool: -//-

Unfortunately smtplib.SMTP( “”, 587) did not work either.

However, smtplib.SMTP( “”, 587) did work!

So the problem is not in my script but in the server setting.

But the server setting was within your script :wink:

Gmail doesn’t use (that’s http) and ports are 465 or 587.

Maximum Cash Discount on any plan with MAXCASH

How To Install PHP.INI / ionCube on DreamHost

What I meant is that the only place in the script that seems to be wrong is the statement to create the SMTP object. I have tried ports 25 and 587. The DreamHost support team confirmed that I should use “” for the server (of course, replacing “mydomain” with my domain name).

Anything else I should try?

I can’t get email to work with the DH mail server.

Can somebody send me a working script?

Thank you in advance!

[quote]I can’t get email to work with the DH mail server.

Can somebody send me a working script?[/quote]
Have you tried reading the documentation at ? They have an example script that I was able to get to work.

Did you try to run the telnet program to manually connect to the mail server? If you can do that successfully from the web server then we would know that its not a network or mail server issue. I hope you understand what I’m getting at. If the constructor is timing out like you said it is, it might be timing out while trying to connect to the mail server, but if you can connect to the mail server using telnet, then the problem is with Python. Did you use a custom install of Python?

:cool: -//-

Thank you for responding.

I was able to run a script that sends email from DH using the gmail SMTP server. However, using the DH STMP host does not work.

I was also not able to telnet from my computer to using ports 25, 465 or 587.

Is there anything else I should try?

Login to a shell user account on DreamHost, Run these commands.

  1. host
    Should show “ A n.n.n.n”
  2. host n.n.n.n
    Should show “Name: Address: n.n.n.n”
  3. telnet 587
    Should show:

[rying n.n.n.n.. Connected to Escape character is '^]'. 220 ESMTPAlso if the software gives you an error message, tell us what the error message says. “I cannot do x” is not very often helpful. For example “telnet: Unable to connect to remote host: Connection refused” is different than timing out and both mean “I cannot telnet”. Also if you aren’t going to bother copying and pasting error messages here, then at least do so in a support message if you haven’t already.

:cool: -//-

I just played with this for over an hour, and finally put the port as 587 and used the login method in smtplib and presto, it works for any destination.

I still think there are limits on the number of emails I can send per day, until I get a trusted sender designation from dreamhost, which is fine, we don’t want dreamhost to get a reputation for sending spam.

This is as short as I can make it.

import smtplib, socket
from email.mime.text import MIMEText

msg = MIMEText(‘a message’)
msg[‘Subject’] = 'the subject’
msg[‘From’] = 'EMAIL@MYDOMAIN.COM’
msg[‘To’] = ‘’

s = smtplib.SMTP(‘mail.MYDOMAIN.COM’, 587)
s.sendmail(msg[‘From’], [msg[‘To’]], msg.as_string())

OK, moved to another dreamhost server, and moved some python email scripts from bluehost,

must be a domain hosted on the dreamhost account I’m using, and the xyz.login has to correspond to a valid email and password on the hosting account, preferably the same domain in the smtplib.SMTP statement.

Over on bluehost shared, you can go port 25 on localhost. If you don’t want your email password in a python script, create an email account in your dreamhost control panel just for sending emails.