|
| 1 | +#!/usr/bin/env/python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +from __future__ import print_function |
| 4 | + |
| 5 | +try: |
| 6 | + import smtplib |
| 7 | +except: |
| 8 | + print("{ smtplib module is missing. Try installing it and try again }\n") |
| 9 | + exit(-1) |
| 10 | +import time |
| 11 | +import sys |
| 12 | + |
| 13 | +from email.mime.multipart import MIMEMultipart |
| 14 | +from email.mime.text import MIMEText |
| 15 | +from getpass import getpass |
| 16 | +from string import Template |
| 17 | + |
| 18 | +''' |
| 19 | +$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ |
| 20 | +$$ $$ |
| 21 | +$$ This project was written for EDUCATION PURPOSE ONLY to understand the SMTP $$ |
| 22 | +$$ functionality as part of python programming. Any illegal use of the tool $$ |
| 23 | +$$ such as spamming is strictly prohibited without the users consent/knowledge $$ |
| 24 | +$$ STAY SAFE AND KEEP CODING $$ |
| 25 | +$$ !THE FUTURE IS IN THE HANDS OF OPEN SOURCE DEVELOPERS! $$ |
| 26 | +$$ $$ |
| 27 | +$$ $$ |
| 28 | +$$ By Stevemats &fkinaro $$ |
| 29 | +$$ $$ |
| 30 | +$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ |
| 31 | +''' |
| 32 | +print(''' |
| 33 | +
|
| 34 | +
|
| 35 | + __ |
| 36 | + o /' ) |
| 37 | + /' ( , |
| 38 | + __/' ) .' `; |
| 39 | + o _.-~~~~' ``---..__ .' ; |
| 40 | + _.--' b) ``--...____.' .' |
| 41 | + ( _. )). `-._ < The green shark world!! |
| 42 | + `\|\|\|\|)-.....___.- `-. __...--'-.'. |
| 43 | + stv `---......____...---`.___.'----... .' `.; |
| 44 | + `-` ` |
| 45 | +''') |
| 46 | +try: |
| 47 | + SERVER_MAIL = input("\n >>>Enter your server email_address:".lower()) |
| 48 | + PASSWORD = getpass(">>>Input your password:") # 'Your server email password' |
| 49 | +except NameError: |
| 50 | + input = raw_input # python 2 |
| 51 | + |
| 52 | + |
| 53 | +def slowprint(thrasher): |
| 54 | + for n in thrasher + '\n': |
| 55 | + sys.stdout.write(n) |
| 56 | + sys.stdout.flush() |
| 57 | + time.sleep(10. / 100) |
| 58 | + |
| 59 | + |
| 60 | +slowprint("Congratulation! Now you can Take a sip of coffee as we automate your mail delivery... ") |
| 61 | +time.sleep(5) |
| 62 | + |
| 63 | + |
| 64 | +def get_contacts(filename): |
| 65 | + """ |
| 66 | + Return two lists names, emails containing names and email addresses |
| 67 | + read from a file specified by filename. |
| 68 | + """ |
| 69 | + |
| 70 | + names = [] |
| 71 | + emails = [] |
| 72 | + with open(filename, mode='r', encoding='utf-8') as contacts_file: |
| 73 | + for a_contact in contacts_file: |
| 74 | + names.append(a_contact.split()[0]) |
| 75 | + emails.append(a_contact.split()[1]) |
| 76 | + return names, emails |
| 77 | + |
| 78 | + |
| 79 | +def read_template(filename): |
| 80 | + """ |
| 81 | + Returns a Template object comprising the contents of the |
| 82 | + file specified by filename. |
| 83 | + """ |
| 84 | + |
| 85 | + with open(filename, 'r', encoding='utf-8') as template_file: |
| 86 | + template_file_content = template_file.read() |
| 87 | + return Template(template_file_content) |
| 88 | + |
| 89 | + |
| 90 | +def main(): |
| 91 | + """ TODO: code functionality to lower all input mails to lowercase $ validate""" |
| 92 | + names, emails = get_contacts('contacts.txt') # read contacts(email and username) |
| 93 | + message_template = read_template('message.txt') # read message(your mail content) |
| 94 | + |
| 95 | + # Highly define your SMTP server here |
| 96 | + s = smtplib.SMTP(host='smtp.gmail.com', port=587) |
| 97 | + s.starttls() |
| 98 | + s.login(SERVER_MAIL, PASSWORD) |
| 99 | + |
| 100 | + # For each contact, send the email: |
| 101 | + for name, email in zip(names, emails): |
| 102 | + msg = MIMEMultipart() # create a message |
| 103 | + |
| 104 | + # add in the actual person name to the message template |
| 105 | + message = message_template.substitute(PERSON_NAME=name.title()) |
| 106 | + |
| 107 | + # Prints out the message body for our sake(not necessary) |
| 108 | + print(message) |
| 109 | + |
| 110 | + # setup the parameters of the message |
| 111 | + msg['From'] = SERVER_MAIL |
| 112 | + msg['To'] = email |
| 113 | + msg['Subject'] = "This is Just A Sample Mail Test" |
| 114 | + |
| 115 | + # add in the message body |
| 116 | + msg.attach(MIMEText(message, 'plain')) |
| 117 | + |
| 118 | + # send the message via the server set up earlier. |
| 119 | + """ TODO: code functionality to send mails at an interval to avoid red flags $ meet limits""" |
| 120 | + s.send_message(msg) |
| 121 | + del msg |
| 122 | + |
| 123 | + # s = your_server |
| 124 | + s.quit() |
| 125 | + print("Email successfully sent.") |
| 126 | + |
| 127 | + |
| 128 | +if __name__ == '__main__': |
| 129 | + main() |
0 commit comments