2017-05-30 19 views
1

私は、毎月のHTML形式のレポートを自分の電子メールに送信する、私のArch Linux VPSで自動システムを作成しようとしています。私はそれのプログラミング言語としてPythonを使用することに決めましたが、私は自分自身が固執しています。PythonでローカルExim MTA経由で電子メールを送信

以下のコードでは、SMTPlibを使用してメッセージを作成して送信しています。

import smtplib 
from email.mime.multipart import MIMEMultipart 
from email.mime.text import MIMEText 


def py_mail(SUBJECT, BODY, TO, FROM): 
    """"With this function we send out our HTML email""" 

    # Create message container - the correct MIME type is multipart/alternative here! 
    MESSAGE = MIMEMultipart('alternative') 
    MESSAGE['subject'] = SUBJECT 
    MESSAGE['To'] = TO 
    MESSAGE['From'] = FROM 
    MESSAGE.preamble = """"Your reader does not support the report format. Please visit 
          us <a href="https://my-website.nl">online</a>!""" 

    # Record the MIME type text/html 
    HTML_BODY = MIMEText(BODY, 'html') 

    # Attach parts into message container 
    MESSAGE.attach(HTML_BODY) 

    # Create SMTP object 
    server = smtplib.SMTP(host='localhost', port=587) 

    # Print debugging output when testing 
    if __name__ == '__main__': 
     server.set_debuglevel(1) 

    # The actual sending of the email 
    try: 
     server.starttls() 
     server.sendmail(FROM, [TO], MESSAGE.as_string()) 
     server.quit() 
    except smtplib.SMTPException as error: 
     print("Error: unable to send mail : {err}".format(err=error)) 


if __name__ == '__main__': 
    """Executes if the script is run as main script (for testing purposes)""" 

    email_content = """" 
    <head> 
     <link href="https://fonts.googleapis.com/css?family=Open+Sans+Condensed:300" rel="stylesheet"> 
     <meta charset="UTF-8"> 
     <title>TestMail</title> 

     <style> 
      h1 { 
       position: absolute; 
       top: 50%; 
       left: 50%; 
       -webkit-transform: translate(-50%, -50%); 
       -moz-transform: translate(-50%, -50%); 
       -ms-transform: translate(-50%, -50%); 
       -o-transform: translate(-50%, -50%); 
       transform: translate(-50%, -50%); 
       font-family: "Open Sans Condensed", sans-serif; 
       font-size: 3em; 

      } 
     </style> 

    </head> 
    <body> 
     <h1>'tis but a scratch</h1> 
    </body> 
    """ 

    TO = '[email protected]' 
    FROM = '[email protected]' 

    py_mail("Test email onderwerp", email_content, TO, FROM) 

しかし、私は、私は次のエラーを取得するスクリプトを実行した後:

ConnectionRefusedError: [Errno 111] Connection refused

私はEximのは、ローカルとして実行しているが、私のサーバー上のMTAのみを送信します。コマンドラインから電子メールを送信するのに問題はありません。 Pythonで接続しようとすると、問題が発生し始めます。

誰かが私を助けることができれば、それはすばらしいでしょう。事前に感謝します

答えて

-1

あなたは本当にlocalhostポート587で実行されているSMTPサーバーを持っていますか?ポート25を試してください。これはSMTP用の標準ポートです。 587はSSL/TLS用ですが、SMTPを設定する必要があります。

本当にポート587である必要がある場合は、ファイアウォールの設定を確認してください。

+0

私はポート25に設定しましたが、残念ながら同じエラーです。また、テストのためにファイアウォールをすばやく無効にしてから再度有効にしました。まだ変更はありません。 –

関連する問題