rewrote mail function, to avoid warnings on mailserver

This commit is contained in:
Ronald Schaten 2013-03-22 22:05:15 +01:00
parent 0e9896f264
commit 5a0f7c9350
2 changed files with 30 additions and 19 deletions

View File

@ -6,7 +6,9 @@ hostname = localhost
database = atomstrom database = atomstrom
[email] [email]
sender = Atomstrom <dummy@dummy.invalid> sender_name = Atomstrom
receiver = dummy@dummy.invalid sender_address = dummy@dummy.invalid
receiver_name = Mail Receiver
receiver_address = dummy@dummy.invalid
prefix_single = [as] prefix_single = [as]
prefix_digest = [atomstrom] prefix_digest = [atomstrom]

View File

@ -1,4 +1,5 @@
#!/usr/bin/env python #!/usr/bin/env python
#coding: utf-8
from sqlalchemy import create_engine, Table, Column, Integer, Text, String, Boolean, DateTime, MetaData, ForeignKey, desc from sqlalchemy import create_engine, Table, Column, Integer, Text, String, Boolean, DateTime, MetaData, ForeignKey, desc
from sqlalchemy.orm import sessionmaker, relation, backref from sqlalchemy.orm import sessionmaker, relation, backref
@ -14,9 +15,14 @@ import hn
import html2text import html2text
import ConfigParser import ConfigParser
import pprint import pprint
import smtplib
from email.mime.text import MIMEText
from optparse import OptionParser from optparse import OptionParser
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
Base = declarative_base() Base = declarative_base()
@ -142,15 +148,19 @@ class Entry(Base):
def send_mail(sender, receiver, subject, body): def send_mail(sender, receiver, subject, body):
print 'sending to %s: %s' % (receiver, subject) print 'sending to %s: %s' % (receiver[0], subject)
mail = MIMEText(body, _charset='utf-8') Charset.add_charset('utf-8', Charset.QP, Charset.QP, 'utf-8')
mail['From'] = sender.encode('utf-8') mail = MIMEMultipart('alternative')
mail['To'] = receiver mail['Subject'] = "%s" % Header(subject, 'utf-8')
mail['Subject'] = subject mail['From'] = "\"%s\" <%s>" % (Header(sender[0], 'utf-8'), sender[1])
mailserver = smtplib.SMTP('localhost') mail['To'] = "\"%s\" <%s>" % (Header(receiver[0], 'utf-8'), receiver[1])
mailserver.sendmail(sender, [receiver], mail.as_string()) textpart = MIMEText(body, 'plain', 'utf-8')
mailserver.quit() 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())
def get_entry_text(entry): def get_entry_text(entry):
if entry.readability: if entry.readability:
@ -195,7 +205,6 @@ def mail_daily_digest(session, sender, receiver, prefix):
subject = '%s (%s) - %d entries' % (today.strftime('%y%m%d'), today.strftime('%A'), count) subject = '%s (%s) - %d entries' % (today.strftime('%y%m%d'), today.strftime('%A'), count)
if prefix != '': if prefix != '':
subject = '%s %s' % (prefix, subject) subject = '%s %s' % (prefix, subject)
sender = '%s <%s>' % ('Atomstrom', sender)
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()
@ -212,7 +221,7 @@ def mail_single_entry(feed, feedinfo, entry, sender, receiver, prefix):
body = '%s\n\n' % get_entry_text(entry) body = '%s\n\n' % get_entry_text(entry)
body = body + '%s\n' % feedinfo.link body = body + '%s\n' % feedinfo.link
body = body + '%s\n' % link body = body + '%s\n' % link
sender = '%s <%s>' % (feedinfo.title, sender) sender[0] = feedinfo.title
send_mail(sender, receiver, subject, body) send_mail(sender, receiver, subject, body)
entry.sent = datetime.now() entry.sent = datetime.now()
@ -351,13 +360,13 @@ if __name__ == '__main__':
if options.fetch: if options.fetch:
fetch_all_feeds(session) fetch_all_feeds(session)
if options.single: if options.single:
sender = config.get('email', 'sender') sender = [config.get('email', 'sender_name'), config.get('email', 'sender_address')]
receiver = config.get('email', 'receiver') receiver = [config.get('email', 'receiver_name'), config.get('email', 'receiver_address')]
prefix = config.get('email', 'prefix_single') prefix = config.get('email', 'prefix_single')
mail_single_entries(session, sender, receiver, prefix) mail_single_entries(session, sender, receiver, prefix)
if options.daily: if options.daily:
sender = config.get('email', 'sender') sender = [config.get('email', 'sender_name'), config.get('email', 'sender_address')]
receiver = config.get('email', 'receiver') receiver = [config.get('email', 'receiver_name'), config.get('email', 'receiver_address')]
prefix = config.get('email', 'prefix_digest') prefix = config.get('email', 'prefix_digest')
mail_daily_digest(session, sender, receiver, prefix) mail_daily_digest(session, sender, receiver, prefix)
if not (options.fetch or options.single or options.daily): if not (options.fetch or options.single or options.daily):