Home > other >  Email python programming problems
Email python programming problems

Time:01-12

This is a core "python programming (third edition)" in the third chapter example 3-4, two different types of task is created and sent E-mail messages, after the operation can't send out, don't know how to modify, another question, is the password input code does not require the sender ah, is this why? Mystery, expert advice please!
 #! The/usr/bin/env python 
'email - exammples. Py - demo creation of email messages'

From email. Mime. Image import MIMEImage
From email. Mime. Multipart import MIMEMultipart
From email. Mime. The text import MIMEText
The from smtplib import SMTP

SENDER='SENDER email @ 126. com'
RECIPS='recipient email @ 126. com'
SOME_IMG_FILE='a certain image. JPG # image in the current folder

# multipart the alternative: text and HTML
Def make_mpa_msg () :
Email=MIMEMultipart (' alternative ')
Text=MIMEText (' Hello World! \ r \ n ', 'plain')
Email. Attach (text)
HTML=MIMEText (
' <body>

' ', 'HTML')
Email. Attach (HTML)
Return email


# multipart: '
Def make_img_msg (fn) :
F=open (fn, 'r')
Data=(https://bbs.csdn.net/topics/f.read)
F. lose ()
Email=MIMEImage (data, name=fn)
Email. Add_header (' Content - Disposition,
'the attachment; Filename="% s" '% fn)
Return email


Def sendMsg (fr, to, MSG) :
S=SMTP (' localhost ')
Is incremented=s.s endmail (fr, to, MSG)
S.q uit ()


If __name__=="__main__ ':
Print (' Sending multipart the alternative MSG.. ')
MSG=make_mpa_msg ()
MSG=[' From '] SENDER
MSG=[' To '] ', '. Join (RECIPS)
MSG=[' Subject '] 'multipart the alternative test'
SendMsg (SENDER, RECIPS, MSG. As_string ())

Print (' Sending image MSG... ')
MSG=make_img_msg (SOME_IMG_FILE)
MSG=[' From '] SENDER
MSG=[' To '] ', '. Join (RECIPS)
MSG=[' Subject '] 'image file test'
SendMsg (SENDER, RECIPS, MSG. As_string ())

  • Related