简体   繁体   中英

Send an email with SMTP in Python

I'm working on a project of making my own Instant-Messaging program, even without graphics or anything, just to get to know the built-in modules in python. Here, I tried to write a code that the user will input the username and password the user wants, and then a e-mail will be sent (to the user), that will contain a 12-character random string, and the user will input it back to the program. Somehow, when I run the code my whole computer freezes! Here's the code:

import smtplib
SMTPServer = smtplib.SMTP("smtp.gmail.com",587)
SMTPServer.starttls()
SMTPServer.login(USERNAME, PASSWORD)*

userEmail = raw_input("Please enter your e-mail: ")
if verifyEmail(userEmail) == False:
    while True:
        userEmail = raw_input("Error! Please enter your e-mail: ")
        if verifyEmail(userEmail) == True:
            break

randomString = generateRandomString()
message = """From: From Person <%s>
To: To Person <%s>
Subject: Ido's IM Program Registration

Your registration code is: %s
""" %(SERVEREMAIL, userEmail, randomString)

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

inputString = raw_input("Input generated code sent: ")

This is a working example of a smtp client. where does your code block?

# -*- coding: utf-8 -*-
import smtplib
from email.mime.text import MIMEText


class SMTPClient(object):
    def __init__(self, recepient, subject, body):
        self.recepient = recepient
        self.subject = subject
        self.body = body
        self.mail_host = conf.get('smtp_server.host')
        self.mail_port = conf.get('smtp_server.port')
        self.username = conf.get('account.username')
        self.password = conf.get('account.password')
        self.mail_sender = conf.get('account.from')
        self._setup()

    def _setup(self):
        self.smtp_client = smtplib.SMTP(self.mail_host, self.mail_port)
        self.smtp_client.ehlo_or_helo_if_needed()
        self.smtp_client.starttls()
        self.smtp_client.login(self.username, self.password)
        self._send_mail()

    def _make_mail(self):
        message = MIMEText(self.body, _charset='utf-8')
        message['From'] = self.mail_sender
        message['To'] = self.recepient
        message['Subject'] = self.subject
        return message.as_string()

    def send_mail(self):
        self.smtp_client.sendmail(self.mail_sender, self.recepient, self._make_mail())
        self.smtp_client.quit()

This works for me!

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email import Encoders

gmail_user = 'example@hotmail.com'
gmail_pwd = 'somepassword'
subject = 'HEY'
text = 'Some text here'

msg = MIMEMultipart()
msg['From'] = gmail_user
msg['To'] = gmail_user
msg['Subject'] = subject

msg.attach(MIMEText(text))

part = MIMEBase('application', 'octet-stream')

Encoders.encode_base64(part)

mailServer = smtplib.SMTP("smtp.live.com", 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(gmail_user, gmail_pwd)
mailServer.sendmail(gmail_user,gmail_user, msg.as_string())
mailServer.close()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM