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