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.xyz.gov"
self.smtpport = "99"
self.emailfrom = "donotreplyError@somewhere.xyz.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()