python 发送 html email

 1 import smtplib
 2 
 3 from email.mime.multipart import MIMEMultipart
 4 from email.mime.text import MIMEText
 5 
 6 # me == my email address
 7 # you == recipient's email address
 8 me = "my@email.com"
 9 you = "your@email.com"
10 
11 # Create message container - the correct MIME type is multipart/alternative.
12 msg = MIMEMultipart('alternative')
13 msg['Subject'] = "Link"
14 msg['From'] = me
15 msg['To'] = you
16 
17 # Create the body of the message (a plain-text and an HTML version).
18 text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org"
19 html = """\
20 <html>
21   <head></head>
22   <body>
23     <p>Hi!<br>
24        How are you?<br>
25        Here is the <a href="http://www.python.org">link</a> you wanted.
26     </p>
27   </body>
28 </html>
29 """
30 
31 # Record the MIME types of both parts - text/plain and text/html.
32 part1 = MIMEText(text, 'plain')
33 part2 = MIMEText(html, 'html')
34 
35 # Attach parts into message container.
36 # According to RFC 2046, the last part of a multipart message, in this case
37 # the HTML message, is best and preferred.
38 msg.attach(part1)
39 msg.attach(part2)
40 
41 # Send the message via local SMTP server.
42 s = smtplib.SMTP('localhost')
43 # sendmail function takes 3 arguments: sender's address, recipient's address
44 # and message to send - here it is sent as one string.
45 s.sendmail(me, you, msg.as_string())
46 s.quit()
原文地址:https://www.cnblogs.com/harveyaot/p/3022581.html