5

I'm using the script below to send an email to myself, the script runs fine with no errors but I don't physically receive an email.

import smtplib

sender = '[email protected]'
receivers = ['[email protected]']

message = """From: From Person <[email protected]>
To: To Person <[email protected]>
Subject: SMTP e-mail test

This is a test e-mail message.
"""

try:
   smtpObj = smtplib.SMTP('localhost')
   smtpObj.sendmail(sender, receivers, message)         
   print "Successfully sent email"
except SMTPException:
   print "Error: unable to send email"

EDIT

The script is named test.py

1
  • 5
    If I ever physically received an email, I'd have to have a talk with my shrink. Commented Jul 27, 2010 at 19:09

4 Answers 4

5

Why you use localhost as the SMTP?

If you are using hotmail you need to use hotmail account, provide the password, enter port and SMTP server etc.

Here is everything you need: http://techblissonline.com/hotmail-pop3-and-smtp-settings/

edit: Here is a example if you use gmail:

def mail(to, subject, text):
    msg = MIMEMultipart()

    msg['From'] = gmail_user
    msg['To'] = to
    msg['Subject'] = subject

    msg.attach(MIMEText(text))

    part = MIMEBase('application', 'octet-stream')
    Encoders.encode_base64(part)
    msg.attach(part)

    mailServer = smtplib.SMTP("smtp.gmail.com", 587)
    mailServer.ehlo()
    mailServer.starttls()
    mailServer.ehlo()
    mailServer.login(gmail_user, gmail_pwd)
    mailServer.sendmail(gmail_user, to, msg.as_string())
    # Should be mailServer.quit(), but that crashes...
    mailServer.close()
Sign up to request clarification or add additional context in comments.

3 Comments

I receive a NameError: Multipart is not defined
@Phil Have you imported it correctly? from email.mime.multipart import MIMEMultipart
Sorry there was a typo in the import
2

I have something to add to Klark's great answer. When I try:

Encoders.encode_base64(part)

I received an error

NameError: global name 'Encoders' is not defined

It should be

encoders.encode_base64(msg)

https://docs.python.org/2/library/email-examples.html

Comments

1

Jeff Atwood's blog post from last April may be of some help.

Comments

0

The "localhost" SMTP server won't work with Hotmail. You'll have to hard-code your password in so Hotmail can authenticate you as well. The default SMTP for Hotmail is "smtp.live.com" on port 25. Try:

import smtplib

sender = '[email protected]'
receivers = ['[email protected]']
password = 'your email password'

message = """From: From Person <[email protected]>
To: To Person <[email protected]>
Subject: SMTP e-mail test

This is a test e-mail message.
"""

try:
   smtpObj = smtplib.SMTP("smtp.live.com",25)
   smtpObj.ehlo()
   smtpObj.starttls()
   smtpObj.ehlo()
   smtpObj.login(sender, password)
   smtpObj.sendmail(sender, receivers, message)         
   print "Successfully sent email"
except SMTPException:
   print "Error: unable to send email"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.