Sending email

F

Fencer

Hello, I'm using Python version 2.6.2 and I discovered it has built-in
libraries for sending email (imports smtplib and email). On the web I
discovered how to send mail through smtp.gmail.com:

mail_server = smtplib.SMTP()

mail_server.connect('smtp.gmail.com')
mail_server.ehlo()
mail_server.starttls()
mail_server.ehlo()
mail_server.login('username', 'password')

msg = MIMEText('Some message text')
msg['Subject'] = 'Some subject'
msg['From'] = 'Mr Underhill'
msg['To'] = 'someemailaddress'

me = 'myemailaddress'
mail_server.sendmail(me, ['someemailaddress'], msg.as_string())

That seems to work just fine but for the messages I will be sending I
don't want to use my private GMail account.
We have an SMTP server at work, and I checked its settings in the
Thunderbird mail client and it's using SSL over port 465, so a bit
different from how GMail's SMTP server is configured in Thunderbird.

I altered my code slightly to account for this:
mail_server = smtplib.SMTP_SSL()
mail_server.connect('mysmtpserver.at.work', 465)

Everything I left untouched. However, it doesn't work:
Traceback (most recent call last):
File "C:\Users\fencer\workspace\Send Email\src\send_email.py", line
16, in <module>
mail_server.ehlo()
File "C:\Python26\lib\smtplib.py", line 382, in ehlo
self.putcmd(self.ehlo_msg, name or self.local_hostname)
File "C:\Python26\lib\smtplib.py", line 318, in putcmd
self.send(str)
File "C:\Python26\lib\smtplib.py", line 310, in send
raise SMTPServerDisconnected('please run connect() first')
smtplib.SMTPServerDisconnected: please run connect() first

As you can see, I'm on Windows Vista.

What do I need to change in my code to fix this problem? Thunderbird
manages to do it! :) Ideally, I would like send an email that the
recipient can't reply to, but if doesn't work, it's fine too. I want to
use this to send automated emails to students with account information
for a course.

- Fencer
 
7

7stud

Hello, I'm using Python version 2.6.2 and I discovered it has built-in
libraries for sending email (imports smtplib and email). On the web I
discovered how to send mail through smtp.gmail.com:

mail_server = smtplib.SMTP()

mail_server.connect('smtp.gmail.com')
mail_server.ehlo()
mail_server.starttls()
mail_server.ehlo()
mail_server.login('username', 'password')

msg = MIMEText('Some message text')
msg['Subject'] = 'Some subject'
msg['From'] = 'Mr Underhill'
msg['To'] = 'someemailaddress'

me = 'myemailaddress'
mail_server.sendmail(me, ['someemailaddress'], msg.as_string())

That seems to work just fine but for the messages I will be sending I
don't want to use my private GMail account.
We have an SMTP server at work, and I checked its settings in the
Thunderbird mail client and it's using SSL over port 465, so a bit
different from how GMail's SMTP server is configured in Thunderbird.

I altered my code slightly to account for this:
mail_server = smtplib.SMTP_SSL()
mail_server.connect('mysmtpserver.at.work', 465)

Everything I left untouched. However, it doesn't work:
Traceback (most recent call last):
   File "C:\Users\fencer\workspace\Send Email\src\send_email.py", line
16, in <module>
     mail_server.ehlo()
   File "C:\Python26\lib\smtplib.py", line 382, in ehlo
     self.putcmd(self.ehlo_msg, name or self.local_hostname)
   File "C:\Python26\lib\smtplib.py", line 318, in putcmd
     self.send(str)
   File "C:\Python26\lib\smtplib.py", line 310, in send
     raise SMTPServerDisconnected('please run connect() first')
smtplib.SMTPServerDisconnected: please run connect() first

As you can see, I'm on Windows Vista.

What do I need to change in my code to fix this problem? Thunderbird
manages to do it! :) Ideally, I would like send an email that the
recipient can't reply to, but if doesn't work, it's fine too. I want to
use this to send automated emails to students with account information
for a course.

- Fencer

The docs say:

---
class smtplib.SMTP_SSL([host[, port[, local_hostname[, keyfile[,
certfile[, timeout]]]]]])

A SMTP_SSL instance behaves exactly the same as instances of SMTP.
SMTP_SSL should be used for situations where SSL is required from the
beginning of the connection and using starttls() is not appropriate.
---

So try getting ride of these two lines:
mail_server.starttls()
mail_server.ehlo()

There's also some info on google about a bug in this regard, so check
to see when you installed python 2.6.2.
 
F

Fencer

7stud wrote:
[snip]

Thanks for your reply. After consulting the sysadmins here I was able to
get it to work.

- Fencer
 
7

7stud

7stud wrote:

[snip]

Thanks for your reply. After consulting the sysadmins here I was able to
get it to work.

- Fencer


Ok, but how about posting your code so that a future searcher will not
be left screaming, "WHAT THE EFF WAS THE SOLUTION!!"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top