tags:

views:

40

answers:

2

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

A: 

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()
Klark
I receive a NameError: Multipart is not defined
Phil
@Phil Have you imported it correctly? from email.mime.multipart import MIMEMultipart
krs1
Sorry there was a typo in the import
Phil
A: 

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

James