I am trying to remove the header section, currently the received email looks like this:
.
I don't want it to be displayed when the receiver gets the email, I am not sure how to hide it.
I tried to just delete it but received an error. That code is included but commented out below.
import donator
import csv
import os
import smtplib
from email.message import EmailMessage
from email.mime.text import MIMEText
def ReadEmailTemplate():
oFile = open('EmailTemplate.txt', 'r')
Subject = oFile.readline()
Body = oFile.read()
return [Subject, Body]
def ReadCSVFile():
with open('ExampleData.csv', 'r') as csv_file:
csv_reader = csv.reader(csv_file)
allDonators = []
for line in csv_reader:
Number = line[0]
Name = line[1]
Amount = line[2]
Date = line[3]
Mode = line[4]
Email = line[5]
allDonators.append(donator.Donator(Number, Name, Amount, Date, Mode, Email))
return allDonators
txt = ReadEmailTemplate()
Subject = txt[0]
Body = txt[1]
# READ VALUES FROM CSV FILE AND RUN THROUGH THEM
allDonators = ReadCSVFile()
# CREATE EMAIL WITH THOSE VALUES
for donator in allDonators:
print(f'Sending email to: {donator.Name} at {donator.Email}...')
msg = EmailMessage()
Body = MIMEText(
Body.replace('REPLACE_DATE', donator.Date).replace('DONATION_AMOUNT', donator.Amount).replace('DONATION_DATE',
donator.Date))
msg.set_content(Body)
msg['From'] = os.environ['PERS_EMAIL']
msg['To'] = donator.Email
msg['Subject'] = Subject.replace('CUSTOMER_NAME', donator.Name)
print(msg.get('Content-Type'))
print(msg.get('Content-Transfer-Encoding'))
print(msg.get('MIME-Version'))
#for hdr in ['Content-Type', 'Content-Transfer-Encoding', 'MIME-Version']:
# del msg[hdr]
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(os.environ['PERS_EMAIL'], os.environ['PERS_EMAILPW'])
smtp.sendmail(os.environ['PERS_EMAIL'], donator.Email, msg.as_string())
smtplib.SMTP_SSL('smtp.gmail.com', 465).quit()
In ReadEmailTemplate
, you have Subject = oFile.readline()
. file.readline()
leaves the newline '\n'
in the line. So, your subject line is followed by a blank line, which terminates the headers, and the rest of the headers are seen as message. Just do
Subject = oFile.readline().strip()
Note that you don't have to split out the fields of your CSV:
for line in csv_reader:
allDonators.append(donator.Donator(*line))