{"id":11,"date":"2024-09-11T10:20:11","date_gmt":"2024-09-11T15:20:11","guid":{"rendered":"https:\/\/danielbondurantcybersecurityportfolio.online\/?p=11"},"modified":"2024-11-07T10:04:57","modified_gmt":"2024-11-07T16:04:57","slug":"automating-email-with-python","status":"publish","type":"post","link":"https:\/\/danielbondurantcybersecurityportfolio.online\/?p=11","title":{"rendered":"Automating Email with Python"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">This post will discuss utilizing Python scripts to automate sending emails. For this example, we will be simulating sending an email based off of a triggered alert.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First, we will need to write and send an email with the template for the alert.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXdNRv8vOg-O3JLrBK93OubrmrsR0r175O8uiK7xQcJEUfqrQU2ohfMY9nZfgyNP-TC2tFgiNQYIsXCXe2jvps6DVJxSZR8AbsjqUi7Tq1FzpBuLWp1Zu9-e30PB3Gml7EmDYxxqBb9d6JZsDcqQc6Hummq2?key=jmQ7ECdivqHQnIuFN3F5gA\" width=\"624\" height=\"372\"><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">The all capitalized words will be replaced with the information pertinent to the email we want to send. The next step is to highlight the fist line of the email body and inspect the element to show us the HTML data. We can copy the HTML element and paste it into a new file. I will name it <code>message.html<\/code>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"998\" height=\"841\" src=\"https:\/\/danielbondurantcybersecurityportfolio.online\/wp-content\/uploads\/2024\/09\/image-3.png\" alt=\"\" class=\"wp-image-72\" style=\"width:680px;height:auto\" srcset=\"https:\/\/danielbondurantcybersecurityportfolio.online\/wp-content\/uploads\/2024\/09\/image-3.png 998w, https:\/\/danielbondurantcybersecurityportfolio.online\/wp-content\/uploads\/2024\/09\/image-3-300x253.png 300w, https:\/\/danielbondurantcybersecurityportfolio.online\/wp-content\/uploads\/2024\/09\/image-3-768x647.png 768w\" sizes=\"auto, (max-width: 998px) 100vw, 998px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXfZ_KZAvR9ZwdHcM54YxOgFogy9eDFCoVMmMBIn9h7U_Cl5Hrt90KfTEFYxDqHE7V2534S3HEifESOr5z1AUMilunsk6Kfia9zKCJ4Yh5cJ4z3AapNaPoDTFxmINww-2C6Tr6ybGywezVMCzFB7Ew3eM0M?key=jmQ7ECdivqHQnIuFN3F5gA\" width=\"624\" height=\"280\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">From here, we can check to see if the copied element gives us what we want by opening a browser and following the file pathway.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXfJYzBmBSU_6edTZtSVjcaXO9HdGxX_FSr5lFWSY9uQ7Y3ygoYyvxHeGvn_igqx6fJwFAJRWzXcHkrHOs5J_escUo0lvAFSyNDj5H3xAfoHgnJZQpNv3f4JRjl7-PoG9VojIyubSlsGcjAYNfRSru_nYyGh?key=jmQ7ECdivqHQnIuFN3F5gA\" width=\"624\" height=\"372\"><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">Looks like we are good.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now we need a Python script:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXcP5jG7obGNb74Rfs4dfLtDDHzZiUq5vz8iXuNRvNw8VYnjUVQlAeGNZujEWdURuK_fduvyLaTyXABwrgEq7isNZ4mFWszP7YZ-5-i9As-9adtlSGASIovA3su3azdMN8_0j9eW2uSkgduZQxTGB0rAhbg?key=jmQ7ECdivqHQnIuFN3F5gA\" width=\"624\" height=\"488\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">After we run this script, it creates the <code>sent_email.html<\/code> file. We can follow the file path in a browser again to see what the email will look like:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXeu43OLkjlWnk_7K2iZj4xHwxLy0GNtGjJfBuLcJ74aMMxN35ZC-ENvlCqKAhrMwDG-3cI0szHd4Ch7tszhXVP8eiyPyhoiEK66uphUd8ujbM8Re0QhC8CAmmLcgYWlZGSS5Osp1uV0C4D_MqtqkrrTfVDV?key=jmQ7ECdivqHQnIuFN3F5gA\" width=\"624\" height=\"308\"><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">As we can see, the all caps placeholder words have been replaced with the necessary data from the variables in the script.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now that we know the script is working, we need to modify it to send as an actual email, as well as have an email account set up to do the sending. We will tackle the second part first by going into the security settings of a Gmail account I created, enabling MFA, and going to app passwords. Enter the necessary info and we get an app password. The password is only shown once, so we need to write it down to use in the script. In a real world use case, we would want to use a secrets manager for better security, but this will suffice for this tutorial.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXf5XqgDRESQXupjWX-yaxIhPybY-iCMtY1KJGkUsFiJaZZZYKl4tHqjXfc3eb9HvwWp-A2GFlnsF7xS942dk9lWwXeKjwPV3T4qGWiKEdQBJElUc1nc8jpnQHamMTamFFXDeZAsg4dh3k8v3KhDFm7zStA?key=TD4t6X1vWJtUTintZ6OWiA\" width=\"624\" height=\"809\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXd8Tl0IJIAGVVG1sUfDjCeluWp4_xsvQgoSUut6jhSlZFOOPzdUYKhNk1AX6ingj9FXhBkenOLp6iWLQhjYbdP9C4A6-xOsjokrT7K4CJ4srHfRVuj5k4owihHoCuD27nnchVOHjI_YPOm5sXZI63dvP_4N?key=TD4t6X1vWJtUTintZ6OWiA\" width=\"624\" height=\"588\"><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">Now that we have the email account set up, we need a method to send an email via Python. For this tutorial we will use yagmail. Perform a pip3 install if needed, and then edit the script to add the necessary information.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"281\" height=\"57\" src=\"https:\/\/danielbondurantcybersecurityportfolio.online\/wp-content\/uploads\/2024\/09\/image-2.png\" alt=\"\" class=\"wp-image-71\" style=\"width:383px;height:auto\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Here is the new script. It is the same base script as before with some added elements. We have to import the yagmail package at the top in addition to the datetime package. I also commented out the the two lines that create and write to the <code>sent_email.html<\/code> file because we don&#8217;t need to write the file every time.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXehsm4DBj28XN9bVjNPID2fvskMNo1mOhxzwCzK52gio20B8rV4gMmAVnY1TKmMqE_YKOdyxfO5O120-n8Ps-eqjwBpwy5PlOtEBbX3u4ZIXYO_8Uaun5SAD44TvP72W8eqiPeaBqnVvEoh_6KBUKbknIQ?key=TD4t6X1vWJtUTintZ6OWiA\" width=\"544\" height=\"610\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"> In the variable section, I added a few variable definitions to incorporate into the yagmail section at the bottom.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"522\" height=\"188\" src=\"https:\/\/danielbondurantcybersecurityportfolio.online\/wp-content\/uploads\/2024\/08\/image-2.png\" alt=\"\" class=\"wp-image-20\" style=\"width:676px;height:auto\" srcset=\"https:\/\/danielbondurantcybersecurityportfolio.online\/wp-content\/uploads\/2024\/08\/image-2.png 522w, https:\/\/danielbondurantcybersecurityportfolio.online\/wp-content\/uploads\/2024\/08\/image-2-300x108.png 300w\" sizes=\"auto, (max-width: 522px) 100vw, 522px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">In this last section, the <code>email_body += line<\/code> will write each line into the email body. The last part is the yagmail block that takes everything and puts it together into the email and sends it out.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"364\" height=\"101\" src=\"https:\/\/danielbondurantcybersecurityportfolio.online\/wp-content\/uploads\/2024\/09\/image-1.png\" alt=\"\" class=\"wp-image-70\" style=\"width:483px;height:auto\" srcset=\"https:\/\/danielbondurantcybersecurityportfolio.online\/wp-content\/uploads\/2024\/09\/image-1.png 364w, https:\/\/danielbondurantcybersecurityportfolio.online\/wp-content\/uploads\/2024\/09\/image-1-300x83.png 300w\" sizes=\"auto, (max-width: 364px) 100vw, 364px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Now when we run the script, we want to see &#8220;sent&#8221; print out in the terminal:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXcGpvGOzr3ausLHpxFbOghg3BIFxLzpVamcS1DhHzGTXd22gEBQF0rLbFYLU4PLAvYYMiPOIOFT0m1iOypjbs46A8Ar9slC8jh-YVt3mo39pcR1VilGGhK5t_a6HZIE5VJi1J9aPQpacxUAExmcjjFOJQ15?key=TD4t6X1vWJtUTintZ6OWiA\" width=\"319\" height=\"134\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">And here is the email:<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXce3rBSuysvl9tq31xwE0RDD_NmF_E8RdM3N8bPCWW3i97Zx18u5e-CDDP6zcJbbTtcI1tKzSlu_IHDwTflrEex4ifkARLbC7z44ywaL8rdEKgPigvz8vuo8wsuSpHw42ivJooJQLA81v94gxJoPnSvF3Kp?key=TD4t6X1vWJtUTintZ6OWiA\" width=\"624\" height=\"289\"><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">Looks like everything is working correctly. In a real world production setting this script would be one part of a larger process to send out security alert information.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post will discuss utilizing Python scripts to automate sending emails. For this example, we will be simulating sending an email based off of a triggered alert. First, we will need to write and send an email with the template for the alert. The all capitalized words will be replaced with the information pertinent to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-11","post","type-post","status-publish","format-standard","hentry","category-python"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/danielbondurantcybersecurityportfolio.online\/index.php?rest_route=\/wp\/v2\/posts\/11","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/danielbondurantcybersecurityportfolio.online\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/danielbondurantcybersecurityportfolio.online\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/danielbondurantcybersecurityportfolio.online\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/danielbondurantcybersecurityportfolio.online\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=11"}],"version-history":[{"count":7,"href":"https:\/\/danielbondurantcybersecurityportfolio.online\/index.php?rest_route=\/wp\/v2\/posts\/11\/revisions"}],"predecessor-version":[{"id":142,"href":"https:\/\/danielbondurantcybersecurityportfolio.online\/index.php?rest_route=\/wp\/v2\/posts\/11\/revisions\/142"}],"wp:attachment":[{"href":"https:\/\/danielbondurantcybersecurityportfolio.online\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=11"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/danielbondurantcybersecurityportfolio.online\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=11"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/danielbondurantcybersecurityportfolio.online\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=11"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}