Created
February 9, 2017 10:33
-
-
Save Dih5/c57f0155d8b36f595dea7fd3f9d204d8 to your computer and use it in GitHub Desktop.
Python mailing script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example of mailing script. Quick and (a little) dirty. | |
# Dih5 - 2017 | |
# Spam is not cool. Thou shalt not spam! | |
import smtplib | |
import email | |
from email.mime.multipart import MIMEMultipart | |
from email.mime.text import MIMEText | |
from email.mime.base import MIMEBase | |
from email import encoders | |
import traceback | |
import itertools | |
# Make sure to active lesser secure apps here: | |
# https://www.google.com/settings/security/lesssecureapps | |
fromaddr = "myaddress@gmail.com" | |
mypasswd = "passwordIsExposedInThisFile" | |
address_file = "ToSend.lst" # Email addresses separated by \n | |
# Amount of recipients in a single message (sent as BCC) | |
block = 50 | |
# Max number of messages to send when the script is run | |
max_msgs = 5 | |
def grouper(iterable, n, fillvalue=None): | |
"Collect data into fixed-length chunks or blocks" | |
# grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx" | |
args = [iter(iterable)] * n | |
return itertools.zip_longest(*args, fillvalue=fillvalue) | |
msg = MIMEMultipart() | |
msg['From'] = fromaddr | |
#msg['To'] = [fromaddr] # No data in the header, all BCC | |
msg['Subject'] = "The email subject" | |
body = "This is the text of the email" | |
msg.attach(MIMEText(body, 'plain')) | |
filename = "Sample.pdf" # Name that will be used to send the file | |
attachment = open("Sample.pdf", "rb") # Path to the file | |
part = MIMEBase('application', 'octet-stream') | |
part.set_payload((attachment).read()) | |
encoders.encode_base64(part) | |
part.add_header('Content-Disposition', "attachment; filename= %s" % filename) | |
msg.attach(part) | |
with open(address_file, 'r') as f: | |
addr_list = f.readlines() | |
addr_list=list(map(lambda s:s.strip(),addr_list)) | |
try: | |
i=0 | |
msgs_sent = 0 | |
server = smtplib.SMTP('smtp.gmail.com', 587) | |
server.starttls() | |
server.login(fromaddr, mypasswd) | |
for to_addr in grouper(addr_list, block): | |
if msgs_sent >= max_msgs: | |
print("************\nDesired number of messages sent. Aborting further sending.") | |
raise Exception("Max messages sent") | |
#msg.replace_header('To', to_addr) | |
text = msg.as_string() | |
server.sendmail(fromaddr, [s for s in to_addr if s is not None], text) | |
print(to_addr) | |
i+=block | |
msgs_sent += 1 | |
except Exception: | |
print("************\nProcess failed while sending to " + addr_list[i]) | |
print(traceback.format_exc()) | |
finally: | |
with open(address_file,'w') as f: | |
for item in addr_list[i:]: | |
f.write("{}\n".format(item)) | |
print("List of recipients updated") | |
server.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment