I have a script below that emails and is working fine. It attaches a document no issues...
I pieced this together with a few examples but want to format the email body and add attachments in the body of the email.
I tried to change it to but it errors out...
msg = MIMEMultipart()
It appears that this section is laying out the body of the email...
dot_mail.send_email("++++++++\n Environment : {0} \n++++++++\n Build_New: \n \n++++++++\n {1}".format("something",now),
"process {0}".format(now.date()), emailto55)
How can this be modified to allow me to add more text to the BODY and images to the BODY????
import arcpy
import sys
import os
from datetime import datetime
import smtplib
import pandas as pd
from email.message import EmailMessage
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
emailto55 = ""
def main():
class DOTEmail:
def __init__(self):
self.smtphost = "csmtp.cov.xxx.gov"
self.smtpport = "99"
self.emailfrom = "donotreplyError@somewhere.xxx.gov"
def send_email(self, content, subj, emailto55):
pdf_File = r"C:\Users\\Instructions.pdf"
pdf_file_Name = "Instructions.pdf"
msg = EmailMessage()
msg['Subject'] = "attachment 5"
msg['From'] = self.emailfrom
msg['To'] = "someone@yahoo.com"
#body = MIMEText("example email body")
#msg.attach(body)
msg.set_content(content)
with open(pdf_File, 'rb') as f:
file_data = f.read()
msg.add_attachment(file_data, maintype="application", subtype="pdf", filename=pdf_file_Name)
vdotsmtp = smtplib.SMTP(self.smtphost, self.smtpport)
vdotsmtp.sendmail(self.emailfrom, emailto55, msg.as_string())
vdotsmtp.quit()
try:
cnt = 0
if cnt == 0:
print("In here")
now = datetime.now()
dot_mail = DOTEmail()
dot_mail.send_email("++++++++\n Environment : {0} \n++++++++\n Build_New: \n \n++++++++\n {1}".format("something",now),
"process {0}".format(now.date()), emailto55)
sys.exit(1)
else:
now = datetime.now()
print(now)
except Exception as e:
now = datetime.now()
print('Failed')
if __name__ == "__main__":
main()
There's a variety of ways you could go about customizing the email body content. Since it looks like the different sections are formatted with the same separators, you might consider something like this to avoid repeating the separators.
try:
cnt = 0
if cnt == 0:
print("In here")
now = datetime.now()
dot_mail = DOTEmail()
email_body_environment = "Environment : {}".format("something")
email_body_build = "Build_New: {}\n".format(" ")
email_body_date = "{}".format(now)
email_body_some_more_content = "Additional Content: {}".format("a variable if you want it")
email_body_parts = [
email_body_environment,
email_body_build,
email_body_date,
email_body_some_more_content
]
email_body = "++++++++\n {}".format(" \n++++++++\n ".join(email_body_parts)
email_subject = "process {0}".format(now.date())
dot_mail.send_email(email_body, email_subject, emailto55)
sys.exit(1)
else:
now = datetime.now()
print(now)
except Exception as e:
now = datetime.now()
print('Failed')
Just make more variables with text content as needed and add them to email_body_parts in the order you want.
As for inserting an image into the body, I'm not sure. Sorry.