Sending email with Python

software development

#1

Hi,

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(‘mail.mydomain.com’)
conn.login(‘m1234567’, ‘password’)
conn.sendmail(email, ‘webmaster@mydomain.com’, msg)
conn.quit()

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?

Thanks,
Mark


#2

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.


#3

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,
Mark


#4

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

session = smtplib.SMTP( “mail.mydomain.com”, 25)

Any ideas?


#5

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


#6

Tried that, still timing out.

I wonder if “mail.mydomain.com” is the correct format for the server?


#7

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

Any ideas?[/quote]

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

[code]> telnet mail.mydomain.com 25
< Connected to server.mail.dreamhost.com.
< Escape character is ‘^]’.
< 220 servermail-a1.g.dreamhost.com ESMTP

[quote]quit
[/quote]

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

:cool: openvein.org -//-


#8

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

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

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


#9

But the server setting was within your script :wink:

Gmail doesn’t use mail.domain.com (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


#10

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 “mail.mydomain.com” for the server (of course, replacing “mydomain” with my domain name).

Anything else I should try?


#11

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

Can somebody send me a working script?

Thank you in advance!


#12

[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 http://docs.python.org/library/smtplib.html ? 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: openvein.org -//-


#13

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 mail.myaccount.com using ports 25, 465 or 587.

Is there anything else I should try?


#14

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

  1. host mail.mydomain.com
    Should show “mail.mydomain.com A n.n.n.n”
  2. host n.n.n.n
    Should show “Name: xxx.mail.dreamhost.com Address: n.n.n.n”
  3. telnet mail.mydomain.com 587
    Should show:

[rying n.n.n.n.. Connected to xxx.mail.dreamhost.com. Escape character is '^]'. 220 something.dreamhost.com 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: openvein.org -//-


#15

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.

[code]#!/usr/bin/python
import smtplib, socket
from email.mime.text import MIMEText

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

s = smtplib.SMTP(‘mail.MYDOMAIN.COM’, 587)
s.login(‘EMAIL@MYDOMAIN.COM’,‘PASSWORD-AS-DEFINED’)
s.sendmail(msg[‘From’], [msg[‘To’]], msg.as_string())
s.quit()[/code]


#16

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

mail.example.com

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.