0

I am trying to attach the file with a message in this program, so I can send an email message along with the attachment but I'm unable to do it. it would very nice if anyone helps me out with my final assignment! Thanks in Advance

import pandas as pd
import smtplib
import credentials
#import getpass

'''
Change these to your credentials and name
'''
your_name = credentials.name
your_email = credentials.email
your_password = credentials.password

server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.ehlo()
server.login(your_email, your_password)

# Read the file
email_list = pd.read_excel("EmailList.xlsx")

# Get all the Names, Email Addreses, Subjects and Messages
all_names = email_list['Name']
all_emails = email_list['Email']
all_subjects = email_list['Subject']
all_messages = email_list['Message']

# Loop through the emails
for idx in range(len(all_emails)):

    # Get each records name, email, subject and message
    name = all_names[idx]
    email = all_emails[idx]
    subject = all_subjects[idx]
    message = all_messages[idx]

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

    # In the email field, you can add multiple other emails if you want
    # all of them to receive the same text
    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)))

# Close the smtp server
server.close()
  • If you want to send an attachment you need to use the techniques described in the answers to linked duplicate. (It's possible in theory to construct one manually using just strings, but in practice it would be tedious to program and difficult to get right) – snakecharmerb Nov 24 '20 at 09:21
  • See also the [examples in the official docs](https://docs.python.org/3/library/email.examples.html) – snakecharmerb Nov 24 '20 at 09:22

0 Answers0