
上QQ阅读APP看书,第一时间看更新
There's more...
If the objective is a bigger operation, like a marketing campaign, or even production emails like confirming a user's email, please check Chapter 8, Dealing with Communication Channels
The email message content used in this recipe is very simple, but emails can be much more complicated than that.
The To field can contain multiple recipients. Separate them with commas, like this:
message['To'] = ','.join(recipients)
Emails can be defined in HTML, with an alternative plain text, and have attachments. The basic operation is to set up a MIMEMultipart and then attach each of the MIME parts that compose the email:
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
message = MIMEMultipart()
part1 = MIMEText('some text', 'plain')
message.attach(part1)
with open('path/image', 'rb') as image:
part2 = MIMEImage(image.read())
message.attach(part2)
The most common SMPT connection is SMPT_SSL, which is more secure and requires a login and password, but plain, unauthenticated SMPT exists; check your email provider documentation.
Remember that this recipe is aimed for simple notifications. Emails can grow quite complex if attaching different information. If your objective is an email for customers or any general group, try to use the ideas in Chapter 8, Dealing with Communication Channels.