Python script to send mail through Amazon SES

Python script to send mail through Amazon SES

Postby legolasthehansy » Tue Jan 14, 2014 2:51 am

I'm setting up Splunk to send e-mails through Amazon SES. But before doing this, I found a Python script (Splunk uses Python to send the mails) that I wanted to test which is not working on my Linux server. The output is shown below. I'm able to send test e-mails on the command line using Sendmail - so there is nothing wrong my Amazon SES credentials. Somehow Python doesn't parse the Auth info correctly?

Output
Code: Select all
[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')


This is the script I'm using to test which I got online.


Code: Select all
#!/usr/bin/python
import smtplib

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

fromaddr = 'user@somehost'
toaddrs  = 'touser@some.com'
msg = """From: USER@DOMAIN.COM

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 = 'AKIJADFAWIUDKHSBLKSKFS'
smtp_password = 'I3JFVKLNDXFVIRFVOINEWROIEIRNVNEROIVNDF'
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()
legolasthehansy
 
Posts: 2
Joined: Tue Jan 14, 2014 2:48 am

Re: Python script to send mail through Amazon SES

Postby stranac » Tue Jan 14, 2014 6:48 am

Code: Select all
smtplib.SMTPAuthenticationError: (535, 'Authentication Credentials Invalid')

Based on this, I'd say your username/password is invalid...
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 909
Joined: Thu Feb 07, 2013 3:42 pm

Re: Python script to send mail through Amazon SES

Postby legolasthehansy » Tue Jan 14, 2014 3:38 pm

Thank you for your response.
I had mentioned on my question that I have sendmail running on this box which uses the same credentials on Amazon SES to deliver e-mails. They work fine.
Somehow Python either doesn't parse the creds correctly or doesn't connect to SES server securely. I want to know if there are any extra settings or packages that need to be insttalled on the Linux box to enable python to send mails through Amazon SES.

Thanks
legolasthehansy
 
Posts: 2
Joined: Tue Jan 14, 2014 2:48 am


Return to Networking

Who is online

Users browsing this forum: No registered users and 1 guest