2012-05-07 5 views
0

私は以下の次のコードを使用しています:pythonでメールを送るために特定の情報を追加する必要がありますか?

import smtplib 
import zipfile 
import tempfile 
from email import encoders 
from email.message import Message 
from email.mime.base import MIMEBase 
from email.mime.multipart import MIMEMultipart 

#... 

def send_file_zipped(the_file, recipients, sender='[email protected]'): 
    myzip = open('file.zip', 'rb') 


    # Create the message 
    themsg = MIMEMultipart() 
    themsg['Subject'] = 'File %s' % the_file 
    themsg['To'] = ', '.join(recipients) 
    themsg['From'] = sender 
    themsg.preamble = 'I am not using a MIME-aware mail reader.\n' 
    msg = MIMEBase('application', 'zip') 
    msg.set_payload(myzip.read()) 
    encoders.encode_base64(msg) 
    msg.add_header('Content-Disposition', 'attachment', filename=the_file + '.zip') 
    themsg.attach(msg) 
    themsg = themsg.as_string() 

    # send the message 
    smtp = smtplib.SMTP("smtp.gmail.com", "587") 
    smtp.connect() 
    smtp.sendmail(sender, recipients, themsg) 
    smtp.close() 

#running this 
send_file_zipped('file.zip', '[email protected]') 

を私が試してみて、それがここに正常に接続するためにさまざまなバリエーションを試してみましたが、私はここに途方に暮れています。私は取得していますエラーは、このです:

Traceback (most recent call last): 
File "/Users/Zeroe/Documents/python_hw/cgi-bin/zip_it.py", line 99, in <module> 
send_file_zipped('file.zip', '[email protected]') 
File "/Users/Zeroe/Documents/python_hw/cgi-bin/zip_it.py", line 40, in send_file_zipped 
smtp.connect() 
File "/usr/local/lib/python3.2/smtplib.py", line 319, in connect 
self.sock = self._get_socket(host, port, self.timeout) 
File "/usr/local/lib/python3.2/smtplib.py", line 294, in _get_socket 
return socket.create_connection((host, port), timeout) 
File "/usr/local/lib/python3.2/socket.py", line 404, in create_connection 
raise err 
File "/usr/local/lib/python3.2/socket.py", line 395, in create_connection 
sock.connect(sa) 
socket.error: [Errno 61] Connection refused 

私は私の問題は、SMTPサーバとの接続であるが、私は、私が行方不明ですか分からないと仮定するつもりです。どんな助けでも大歓迎です!!

答えて

1

smtp.connect()は間違っています/冗長です。 smtplib.SMTP(...)は、初期化時に.connectを呼び出します。パラメータなしの.connectコールは、localhostへの接続を意味し、マシンにSMTPサーバが実行されていない場合はエラーが発生します。

あなたの目標は、Gmailを使ってメールを送信することです。 GMailのSMTP requires authenticationは実行していないことに注意してください。

あなたの最後の行は、それに応じて次のようになります。

# send the message 
smtp = smtplib.SMTP("smtp.gmail.com", 587) 
smtp.helo() 
smtp.starttls()     # Encrypted connection 
smtp.ehlo() 
smtp.login(username, password) # Give your credentials 
smtp.sendmail(sender, recipients, themsg) 
smtp.quit() 
+0

うわー、最後に誰かがそれを説明することができます!本当に感謝しております。たとえそれが失敗したとしても、最終的に何かの最初の兆候が起こります。私が得ているエラーは "smtplib.SMTPException:STARTTLS拡張機能はサーバーでサポートされていません" smtp_ssl()を使うべきでしょうか? – Andy

+0

実際に私はそれを理解しました。私のMacはSTARTTLSをサポートしていませんが、TTLSを使用しない場合、pythonのドキュメントでは必要ないと述べたので、starttls関数とehloとheloを削除しました。代わりにSMTP_SSLを使用し、ホスト名を追加して成功しました!ほとんどの人がちょうど私に私にエラーを与えるリンクに私を指示するので、あなたが私に実際の説明を与えるために取り出した時を本当に感謝します。 – Andy

+1

@Andy:これはそれを解決するのを助けてうれしいです:)。 – Avaris

0

これは問題ではないかもしれませんが、ポート番号を文字列として指定しているため、おそらく動作しません。

+0

うーん、私はそれを試してみましょう。 – Andy

+0

Yupは、SMTP()とSMTP_SSL()の両方を、文字列以外のホストで試してみましたが、それと同じ問題です。しかし、私のコンピュータのデフォルトのホスト名を調べるにはどうしたらいいですか?私はそれを見て、人々はちょうどコマンドラインで入力ホスト名と言うが、何も画面にエコー、確信していない – Andy

関連する問題