透過 Amazon SES 發送郵件的 Python 腳本

透過 Amazon SES 發送郵件的 Python 腳本

我正在設定 Splunk 以透過 Amazon SES 發送電子郵件。但在這樣做之前,我發現了一個我想測試的 Python 腳本(Splunk 使用 Python 發送郵件),但它在我的 Linux 伺服器上不起作用。輸出如下圖所示。我能夠使用 Sendmail 在命令列上發送測試電子郵件 - 因此我的 Amazon SES 憑證沒有任何問題。不知何故,Python 無法正確解析身份驗證資訊?

輸出

[root@HOSTNAME ~]# python ses.py
Message length is 47
send: 'ehlo HOSTNAME\r\n'
reply: '250-email-smtp.amazonaws.com\r\n'
reply: '250-8BITMIME\r\n'
reply: '250-SIZE 10485760\r\n'
reply: '250-STARTTLS\r\n'
reply: '250-AUTH PLAIN LOGIN\r\n'
reply: '250 Ok\r\n'
reply: retcode (250); Msg: email-smtp.amazonaws.com
8BITMIME
SIZE 10485760
STARTTLS
AUTH PLAIN LOGIN
Ok
send: 'STARTTLS\r\n'
reply: '220 Ready to start TLS\r\n'
reply: retcode (220); Msg: Ready to start TLS
send: 'ehlo HOSTNAME\r\n'
reply: '250-email-smtp.amazonaws.com\r\n'
reply: '250-8BITMIME\r\n'
reply: '250-SIZE 10485760\r\n'
reply: '250-STARTTLS\r\n'
reply: '250-AUTH PLAIN LOGIN\r\n'
reply: '250 Ok\r\n'
reply: retcode (250); Msg: email-smtp.amazonaws.com
8BITMIME
SIZE 10485760
STARTTLS
AUTH PLAIN LOGIN  
Ok
send: 'AUTH PLAIN            ASOIJFAIUSHDFIGASDALIUSFDILUAI2FIUWHIVHSLIHDVUISHDLVIUSLIDUVKSUHDLKVSUHD=\r\n'
reply: '535 Authentication Credentials Invalid\r\n'
reply: retcode (535); Msg: Authentication Credentials Invalid
Traceback (most recent call last):
File "ses.py", line 31, in <module>
server.login(smtp_username, smtp_password)
File "/usr/lib64/python2.6/smtplib.py", line 589, in login
raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (535, 'Authentication Credentials Invalid')

這是我用來測試的腳本,我從網路上得到的。

#!/usr/bin/python
import smtplib

def prompt(prompt):
return raw_input(prompt).strip()

fromaddr = 'user@somehost'
toaddrs  = '[email protected]'
msg = """From: [email protected]

Hello, this is dog.
"""

print "Message length is " + repr(len(msg))

#Change according to your settings
smtp_server = 'email-smtp.us-east-1.amazonaws.com'
smtp_username = '[redacted]'
smtp_password = '[redacted]'
smtp_port = '587'
smtp_do_tls = True

server = smtplib.SMTP(
host = smtp_server,
port = smtp_port,
timeout = 10 
)
server.set_debuglevel(10)
server.starttls()
server.ehlo()
server.login(smtp_username, smtp_password)
server.sendmail(fromaddr, toaddrs, msg)
print server.quit()

答案1

也許不是您正在尋找的答案,但我正在使用Python博托處理AWS相關內容的函式庫:

import boto
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

def send_ses(fromaddr,
             subject,
             body,
             recipient,
             attachment=None,
             filename=''):
    """Send an email via the Amazon SES service.

    Example:
      send_ses('[email protected], 'greetings', "Hi!", '[email protected])

    Return:
      If 'ErrorResponse' appears in the return message from SES,
      return the message, otherwise return an empty '' string.
    """
    msg = MIMEMultipart()
    msg['Subject'] = subject
    msg['From'] = fromaddr
    msg['To'] = recipient
    msg.attach(MIMEText(body))
    if attachment:
        part = MIMEApplication(attachment)
        part.add_header('Content-Disposition', 'attachment', filename=filename)
        msg.attach(part)
    conn = boto.connect_ses()
    result = conn.send_raw_email(msg.as_string())
    return result if 'ErrorResponse' in result else ''

相關內容