Merge pull request #366 from 4rnv/master

Send bulk mail with individualized attachments
This commit is contained in:
Ayush Bhardwaj 2024-01-18 18:04:52 +01:00 committed by GitHub
commit 47926c3b58
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 120 additions and 0 deletions

View File

@ -0,0 +1,30 @@
# Mass_Email_Unique_Attachments
Sends mails to a list of addresses, with different attachments.
Useful for mailing personalized certificates, layoff letters and wedding invitations.
Reads data from a CSV file with email addresses and the names of the files which are to be sent as attachments.
# Setup
- Create a folder containing all attachments which are to be sent
- Create a CSV file with two columns, the name of the attachment file, and the mail address of the receiver
- Add the path of the folder and CSV file in your script
# Usage
There are two scripts, the native script will use the Outlook or Windows Mail to send the mails. The mails will be sent from your currently logged-in mail id. This script is suitable for Windows.
The SMTP script allows you to setup a SMTP server to send mails. Follow the comment instructions in the script to setup an SMTP server. This requires you to allow alternative sign-in from your email provider.
The `smtp_server` depends on which mail provider you are using. Common servers for major providers are:
- Yahoo!
- smtp.mail.yahoo.com
- Gmail
- smtp.gmail.com
- Outlook
- smtp.office365.com / smtp-mail.outlook.com
The password to be entered in `smtp_password` is generated from your email provider settings.
- https://hotter.io/docs/email-accounts/secure-app-gmail/
- https://superuser.com/questions/1521236/how-to-allow-less-secure-app-access-in-microsoft-email
- https://help.inspectionsupport.com/en/articles/392427-enable-less-secure-apps-for-smtp-use-isn-yahoo-mail
The first script is suitable for users who don't wish to setup an SMTP server, or don't want to generate a less secure key.
# Alteration
You can modify the code to change the type of attachments being sent, for example instead of PDFs, you can send Word documents by changing the `pdf_file_name = row['name'] + '.pdf'` to `pdf_file_name = row['name'] + '.docx'`. For images, use .png, .jpg, etc.
You can also comment out the CC option if you don't wish to CC the mail to anyone.

View File

@ -0,0 +1,37 @@
import win32com.client
import pandas as pd
import os
# Load the CSV file
csv_file = 'your_file_path_here' # Replace with the path to your CSV file, make sure to have \\ instead of \
df = pd.read_csv(csv_file)
# Path to the folder containing the PDFs
pdf_folder_path = 'your_folder_here' # Add the attachment folder you wish to send (name them as per the CSV file)
# Outlook setup
ol = win32com.client.Dispatch("outlook.application")
olmailitem = 0x0
# Sending each mail
for index, row in df.iterrows():
newmail = ol.CreateItem(olmailitem)
newmail.Subject = 'enter_subject_here'
newmail.To = row['email'] # Assuming your CSV has a column named 'email' with the mail address
newmail.CC = '' # cc; optional
newmail.Body = '' # text contents of your mail
# PDF file name and path
pdf_file_name = row['name'] + '.pdf' # Assuming your CSV has a column named 'name'
pdf_file_path = os.path.join(pdf_folder_path, pdf_file_name)
if os.path.exists(pdf_file_path):
newmail.Attachments.Add(pdf_file_path)
else:
print(f"PDF file not found for {row['name']}")
newmail.Send()
# Note: This script will send emails as soon as it's run. Be careful!

Binary file not shown.

View File

@ -0,0 +1,52 @@
import pandas as pd
import os
import smtplib
from email.message import EmailMessage
from email.mime.base import MIMEBase
from email import encoders
# Load the CSV file
csv_file = 'file_path' # CSV Path
df = pd.read_csv(csv_file)
# Path to the folder containing the PDFs
pdf_folder_path = 'Attachment_folder_path' # Add the attachment folder you wish to send (name them as per the CSV file)
# Email server configuration
smtp_server = '' # Replace with your SMTP server
smtp_port = 587 # Replace with your SMTP port (commonly 587 for TLS)
smtp_user = '' # Replace with your email address
smtp_password = '' # Replace with your unique access password (typically 16 letter generated through your email provider's settings)
# Sending each mail
for index, row in df.iterrows():
# Create a new email message
msg = EmailMessage()
msg['Subject'] = 'enter_subject_here'
msg['From'] = smtp_user
msg['To'] = row['email'] # Assuming your CSV has a column named 'email' for the address
msg['CC'] = '' # CC; optional
msg.set_content('') # The text contents of the mail
# PDF file name and path
pdf_file_name = row['name'] + '.pdf' # Assuming your CSV has a column named 'name' for the person you are sending to
pdf_file_path = os.path.join(pdf_folder_path, pdf_file_name)
# Check if the PDF file exists and attach it
if os.path.exists(pdf_file_path):
with open(pdf_file_path, 'rb') as f:
file_data = f.read()
file_type = 'application/pdf'
file_name = pdf_file_name
msg.add_attachment(file_data, maintype='application', subtype='pdf', filename=file_name)
else:
print(f"PDF file not found for {row['name']}")
# Send the email
with smtplib.SMTP(smtp_server, smtp_port) as server:
server.starttls()
server.login(smtp_user, smtp_password)
server.send_message(msg)
# This script will send emails as soon as it's run. Be careful!

View File

@ -28,6 +28,7 @@ So far, the following projects have been integrated to this repo:
|[AI for guess the number](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/AI_for_Guess_the_number) | [Omar Sameh](https://github.com/ShadowHunter15) |
|[Address locator](Location_Of_Adress) | [Chris]() |
|[Asymmetric Encryption](asymmetric_cryptography) |[victor matheus](https://github.com/victormatheusc) |
|[Attachment Unique Mail](Attachment_Unique_Mail) |[Arnav Dandekar](https://github.com/4rnv) |
|[Automated calendar](automated_calendar) | [J.A. Hernández](https://github.com/jesusalberto18) |
|[Automated emails](automated_email) | [Suvigya](https://github.com/SuvigyaJain1) |
|[Battery_notification](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Battery_notification/)|[Krishna Sharma](https://github.com/krishnasharma1386)|