-1

I'm fairly new to Python, trying to help automate a few things at work. I'm trying to use it to send out a message to a list of customers who have purchased with us in the past three months in an excel spreadsheet. I've got the below but it currently sends the html message as plain text. I've managed to get it to send the html message properly before, but wasn't able to combine pulling data from excel using pandas with sending the html message.

Any help would be very much appreciated.

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

your_name = "NAME"
your_email = "EMAIL"
your_password = "PASSWORD"


server = smtplib.SMTP('smtp.office365.com', 587)
server.starttls()
server.ehlo()
server.login(your_email, your_password)


email_list = pd.read_excel("python_test.xlsx")


all_names = email_list['Name']
all_emails = email_list['Email']

for idx in range(len(all_emails)):

    name = all_names[idx]
    email = all_emails[idx]
    subject = ‘Subject’


    message = MIMEMultipart('mixed')


    BODY = html = """\
    <html>
     Html message goes here 
    </html>
    """

    HTML_BODY = MIMEText(BODY, 'html')
    message.attach(HTML_BODY)

    full_email = ("From: {0} <{1}>\n"
                  "To: {2} <{3}>\n"
                  "Subject: {4}\n\n"
                  "{5}"
                  .format(your_name, your_email, name, email, subject, message,))

    try:
        server.sendmail(your_email, [email], full_email)
        print('Email to {} successfully sent!\n\n'.format(email))
    except Exception as e:
        print('Email to {} could not be sent :( because {}\n\n'.format(email, str(e)))

server.close()
  • don't you get error message ? You have wrong char `”` at the end `your_password = "PASSWORD”`. Even Stackoverflow has problem to highlight/colorize code becode this char is wrong. – furas Jun 22 '20 at 08:59
  • Python should have special method to attache HTML and plain text. As I know HTML is send as normal attachement and only mail clients/prgrams display it as HTML. Putting it using `format()` is not good idea - it has to add special header before every attachement but using `format()` to create string manually probably you don't add it. You could save in .eml your wrong mail and correctly created mail and compare what you have in both version - and you will see differences. – furas Jun 22 '20 at 09:04
  • BTW: now you have problem with wrong chars in `subject = ‘Subject’`. Is this your real code ? It gives error because you have again wrong chars. – furas Jun 22 '20 at 09:12

1 Answers1

0
# import the necessary components first
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
port = 2525 
smtp_server = "smtp.mailtrap.io"
login = "1a2b3c4d5e6f7g" # paste your login generated by Mailtrap
password = "1a2b3c4d5e6f7g" # paste your password generated by Mailtrap
sender_email = "mailtrap@example.com"
receiver_email = "new@example.com"
message = MIMEMultipart("alternative")
message["Subject"] = "multipart test"
message["From"] = sender_email
message["To"] = receiver_email
# write the plain text part
text = """\
Hi,
Check out the new post on the Mailtrap blog:
SMTP Server for Testing: Cloud-based or Local?
https://blog.mailtrap.io/2018/09/27/cloud-or-local-smtp-server/
Feel free to let us know what content would be useful for you!"""
# write the HTML part
html = """\
<html>
  <body>
    <p>Hi,<br>
       Check out the new post on the Mailtrap blog:</p>
    <p><a href="https://blog.mailtrap.io/2018/09/27/cloud-or-local-smtp-server">SMTP Server for Testing: Cloud-based or Local?</a></p>
    <p> Feel free to <strong>let us</strong> know what content would be useful for you!</p>
  </body>
</html>
"""
# convert both parts to MIMEText objects and add them to the MIMEMultipart message
part1 = MIMEText(text, "plain")
part2 = MIMEText(html, "html")
message.attach(part1)
message.attach(part2)
# send your email
with smtplib.SMTP("smtp.mailtrap.io", 2525) as server:
    server.login(login, password)
    server.sendmail(
        sender_email, receiver_email, message.as_string()
    )
print('Sent') 

example getted here https://blog.mailtrap.io/sending-emails-in-python-tutorial-with-code-examples/