2011-11-30 5 views
7

私はPythonスクリプトで電子メールを送信したいと思いますが、残念ながら、それは単純にmail()関数を使うことができるPHPと同じではありません。MacまたはLinuxでPythonで電子メールを送信する最も良い方法は?

私はこの例で使用しました:

import smtplib 
    FROM = "[email protected]" 
    TO = ["[email protected]"] 

    SUBJECT = "Hello!" 

    TEXT = "This message was sent with Python's smtplib." 
    server = smtplib.SMTP(SERVER) 
    server.sendmail(FROM, TO, message) 
    server.quit() 

をしかし、それだけで私も

私はどのように
Traceback (most recent call last): 
    File "mylo.py", line 70, in <module> 
    sys.exit(main()) 
    File "mylo.py", line 66, in main 
    send_mail() 
    File "mylo.py", line 37, in send_mail 
    server = smtplib.SMTP(SERVER) 
    File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/smtplib.py", line 239, in __init__ 
    (code, msg) = self.connect(host, port) 
    File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/smtplib.py", line 295, in connect 
    self.sock = self._get_socket(host, port, self.timeout) 
    File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/smtplib.py", line 273, in _get_socket 
    return socket.create_connection((port, host), timeout) 
    File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/socket.py", line 512, in create_connection 
    raise error, msg 
socket.error: [Errno 61] Connection refused 

...の意味を知らないエラーの全体の束を返します。 Pythonで電子メールを送信しますか?

答えて

14

私は電子メールで送信するロジックを書き直し:

#!/usr/bin/python -tt 

from email.mime.text import MIMEText 
from datetime import date 
import smtplib 

SMTP_SERVER = "smtp.gmail.com" 
SMTP_PORT = 587 
SMTP_USERNAME = "[email protected]" 
SMTP_PASSWORD = "yourpassword" 

EMAIL_TO = ["[email protected]", "[email protected]"] 
EMAIL_FROM = "[email protected]" 
EMAIL_SUBJECT = "Demo Email : " 

DATE_FORMAT = "%d/%m/%Y" 
EMAIL_SPACE = ", " 

DATA='This is the content of the email.' 

def send_email(): 
    msg = MIMEText(DATA) 
    msg['Subject'] = EMAIL_SUBJECT + " %s" % (date.today().strftime(DATE_FORMAT)) 
    msg['To'] = EMAIL_SPACE.join(EMAIL_TO) 
    msg['From'] = EMAIL_FROM 
    mail = smtplib.SMTP(SMTP_SERVER, SMTP_PORT) 
    mail.starttls() 
    mail.login(SMTP_USERNAME, SMTP_PASSWORD) 
    mail.sendmail(EMAIL_FROM, EMAIL_TO, msg.as_string()) 
    mail.quit() 

if __name__=='__main__': 
    send_email() 

これは非常に設定可能なスクリプトです。

+0

をチェックアウトこれが働いていることがテストされています。 –

+0

ローカルホストから電子メールを送信する場合はどうすればよいですか?ユーザー名とパスワードを設定するにはどうすればよいですか? – NoobDev4iPhone

+0

その場合、サーバが 'localhost'に変更され、ポートが' 25'(デフォルト)に変更され、認証ロジックが削除されたとします。 –

-1

SendGrid APIを使用することをお勧めします。 SendGridのAPIを使用すると、必要なすべての簡単なアカウントで、このコードは、電子メールを送信します:完全なドキュメントについては

import sendgrid 

sg = sendgrid.SendGridClient('Username','Password') 
message = sendgrid.Mail() 

message.add_to("Email Address of Reciever") 
message.set_from("Email Address of Sender") 
message.set_subject("Email Subject") 
message.set_html("Email html") 

sg.send(message) 

https://sendgrid.com/docs/Integrate/Code_Examples/python.html

関連する問題