Created
March 19, 2024 13:49
-
-
Save Shaam-K/b562c77f6b6747ad5249c78eb9887b16 to your computer and use it in GitHub Desktop.
Sending emails with attachments from csv file in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import smtplib | |
| import ssl | |
| from email.message import EmailMessage | |
| from email.mime.base import MIMEBase | |
| from email import encoders | |
| import csv | |
| from PIL import Image | |
| from PIL import ImageDraw | |
| from PIL import ImageFont | |
| from io import BytesIO | |
| # Define email sender and receiver | |
| email_sender = 'YOUR EMAIL ID HERE' | |
| email_password = 'YOUR APP PASSWORD HERE' | |
| def addTextImage(name): | |
| img = Image.open('image location here (.jpg format)') | |
| # Call draw Method to add 2D graphics in an image | |
| I1 = ImageDraw.Draw(img) | |
| nameFormat = name.title() | |
| if len(nameFormat) > 25: | |
| myFont = ImageFont.truetype('font location here (.tff format)', 55) | |
| else: | |
| myFont = ImageFont.truetype('font location here (.tff format)', 80) | |
| # Add Text to an image : 810, 690 are x and y coordinates, fill is text color (takes rgb values) | |
| I1.text((810, 690), nameFormat, font=myFont, fill =(128, 10, 5),anchor='ms') | |
| imgByte = BytesIO() | |
| img.save(imgByte,format='JPEG') | |
| return imgByte.getvalue() | |
| failed_mails = [] # all emails that didnt get the mail will be stored here | |
| # Set the subject of the email | |
| subject = 'Some Subject' | |
| # Looping through the CSV file and sending emails with attachments | |
| with open('location to email_list (.csv format)', mode='r') as sendFile: | |
| sendFileCSV = csv.DictReader(sendFile) # each line in csv is converted into a dictionary | |
| for entry in sendFileCSV: | |
| email_name = entry['Name'] | |
| email_receiver = entry['Email'] | |
| # IMP: To leave space after a line, make sure to add <br> tag | |
| body = ''' | |
| Hey {}, <br> | |
| <br> | |
| example: format | |
| Thanks for attending the event "Software Development Program", we hope you gained valuable insights from our guest speakers. | |
| <br> | |
| <br> | |
| Your participation certificate has been attached to this mail :) <br> | |
| '''.format(email_name) | |
| em = EmailMessage() | |
| em['From'] = email_sender | |
| em['To'] = email_receiver | |
| em['Subject'] = subject | |
| em.set_content(body) | |
| em.add_alternative(body, subtype='html') | |
| payload = MIMEBase('application', 'octate-stream') | |
| payload.set_payload(addTextImage(email_name)) | |
| encoders.encode_base64(payload) #encode the attachment | |
| #add payload header with filename | |
| payload.add_header('Content-Disposition', 'attachment', filename=f"NAME OF ATTACHMENT{email_name.upper()}.jpg") | |
| em.attach(payload) | |
| # Add SSL (layer of security) | |
| context = ssl.create_default_context() | |
| # Log in and send the email | |
| with smtplib.SMTP_SSL('smtp.gmail.com', 465, context=context) as smtp: | |
| try: | |
| smtp.login(email_sender, email_password) | |
| smtp.sendmail(email_sender, email_receiver, em.as_string()) | |
| except: | |
| failed_mails.append(email_name + '--' + email_receiver) | |
| print('Sent: ', email_name + '--' + email_receiver) | |
| print('Sent full batch') | |
| print('failed mails:', failed_mails) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment