I am trying to learn python from a book ("Hello! Python"). This code should, according to the book, send an email. until lucky.
import os
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib
def send_message(message):
s = smtplib.SMTP('smtp.me.com')
s.sendmail(message['From'], message['To'], message.as_string())
s.quit()
def mail_report(to, ticker_name):
outer = MIMEMultipart()
outer['Subject'] = "Stock report for " + ticker_name
outer['From'] = "myemail@mac.com"
outer['To'] = to
inner = MIMEMultipart('alternative')
text = "Here is the stock report for " + ticker_name
html = """\
<html>
<head></head>
<body>
<p>Here is teh stock report for
<b> """ + ticker_name + """ </b>
</p>
</body>
</html>
"""
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
inner.attach(part1)
inner.attach(part2)
outer.attach(inner)
filename = 'stocktracker-%s.csv' % ticker_name
csv_text = ''.join(file(filename).readlines())
csv_part = MIMEText(csv_text, 'csv')
csv_part.add_header('Content-Disposition', 'attachment', filename=filename), outer.attach(csv_part)
return outer
if __name__ == '__main__':
email = mail_report('myemail@mac.com', 'GOOG')
send_message(email)
I do not receive an error message, but I also do not receive an email. (needless to say, I'm using my actual email address, not " myemail@mac.com "). All suggestions and recommended readings are appreciated.
source
share