Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Email problem

Tags:

python

email

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

like image 327
Phil Avatar asked Feb 27 '23 14:02

Phil


1 Answers

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()
like image 135
Klark Avatar answered Mar 07 '23 17:03

Klark