commit 6372173fa3168a5e87998dcf95820c2a370c8692 Author: thisfro Date: Mon Dec 7 15:01:09 2020 +0100 initial commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..8e56642 --- /dev/null +++ b/README.md @@ -0,0 +1,11 @@ +# pymail + +Easily send mails via SMTP and python + +## Setup +1. Copy `vars.py.example` to `vars.py` +2. Setup `vars.py` with your credentials +3. Adapt `message.txt` and import `contacts.csv` + +## Usage +```python mail.py``` \ No newline at end of file diff --git a/mail.py b/mail.py new file mode 100644 index 0000000..cf08831 --- /dev/null +++ b/mail.py @@ -0,0 +1,83 @@ +import smtplib + +from string import Template + +from email.mime.multipart import MIMEMultipart +from email.mime.text import MIMEText + +import csv + +from vars import * + +def get_contacts(filename): + """ + Return the lists containing the infos + read from a file specified by filename. + """ + + snames = [] + fnames =[] + emails = [] + pwd = [] + user = [] + with open(filename, mode='r', encoding='utf-8') as contacts_file: + for a_contact in contacts_file: + snames.append(a_contact.split()[0]) + fnames.append(a_contact.split()[1]) + emails.append(a_contact.split()[2]) + pwd.append(a_contact.split()[3]) + user.append(a_contact.split()[4]) + return snames, fnames, emails, pwd, user + +def read_csv(filename): + with open(filename, newline='') as csvfile: + data = list(csv.reader(csvfile)) + return data + +def read_template(filename): + """ + Returns a Template object comprising the contents of the + file specified by filename. + """ + + with open(filename, 'r', encoding='utf-8') as template_file: + template_file_content = template_file.read() + return Template(template_file_content) + +def main(): + # names, emails, reda, ws = get_contacts('mycontacts.txt') # read contacts + contacts = read_csv('contacts.csv') + message_template = read_template('message.txt') + + # set up the SMTP server + s = smtplib.SMTP(HOST, PORT) + s.starttls() + s.login(MY_ADDRESS, PASSWORD) + + # For each contact, send the email: + for i in range(len(contacts)): + msg = MIMEMultipart() # create a message + + # add in the actual person name to the message template + message = message_template.substitute(SNAME=str(contacts[i][0]), FNAME=contacts[i][1], PWD=contacts[i][3], USER =contacts[i][4]) + + # Prints out the message body for our sake + print(message) + + # setup the parameters of the message + msg['From']=MY_ADDRESS + msg['To']=contacts[i][2] + msg['Subject']=SUBJECT + + # add in the message body + msg.attach(MIMEText(message, 'html')) + + # send the message via the server set up earlier. + s.send_message(msg) + del msg + + # Terminate the SMTP session and close the connection + s.quit() + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/vars.py.example b/vars.py.example new file mode 100644 index 0000000..1904b27 --- /dev/null +++ b/vars.py.example @@ -0,0 +1,5 @@ +MY_ADDRESS = 'example@domain.tld' +PASSWORD = 'p4ssw0rd' +HOST = 'mail.domain.tld' +PORT = 587 +SUBJECT = '' \ No newline at end of file