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
[email]
sender = Atomstrom <dummy@dummy.invalid>
receiver = dummy@dummy.invalid
sender_name = Atomstrom
sender_address = dummy@dummy.invalid
receiver_name = Mail Receiver
receiver_address = dummy@dummy.invalid
prefix_single = [as]
prefix_digest = [atomstrom]

View File

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