Wchristner
Wchristner

Reputation: 187

Email Python 3 SMTPlib

I am working on a simple program with Python 3.4 on Windows 8.1, and being a newcomer to programming, I am a bit stumped.

When I send myself an email to my Godaddy office 365 account, the only thing I can see is the title "No Subject", (I would like to have something there.) I can see the From field which is my email address, and the rest is blank. Nothing in the body of the email. I figured I would ask someone who has more experience than I do as to what I am doing wrong. Here is my code, cleaned up a bit.

#Send Email
import smtplib
server=smtplib.SMTP('smtp.office365.com',587)
server.starttls()
server.login('mylogininfo','mypassword')
server.sendmail('[email protected]','[email protected]',"This is a test 
Pyhton Email")

I don't know if there is a better way of doing this but I am open to suggestions. Thanks Everyone.

Upvotes: 0

Views: 3933

Answers (1)

user5713018
user5713018

Reputation:

You should create your email with MIMEMultipart as follows:

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

def send_email():
    to_emails= "[email protected]"
    message="Hi, this is the email body"
    s = smtplib.SMTP(host='smtp.office365.com', port=587)
    s.starttls()
    s.login('mylogininfo','mypassword')
    msg = MIMEMultipart()
    msg['From']='mylogininfo'
    msg['To']=to_emails
    msg['Subject']="My Subject"
    msg.attach(MIMEText(message, 'plain'))
    s.send_message(msg)
    del msg
    s.quit()

send_email()

Upvotes: 1

Related Questions