Skip to content

Commit a083398

Browse files
authored
Merge pull request Python-World#296 from tripleee/send-email-from-csv-formatting
Send_email_from_CSV: Update with proper email library, etc
2 parents 933393c + 95b202d commit a083398

2 files changed

Lines changed: 53 additions & 18 deletions

File tree

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,43 @@
11
# Send Emails from CSV File
22

3-
### Emails.csv : - Here CSV file Contain List Of Emails
3+
This project contains a simple bulk email script
4+
which sends the same message to a list of recipients.
45

5-
### Credentials.txt : -Contain information about Yoour Email
66

7-
1. Your Email Address
8-
2. Your Email Password
7+
## Dependencies
98

10-
### CSV module :- As it is lightweight than Pandas.
9+
This project only requires the Python standard library
10+
(more specifically, the `csv`, `email`, and `smtplib` modules).
11+
12+
13+
## Running the script
14+
15+
The script requires two configuration files:
16+
17+
* `emails.csv` should contain the email addresses to send the message to.
18+
* `credentials.txt` should contain your SMTP server login credentials,
19+
with your user name and your password on sepate lines,
20+
with no additional whitespace or other decorations.
21+
22+
The project's directory contains two example files which you'll
23+
probably both want and need to edit.
24+
25+
Once you have these files set up, simply
26+
27+
```
28+
python Send_emails.py
29+
```
30+
31+
32+
## Development ideas
33+
34+
A proper email sender would use `Cc:` or `Bcc:` and send the same
35+
message just once.
36+
37+
Don't play frivolously with this; your email provider,
38+
and/or the recipient's,
39+
may have automatic filters which quickly block anyone who sends
40+
multiple identical messages.
41+
42+
The script simply hardcodes the conventions for Gmail.com.
43+
Other providers may use a different port number and authentication regime.
Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import smtplib
21
import csv
2+
from email.message import EmailMessage
3+
import smtplib
34

45

56
def get_credentials(filepath):
67
with open("credentials.txt", "r") as f:
7-
Email_Address = f.readline()
8-
Email_Pass = f.readline()
9-
return (Email_Address, Email_Pass)
8+
email_address = f.readline()
9+
email_pass = f.readline()
10+
return (email_address, email_pass)
1011

1112

1213
def login(email_address, email_pass, s):
@@ -19,26 +20,27 @@ def login(email_address, email_pass, s):
1920
print("login")
2021

2122

22-
23-
def Send_mail():
23+
def send_mail():
2424
s = smtplib.SMTP("smtp.gmail.com", 587)
25-
Email_Address, Email_Pass = get_credentials("./credentials.txt")
26-
login(Email_Address, Email_Pass, s)
27-
25+
email_address, email_pass = get_credentials("./credentials.txt")
26+
login(email_address, email_pass, s)
2827

2928
# message to be sent
30-
subject = "Welcome to python"
29+
subject = "Welcome to Python"
3130
body = """Python is an interpreted, high-level,
3231
general-purpose programming language.\n
3332
Created by Guido van Rossum and first released in 1991,
3433
Python's design philosophy emphasizes code readability\n
3534
with its notable use of significant whitespace"""
36-
message = f"Subject : {subject} \n\n {body}"
35+
36+
message = EmailMessage()
37+
message.set_content(body)
38+
message['Subject'] = subject
3739

3840
with open("emails.csv", newline="") as csvfile:
3941
spamreader = csv.reader(csvfile, delimiter=" ", quotechar="|")
4042
for email in spamreader:
41-
s.sendmail(Email_Address, email[0], message)
43+
s.send_message(email_address, email[0], message)
4244
print("Send To " + email[0])
4345

4446
# terminating the session
@@ -47,4 +49,4 @@ def Send_mail():
4749

4850

4951
if __name__ == "__main__":
50-
Send_mail()
52+
send_mail()

0 commit comments

Comments
 (0)