send plain text mails -- not really clean, but useful :-/

This commit is contained in:
Ronald Schaten 2013-04-05 21:16:56 +02:00
parent c84c465a14
commit 4a01c4935b

View File

@ -16,28 +16,19 @@ import urllib2
import html2text import html2text
import ConfigParser import ConfigParser
from argparse import ArgumentParser from argparse import ArgumentParser
from cStringIO import StringIO
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header from email.header import Header
from email import Charset
from email.generator import Generator
import smtplib import smtplib
def send_mail(sender, receiver, subject, body): def send_mail(sender, receiver, subject, body):
print 'sending to %s: %s' % (receiver[0], subject) print 'sending to %s: %s' % (receiver[0], subject)
Charset.add_charset('utf-8', Charset.QP, Charset.QP, 'utf-8') message = 'From: "%s" <%s>\n' % (Header(sender[0], 'utf-8'), sender[1])
mail = MIMEMultipart('alternative') message += 'To: "%s" <%s>\n' % (Header(receiver[0], 'utf-8'), receiver[1])
mail['Subject'] = "%s" % Header(subject, 'utf-8') message += 'Subject: %s\n' % Header(subject, 'utf-8')
mail['From'] = "\"%s\" <%s>" % (Header(sender[0], 'utf-8'), sender[1]) message += 'Content-Type: text/plain; charset="utf-8"\n\n'
mail['To'] = "\"%s\" <%s>" % (Header(receiver[0], 'utf-8'), receiver[1]) message += body.encode('utf-8')
textpart = MIMEText(body, 'plain', 'utf-8') server = smtplib.SMTP('localhost')
mail.attach(textpart) server.sendmail(sender[1], [receiver[1]], message)
str_io = StringIO() server.close()
gen = Generator(str_io, False)
gen.flatten(mail)
s = smtplib.SMTP('localhost')
s.sendmail("", receiver[1], str_io.getvalue())
def truncate_text(content, length=100, suffix='...'): def truncate_text(content, length=100, suffix='...'):
content = " ".join(content.split()) content = " ".join(content.split())
@ -80,8 +71,8 @@ def mail_daily_digest(session, sender, receiver, prefix):
if prefix != '': if prefix != '':
subject = '%s %s' % (prefix, subject) subject = '%s %s' % (prefix, subject)
send_mail(sender, receiver, subject, body) send_mail(sender, receiver, subject, body)
for feed, feedinfo, entry in entries: #for feed, feedinfo, entry in entries:
entry.sent = datetime.now() # entry.sent = datetime.now()
else: else:
print 'no unmailed digest-entries found... not sending mail.' print 'no unmailed digest-entries found... not sending mail.'