While the debugging server in the terminal was good enough to get a feel for how smtplib and email.mime work, it'll be easier to use a more traditional email provider to test out attaching a document and using a an email template. If you run the script above and everything goes according to plan, you should get the message below. Using the MIME Multipart standard, not only can we include HTML, we can also define the email subject, and what should show up in the From/To fields. Local_ndmail(sender_email, receiver_email, message.as_string()) With smtplib.SMTP(smtp_server, port) as local_server: Message = 'We know where the hobbits are going!' Once again, let's set up a basic message and send it, this time with some bolded text and a hyperlink. To send a MIME-formatted email, we'll need to use the email.mime module. If you want to learn more about MIME, you can check out the W ikipedia page, but for the most part it is a standard that allows you to include more than basic ASCII characters in your emails. To include any extra markup, we'll need to send a MIME multipart email. END MESSAGE - Sending a Simple HTML EmailĮven if you are just emailing a family member, a plain text email might be a little too plain-just bolding and italicizing text still requires HTML. If you run the script above, you should see the output below from your debugging server! - MESSAGE FOLLOWS -ī"They're taking the hobbits to Isengard!" Local_ndmail(sender_email, receiver_email, message) With our email addresses and message content set up, we can actually send an email! with smtplib.SMTP(smtp_server, port) as local_server: Sender_email = "They're taking the hobbits to Isengard!" Start out by importing smtplib, setting up your server, and defining some basic info. In a separate terminal or script, we can now get started on sending an email. Open up a new terminal prompt and run this command: python -m smtpd -c DebuggingServer -n localhost:1025 Let's start by setting up a debugging server running locally so that we don't have to mess around with any external services (yet). Personalizing the Template and Sending with Python
0 Comments
Leave a Reply. |