diff --git a/README.md b/README.md index 7381c18..8bd85c4 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ # schatzi-send-mail-to-members -Skript um die Spendenquittungen automatisiert zu versenden \ No newline at end of file +Skript um die Spendenquittungen automatisiert zu versenden +Missing: Gültige Signatur an die Mail hängen \ No newline at end of file diff --git a/addresslist.txt b/addresslist.txt new file mode 100644 index 0000000..e45c16c --- /dev/null +++ b/addresslist.txt @@ -0,0 +1,2 @@ +Alice;Alice.pdf;Alice@gmail.com +Bob;Bob.pdf;Bob@datenklo.de \ No newline at end of file diff --git a/message.txt b/message.txt new file mode 100644 index 0000000..761dd99 --- /dev/null +++ b/message.txt @@ -0,0 +1,9 @@ +Hallo ${PERSON_NAME}, + +Anbei die Spendenquittung für deine Mitgliedsbeiträge und Spenden an die Binary Kitchen e.V. für das Jahr 2019. Diese kannst du beim Finanzamt einreichen, da wir ein gemeinnütziger Verein sind. +Sollte sich ein Fehler eingeschlichen haben, bitte melde dich bei mir, damit ich dir eine fehlerfreie Quittung zukommen lassen kann. Das kann leicht passieren, da dies das erste mal ist, dass ich diese Quittungen automatisiert erstelle. + +Viele Grüße, +Clemens + +P.S. Leider konnten wir keine Umlaute aus der Mitgliederdatenbank exportieren, das Finanzamt sollte aber kein Problem mit der Schreibweise Stra?e haben. Sorry dafür. \ No newline at end of file diff --git a/pdfs/Alice.pdf b/pdfs/Alice.pdf new file mode 100644 index 0000000..7d966be Binary files /dev/null and b/pdfs/Alice.pdf differ diff --git a/pdfs/Bob.pdf b/pdfs/Bob.pdf new file mode 100644 index 0000000..7d966be Binary files /dev/null and b/pdfs/Bob.pdf differ diff --git a/send_all.py b/send_all.py new file mode 100644 index 0000000..f374bb5 --- /dev/null +++ b/send_all.py @@ -0,0 +1,107 @@ +# -*- coding: utf-8 -*- +""" +Created on Thu Jan 9 00:09:12 2020 + +@author: sky +""" + +import smtplib + +from string import Template + +from email.mime.multipart import MIMEMultipart +from email.mime.text import MIMEText +import email.mime.application + +MY_ADDRESS = 'venti@binary-kitchen.de' +PASSWORD = 'FILL ME' + +pdf_dir = 'pdfs/' +host_ = 'mail.binary-kitchen.de' +port_ = 587 + +dry_run = True # do not send mail, but set everything up + +def get_contacts(filename): + """ + Return two lists names, emails containing names and email addresses + read from a file specified by filename. + """ + + names = [] + pdfs = [] + emails = [] + with open(filename, mode='r', encoding='utf-8') as contacts_file: + for a_contact in contacts_file: + names.append(a_contact.split(";")[0]) + pdfs.append(a_contact.split(";")[1]) + emails.append((a_contact.split(";")[2].strip())) + return names, pdfs, emails + +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, pdfs, emails = get_contacts('addresslist.txt') # read contacts + message_template = read_template('message.txt') + + # set up the SMTP server + s = smtplib.SMTP(host=host_, port=port_) + s.starttls() + s.login(MY_ADDRESS, PASSWORD) + + # For each contact, send the email: + for name, pdf, email_ in zip(names, pdfs, emails): + msg = MIMEMultipart() # create a message + + + # add in the actual person name to the message template + message = message_template.substitute(PERSON_NAME=name.title()) + + # Prints out the message body for our sake + # print(message) + + # setup the parameters of the message + msg['From']=MY_ADDRESS + msg['To']=email_ + msg['Subject']="Mitgliedsbeitrags- und Spendenquittung Binary Kitchen 2019" + + # add in the message body + msg.attach(MIMEText(message, 'plain')) + + # PDF attachment + filename=pdf_dir + pdf + try: + fp=open(filename,'rb') + + att = email.mime.application.MIMEApplication(fp.read(),_subtype="pdf") + fp.close() + att.add_header('Content-Disposition','attachment',filename=filename) + msg.attach(att) + + # send the message via the server set up earlier. + if dry_run == False: + try: + s.send_message(msg) + except: + print("Could not send to",name, pdf, email_) + else: + print("Simulating...sending",name, pdf, email_) + + except FileNotFoundError: + print(filename, 'File does not exist. Mail was not send') + + del msg + + # Terminate the SMTP session and close the connection + s.quit() + +if __name__ == '__main__': + main() \ No newline at end of file