Ashish  Kumar Saxena
Ashish Kumar Saxena

Reputation: 4710

BotoServerError: 400 Bad Request: Mail is not sending

from datetime import datetime as dt, timedelta, date
from django.conf import settings
from django.core.mail import send_mail
from django.core.mail import EmailMultiAlternatives
from django.core.management.base import BaseCommand, CommandError
from hive.apps.accounts.sms_utils import send_sms_twilio



to_email = ['[email protected]']
subject = 'Your subject here'
email_body = 'The following clients are currently eligible for Test.Please contact them .<br>'


msg = EmailMultiAlternatives(subject, '', settings.DEFAULT_FROM_EMAIL, to_email)
msg.attach_alternative(email_body, "text/html")
msg.send()

When i run this code on centos with AWS instance.i am getting following error

 BotoServerError: 400 Bad Request
  <ErrorResponse xmlns="http://ses.amazonaws.com/doc/2010-12-01/">
  <Error>
  <Type>Sender</Type>
  <Code>RequestExpired</Code>
  <Message>Request timestamp: Wed, 01 Oct 2014 07:35:22 GMT expired.  It must be within 300 secs/ of server time.</Message>
  </Error>
  <RequestId>c79a9723-493c-11e4-b2d8-51a7cb197196</RequestId>
  </ErrorResponse>

while its working on another AWS instance.

I have Tested following possibilities.

  1. Server date and time.
  2. simple mail is also not working: send_mail('your subject',str(e), settings.DEFAULT_FROM_EMAIL, fail_email , fail_silently=False)

Upvotes: 2

Views: 1508

Answers (2)

Alexander Tyapkov
Alexander Tyapkov

Reputation: 5047

In my case my Ubuntu server was out of synchronization with SES backend and returned me the same message. To solve it:

  1. Check the time on SES and your local time: Checking SES time with wget -S "https://email.eu-west-1.amazonaws.com" Change the link depending on you email endpoint.
  2. Check time on your server with date

  3. If they are different install ntp on server with sudo apt-get install ntp

  4. And finally syncronize your server time:

sudo service ntp stop sudo ntpdate -s time.nist.gov sudo service ntp start

Upvotes: 0

TrackZero
TrackZero

Reputation: 136

That error indicates your server time isn't synced with Amazon's server's time...

you can check the time of Amazon's server by running:

wget -S "https://email.us-east-1.amazonaws.com"

look for the timestamp in the returned message. replace us-east-1 with your appropriate regional endpoint.

Compare that to your system's time...

if you need an NTP server, you can use:

0.amazon.pool.ntp.org
1.amazon.pool.ntp.org
2.amazon.pool.ntp.org
3.amazon.pool.ntp.org

if this is in one of your own Amazon VPCs, you can set a DHCP option set on the VPC with those ntp servers specified. If your server still isn't syncing, make sure you don't have a security group blocking ntp (UDP 123).

Upvotes: 1

Related Questions