Tech エントリー[サーバー / プログラミング]
MacOSX 10.4 + MacPorts の Python 2.5 で smtplib 使ってたら socket.ssl ってエラーが
OP25B の影響下でローカルマシンからメール送信したくて、以前書いたプログラムなんだけど、マシンを再インストールしたり、ゴニョゴニョ色々していたら動かなくなった...
書いたのは、以下のようなプログラム。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import smtplib
from email.MIMEText import MIMEText
from email.Utils import formatdate
args = {}
args["subject"] = sys.argv[1]
args["from_email"] = sys.argv[2]
args["to_email"] = sys.argv[3]
#UTF-8
data = sys.stdin.read()
body = unicode(data, "utf8").encode('iso-2022-jp','replace')
msg = MIMEText(body, 'plain', 'iso-2022-jp')
msg['Subject'] = args["subject"]
msg['From'] = args["from_email"]
msg['To'] = args["to_email"]
msg['Date'] = formatdate()
s = smtplib.SMTP('smtp.gmail.com', 587)
s.ehlo()
s.starttls()
s.ehlo()
s.login('example@gmail.com', 'password')
s.sendmail(args["from_email"], [args["to_email"]], msg.as_string())
s.quit()
上記ソースを send_mail.py で保存したと仮定して、
echo "test mail" | send_mail.py "subject" send_from@example.com send_to@example.com
とかすればいい。
最初は、標準で入っている python 2.3 ? で実行したら
Traceback (most recent call last):
File "/opt/local/script/update-notifier.py", line 16, in ?
body = unicode(data, "utf8").encode('iso-2022-jp','replace')
LookupError: unknown encoding: iso-2022-jp
とかエラーが出て ???
macports でインストールした python2.5 で再実行したら
echo "test mail" | python2.5 send_mail.py "subject" send_from@example.com send_to@example.com
socket.ssl なんて attribute はないと
Traceback (most recent call last):
File "/opt/local/script/update-notifier.py", line 27, in
s.starttls()
File "/opt/local/lib/python2.5/smtplib.py", line 605, in starttls
sslobj = socket.ssl(self.sock, keyfile, certfile)
AttributeError: 'module' object has no attribute 'ssl'
困って検索したら関連ありそうなページがいくつか見つかったんだけど、英語でよくわからない。
- [Trac] Re: ERROR: Failure sending notification on creation of ticket #1:
- Problem with ssl in Python 2.5
二つ目のサイトはエラーってて Google のキャッシュからなんとか情報拾って、
Problem with ssl in Python 2.5
There it was! a py25-socket-ssl port. I proceeded installing it:
» e-shell.org
書かれているがままに、py25-socket-ssl をインストールしたら
sudo /opt/local/bin/port install py25-socket-ssl
動いた。
だけど、プロバイダのメール (e.g. @biglobe.ne.jp とか) にはスパム判定されちゃうのか、うまく送れなかった。
多分原因は、ここ ([J] YahooBB 回線下で OP25B に対応する自宅サーバーの MTA - Jamz (Tech)) とかと似たようなことだろうから無視。
とりあえず、解決。
コメント (0) トラックバック (1) Atom/RSS
投稿: 2008年06月28日 16:31 / 最終更新: 2008年06月28日 16:58
» 国際化対応済み (標準で日本語化されている) Trac 0.12dev をインストール
« suEXEC を /home 配下で有効にする
トラックバック
-
Op25b Bookmarks
Post Saver - Website voting and saving system2009年7月 6日 04:47 - Bookmarked your page with keywords op25b!



コメント (投稿する)