commentsプラグインでメール送信 導入
posted by jun-g at Sat, 14 Oct 2006 06:48 JST
メール送信部分の修正、スタイルシートの調整が完了したので、commentsプラグインを導入してみた。
comments.pyの修正箇所はこんな感じ。
--- comments.py.orig Thu Oct 12 23:07:08 2006 +++ comments.py Sat Oct 14 06:39:54 2006 @@ -450,26 +450,21 @@ from rfc822 import formatdate from socket import gethostbyaddr + from email.Header import Header + from email.MIMENonMultipart import MIMENonMultipart + author = escape_SMTP_commands(clean_author(comment['author'])) description = escape_SMTP_commands(comment['description']) ipaddress = escape_SMTP_commands(comment.get('ipaddress', '?')) - if comment.has_key('email'): - email = escape_SMTP_commands(clean_author(comment['email'])) - else: - email = config['comment_smtp_from'] - try: server = smtplib.SMTP(config['comment_smtp_server']) curl = config['base_url']+'/'+entry['file_path'] - comment_dir = os.path.join(config['comment_dir'], entry['absolute_path']) - + from_addr = config['comment_smtp_from'] + to_addr = config['comment_smtp_to'] + subject = "comment by %s" % author + enc = config["blog_encoding"] message = [] - message.append("From: %s" % email) - message.append("To: %s" % config["comment_smtp_to"]) - message.append("Date: %s" % formatdate(float(comment['pubDate']))) - message.append("Subject: comment by %s" % author) - message.append("") message.append("Name: %s" % author) if comment.has_key('email'): message.append("Email: %s" % comment['email']) @@ -479,12 +474,19 @@ message.append("Hostname: %s (%s)" % (gethostbyaddr(ipaddress)[0], ipaddress)) except: message.append("IP: %s" % ipaddress) - message.append("Entry URL: %s" % curl) + message.append("Entry URL: %s.html" % curl) message.append("Comment location: %s" % comment_filename) message.append("\n\n%s" % description) - server.sendmail(from_addr=email, - to_addrs=config['comment_smtp_to'], - msg="\n".join(message)) + msg = "\n".join(message) + part = MIMENonMultipart("text", "plain") + subj = Header(subject.encode(enc), enc) + part["Subject"] = subj + part["From"] = from_addr + part["To"] = to_addr + part.set_payload(msg.encode(enc), enc) + server.sendmail(from_addr=from_addr, + to_addrs=to_addr, + msg=part.as_string()) server.quit() except Exception, e: logger = tools.getLogger()
ついでにエントリの固定リンクに拡張子を付けるようにしたり、Recent Entriesを最大10件までの表示に変更したりした。