-
Notifications
You must be signed in to change notification settings - Fork 0
/
sendemail.py
103 lines (74 loc) · 2.56 KB
/
sendemail.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python
import smtplib
import sys
import tempfile
from subprocess import call
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from decouple import config
SMTP_SERVER = config("SMTP_SERVER")
SMTP_PORT = config("SMTP_PORT")
EMAIL_ADDRESS = config("EMAIL_ADDRESS")
EMAIL_PASSWORD = config("EMAIL_PASSWORD")
def read_message():
"""
Opens an editor to receive a text or html message to be sent as the message body of the email.
:return:
"""
editor = config("EDITOR", default="vim")
with tempfile.NamedTemporaryFile(suffix=".tmp") as tf:
call([editor, tf.name])
tf.seek(0)
return tf.read()
def prepare_message():
"""
Prepares the message to be sent.
:return:
"""
msg_to_send = MIMEMultipart('alternative')
msg_to_send['Subject'] = raw_input("Enter the subject of the email: ")
msg_to_send['From'] = EMAIL_ADDRESS
msg_to_send.attach(MIMEText(read_message(), 'html'))
return msg_to_send
def smtp_connect():
"""
This function initializes and greets the smtp server.
It logs in using the provided credentials and returns the smtp server object as a result.
:return:
"""
server = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
server.ehlo()
server.starttls()
server.ehlo()
server.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
return server
def send_email(email, prepared_message):
"""
Send the email using the previous prepared message received as a parameter.
:param email:
:param prepared_message:
:return:
"""
server = smtp_connect()
server.sendmail(prepared_message['From'], email, prepared_message.as_string())
server.close()
if __name__ == "__main__":
print("""
Welcome to Mail Spammer.
This simples script reads a file with a list of emails that you will input next.
Then it will open a Vim text editor so you can enter the message that you want people in the list to receive.
After that, you just need to wait until it finish.
Enjoy.
""")
argfile = raw_input("Enter the path to the emails list file:")
argfile = open(argfile, 'r')
prepared_message = prepare_message()
for email in argfile.readlines():
try:
print("Sending to: " + email)
send_email(email, prepared_message)
print("Success!")
except:
e = sys.exc_info()[0]
print("Error: It was not possible to send your email to: " + email)
print("Message: " + e)