-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmail.py
68 lines (53 loc) · 1.82 KB
/
mail.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python3
import smtplib
import csv
from email.message import EmailMessage
# MIME libraries
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# Your gmail credentials
gmail_name = 'your gmail account goes here'
gmail_password = 'your password goes here'
# Define the message
bodyHTML = open('email.html').read()
bodyPLAIN = open('email.txt').read()
# Basic function to send mails
def send_mail(body_html, body_plan, subject, to):
msg = MIMEMultipart('mixed')
msgR = MIMEMultipart('related')
msgA = MIMEMultipart('alternative')
# Add HTML and plain text version of the mail
msgR.attach(MIMEText(bodyHTML, 'html'))
msgA.attach(MIMEText(bodyPLAIN, 'plain'))
msgA.attach(msgR)
msg.attach(msgA)
msg['Subject'] = subject
msg['From'] = gmail_name
msg['To'] = to
# Send the email
try:
server_ssl.send_message(msg)
except:
print('Impossible to send the mail')
##########################################
# Open the connection to the server and login into it
try:
server_ssl= smtplib.SMTP_SSL('smtp.gmail.com', 465)
server_ssl.login(gmail_name,gmail_password)
except:
print("Impossible to set the connection")
with open('to_example.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
line_count = 0
for row in csv_reader:
if line_count != 0:
# Personalize email with name
bodyHTML = bodyHTML.replace("{}", row[0], 1)
bodyPLAIN = bodyPLAIN.replace("{}", row[0], 1)
send_mail(bodyHTML, bodyPLAIN, 'subject', row[1])
# Get back non-personalize version
bodyHTML = bodyHTML.replace(row[0], "{}", 1)
bodyPLAIN = bodyPLAIN.replace(row[0], "{}", 1)
line_count += 1
# Close the connection
server_ssl.quit()