Created
March 12, 2017 14:23
-
-
Save n1chre/4e9a1085ddc1961f47e7b680c86ad485 to your computer and use it in GitHub Desktop.
Scripts for sending emails to FER students
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
[user] | |
username: fhxxxxx@fer.hr | |
password: ********** | |
[smtp] | |
server: smtp.office365.com | |
port: 587 | |
tls: true |
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
import smtplib, os | |
from email.MIMEMultipart import MIMEMultipart | |
from email.MIMEBase import MIMEBase | |
from email.MIMEText import MIMEText | |
from email.MIMEImage import MIMEImage | |
from email.Utils import COMMASPACE, formatdate | |
from email import Encoders | |
import ConfigParser | |
def sendmail(config_file, send_from, send_to, subject, body, | |
files=None, data_attachments=None, html=False, images=None): | |
if files is None: | |
files = [] | |
if images is None: | |
images = [] | |
if data_attachments is None: | |
data_attachments = [] | |
config = ConfigParser.ConfigParser() | |
config.read(config_file) | |
if config is not None: | |
server = config.get('smtp', 'server') | |
port = config.get('smtp', 'port') | |
tls = config.get('smtp', 'tls').lower() in ('true', 'yes', 'y') | |
username = config.get('user', 'username') | |
password = config.get('user', 'password') | |
msg = MIMEMultipart('related') | |
msg['From'] = send_from | |
msg['To'] = send_to if isinstance(send_to, basestring) else COMMASPACE.join(send_to) | |
msg['Date'] = formatdate(localtime=True) | |
msg['Subject'] = subject | |
msg.attach( MIMEText(body, 'html' if html else 'plain') ) | |
for f in files: | |
part = MIMEBase('application', "octet-stream") | |
part.set_payload( open(f,"rb").read() ) | |
Encoders.encode_base64(part) | |
part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f)) | |
msg.attach(part) | |
for f in data_attachments: | |
part = MIMEBase('application', "octet-stream") | |
part.set_payload( f['data'] ) | |
Encoders.encode_base64(part) | |
part.add_header('Content-Disposition', 'attachment; filename="%s"' % f['filename']) | |
msg.attach(part) | |
for (n, i) in enumerate(images): | |
fp = open(i, 'rb') | |
msgImage = MIMEImage(fp.read()) | |
fp.close() | |
msgImage.add_header('Content-ID', '<image{0}>'.format(str(n+1))) | |
msg.attach(msgImage) | |
smtp = smtplib.SMTP(server, int(port)) | |
if tls: | |
smtp.starttls() | |
if username is not None: | |
smtp.login(username, password) | |
smtp.sendmail(send_from, send_to, msg.as_string()) | |
smtp.close() |
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
#! /usr/bin/env python | |
# -*- coding: UTF-8 -*- | |
from sendmail import sendmail | |
import os | |
import re | |
import json | |
conf_file = 'fer-mail.conf' | |
mail_from = 'filip.hrenic@fer.hr' | |
subject = '[UUUZ] Seminar' | |
clanci_dir = '/Users/fhrenic/Dropbox/Public/UUUZ/clanci/' | |
clanak_regex = '(\d+).*' | |
def create_body(title, date): | |
return """Poštovani, | |
u privitku vam šaljem članak za seminar. | |
Naslov: {title} | |
Datum: {date} | |
P.S. mailovi su poslani sa skriptom pa ako je nečiji mail pogrešan, molim vas da toj osobi proslijedite članak ili javite meni. | |
Lijep pozdrav, | |
Filip Hrenić | |
""".format(title=title, date=date) | |
def get_clanci(): | |
clanci = dict() | |
for clanak in os.listdir(clanci_dir): | |
match = re.search(clanak_regex, clanak, re.IGNORECASE) | |
num = int(match.group(1)) | |
name = match.group(0) | |
clanci[num] = name | |
return clanci | |
def create_pdf(filename, name): | |
return { | |
'data' : open(clanci_dir + filename,'rb').read(), | |
'filename' : name | |
} | |
with open('popis.json') as popis: | |
clanci = get_clanci() | |
for mail_data in json.load(popis): | |
number = int(mail_data['number']) | |
mails_to = mail_data['mails'] | |
date = mail_data['date'] | |
title = mail_data['title'] | |
body = create_body(title, date) | |
clanak = clanci[number] | |
pdf = create_pdf(clanak, title) | |
print 'Sending article {0}...'.format(number), | |
sendmail(conf_file, mail_from, mails_to, subject, body, data_attachments=[pdf]) | |
print ' done!' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment