Sending email using mail.mime.multipart in Python

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

    # Internal text container
    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.

+5
source share
2 answers

You can start the local debug SMTP server. Find where smtpd.py is located, then run the command:

$ python /usr/lib/python2.7/smtpd.py -n -c DebuggingServer localhost:8025

Then, on the second terminal screen, start the Python interpreter:

>>> import smtplib
>>> s = smtplib.SMTP('localhost', 8025)
>>> s.sendmail('me', 'you', 'Hi!')

You should see "Hello!". on the first screen.

+2

smtplib.sendmail , - - . . , , .

smtp.me.com SMTP ( 25). , :

  • - 25
  • smtp.me.com 25 ( )
  • - , @mac.com ( myemail@mac.com : 550 5.1.1 unknown or illegal alias: myemail@mac.com )
  • smtp.me.com @mac.com(Mobile Me iCloud , , @mac.com - )

smtp.me.com, -, ( smtp.me.com, mac.com).

0

All Articles