Python Program to Merge Mails

Python Program to Merge Mails: In this program, you’ll learn to merge mails into one. Mail merge is a process of doing this. Instead of writing each mail separately, we have a template for the body of the mail and a list of names that we merge together to form all the mails.

Python Program to Merge Mails

Source Code to merge mails

import csv, smtplib
port = 2525 
smtp_server = "smtp.mailtrap.io"
login = "1a2b3c4d5e6f7g" # paste your login generated by Mailtrap
password = "1a2b3c4d5e6f7g" # paste your password generated by Mailtrap

message = """Subject: Your test score
To: {recipient}
From: {sender}

Hi {name}, thanks for accepting our challenge! Your current score is {score}. Contact us if you have further questions.
Cheers! """
sender = "[email protected]"

with smtplib.SMTP(smtp_server, port) as server:
    server.login(login, password)
    with open("students.csv") as file:
        reader = csv.reader(file)
        next(reader)  # add this to skip the header row
        for name, email, score in reader:
            server.sendmail(
               sender,
                email,
                message.format(name=name, recipient=email, sender=sender, score=score)
            )
            print(f'Sent to {name}')