I have the code written below:
class SendEmail:
def __init__(self, eSubject, eBody, eAttachmnts=None):
self.eSubject = eSubject
self.eAttachments = eAttachmnts
self.eBody = HTMLBody(eBody)
def setSend(self):
print("Enter your Email Credentials")
credsJson, usr, pWd = getCredential()
self.setEmailParams(usr, pWd)
def setEmailParams(self, usrNm, Pwd):
creds = Credentials(usrNm, Pwd)
config = Configuration(server=c.emailServer, credentials=creds)
account = Account(
primary_smtp_address=c.sourceSmtpAddress,
autodiscover=False,
config=config,
access_type=DELEGATE,
)
self.sendEmail(account)
def sendEmail(self, account):
msg = Message(
account=account,
folder=account.sent,
subject=self.eSubject,
body=self.eBody,
to_recipients=c.receipients,
)
for aName, aContent in self.eAttachments or []:
msg.attach(FileAttachment(name=aName, content=aContent))
msg.send_and_save()
I'm calling this from my main script as :
attachments = []
for i in attachmntLst:
with open(i, 'rb') as f:
content = f.read()
attachments.append((i, content))
emailObject = SendEmail(subjectLine, DF.to_html(), attachments)
emailObject.setSend()
subjectLine
is a string
. DF
is a pandas dataframe
. I want the dataframe to be printed in a tabular form as email body, hence used to_html()
. attachmentLst
is a list
of filenames that I want attached to the email.
The code works well in sending the email with appropriate email body in tabular format. However no attachments are added to it, and I don't see any error messages as well. What is going wrong?