Email Not Sending in AWS SES - python

Settings.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'email-smtp.us-west-2.amazonaws.com'
EMAIL_HOST_USER = config('SMTP_USER')
EMAIL_HOST_PASSWORD = config('SMTP_PW')
EMAIL_PORT = 587
VERIFIED_IDENTITY = config('VERIFIED_IDENTITY')
I have a verified email in my AWS SES and I am trying to send a message. I get a sent message everytime I use my contact form but no message actually gets sent.
if 'contactForm' in request.POST:
#print('Contact form')
contact_form = ContactForm(request.POST)
if contact_form.is_valid():
contact_form.save()
email_subject = f'New contact {contact_form.cleaned_data["email"]}: {contact_form.cleaned_data["subject"]}'
email_message = contact_form.cleaned_data['message']
print(email_subject,email_message)
try:
send_mail(email_subject, email_message,settings.VERIFIED_IDENTITY,[settings.VERIFIED_IDENTITY])
except BadHeaderError:
print('bad')

In my opinion the issue might be related to DNS SPF record. See below the link related.
"A sender policy framework (SPF) record is a type of DNS TXT record that lists all the servers authorized to send emails from a particular domain."
https://www.cloudflare.com/learning/dns/dns-records/dns-spf-record/#:~:text=A%20sender%20policy%20framework%20(SPF,Domain%20Name%20System%20(DNS).
https://docs.aws.amazon.com/ses/latest/dg/send-email-authentication-spf.html
To set up SPF, you publish a TXT record to the DNS configuration for your domain. This record contains a list of the servers that you authorize to send email from your domain. When an email provider receives a message from your domain, it checks the DNS records for your domain to make sure that the email was sent from an authorized server.
Regards,
Ed.

Related

Whenever I use send_mail in django it sends me emails from myself rather than the address I'm providing

So I've been working on this blog website and I wanted to add a form so that users can contact the blog admin. However when they fill out the form and send email.. The email I receive is from myself and not from the user. Someone Please help me to fix it.
The form is working correctly and message-email does return the email that they enter.
e.g. lets say in my form I add a user email as example1#example.com
but when I recieve an email it's not coming from example1#example.com but from my host email myemail#gmail.com.
views.py:
def contact(request):
if request.method == 'POST':
message_name = request.POST['message-name']
message_email = request.POST['message-email']
message = request.POST['message']
#send mail
send_mail(
'message from ' + message_name + ' their email ' + message_email ,
message,
message_email,
['myemail#gmail.com'],
)
return render(request, 'blog/contact.html', {'message_name':message_name})
settings.py:
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_POST = 587
EMAIL_HOST_USER = 'myemail#gmail.com'
EMAIL_HOST_PASSWORD = '******'
EMAIL_USE_TLS = True
This may have some useful information.
https://www.digitalocean.com/community/tutorials/how-to-use-google-s-smtp-server
Note: Google will automatically rewrite the From line of any email you
send via its SMTP server to the default email address associated with
the account if the one used is not on the Send mail as addresses list
in Gmail or G Suite settings. You can verify the list by going to the
Accounts and Import tab on the settings screen.
You need to be aware of this nuance because it affects the
presentation of your email, from the point of view of the recipient,
and it may also affect the Reply To setting of some programs.

How to use AWS Simple Email Service (SES) in Django?

I'm trying to use this library to integrate my Django project with AWS SES.
settings.py
EMAIL_BACKEND = 'django_ses.SESBackend'
AWS_ACCESS_KEY_ID = 'my_aws_access_key'
AWS_SECRET_ACCESS_KEY = 'my_aws_secret_access_key'
AWS_SES_REGION_NAME = 'us-west-2'
AWS_SES_REGION_ENDPOINT = 'email.us-west-2.amazonaws.com'
It throws the following error
SESAddressNotVerifiedError: 400 Email address is not verified.
<ErrorResponse xmlns="http://ses.amazonaws.com/doc/2010-12-01/">
<Error>
<Type>Sender</Type>
<Code>MessageRejected</Code>
<Message>Email address is not verified. The following identities failed the check in region US-WEST-2: jpark1320#gmail.com, webmaster#localhost</Message>
</Error>
<RequestId>0220c0a0-741b-11e8-a153-475b5dfc6545</RequestId>
</ErrorResponse>
I can't even guess why is wrong on my codes. But, one thing might be a problem is send_mail(). I'm using trying to send an email to a user for sign-up confirmation. I put the codes for sending email below.
SMTP settings
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'my_google_email#gmail.com'
EMAIL_HOST_PASSWORD = 'my_google_email_password'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = 'My Team Name <noreply#gmail.com>'
Update
views.py
def signup(request):
if request.method == 'POST':
form = SignupForm(request.POST)
if form.is_valid():
# Create a user object to set email to be username before passing it to db
user = form.save(commit=False)
user.is_active = False
user.email = form.cleaned_data['username']
user.save()
current_site = get_current_site(request)
mail_subject = "[Modvisor] Please verify your email address."
message = render_to_string('accounts/account_active_email.html', {
'user': user,
'domain': current_site.domain,
'uid': urlsafe_base64_encode(force_bytes(user.pk)),
'token': account_activation_token.make_token(user),
})
to_email = user.email
email = EmailMessage(
mail_subject, message, to=[to_email]
)
email.send()
return redirect('signup_confirm')
else:
form = SignupForm()
return render(request, 'accounts/register.html', {'form': form})
The relevant part of the error is "Email address is not verified". By default SES is in sandbox mode where it won't let you use From or To addresses that you have not previously verified. You need to verify the addresses in the SES console or open a support request to leave sandbox.
Verifying Email Addresses in Amazon SES
To verify an address go to the SES console. On the left side select Email Addresses and then click Verify New Email Address. You will need to have access to the email address so you can click the link that will be sent to it.
Moving Out of the Amazon SES Sandbox
To move out of the sandbox simply open a support request, describe your use case and wait a few days.

send_mail is clearly sending email but no email is showing up in inbox

I have these email settings in my settings.py
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'email#myemail.com'
EMAIL_HOST_PASSWORD = 'password'
EMAIL_PORT = 587
and am using this function to send email to recipients.
def send_email(subject, body, recipients, agent_email, bcc=[], attachments=[]):
recipient_list = []
if isinstance(recipients, (str, unicode,)):
recipient_list.append(recipients)
else:
recipient_list = recipients
recipient_list = recipient_list + bcc
send_mail(subject, body, settings.EMAIL_FROM, recipient_list)
while it looks quite clear when I go to the django-admin site that the email was indeed sent and no error messages show in the log files whatsoever, when I go to check the email that it was sent to, nothing shows in the inbox. I would expect to see the email there, especially given that it shows as sent in django-admin. Have I misunderstood something about how email is sent from the system?
EDIT
I also checked my spam folder and added
EMAIL_FROM = 'email#myemail.com'
because I noticed it wasn't there before. Same results, though. Email appears sent according to django-admin but no email in my inbox.
Try to add the following in settings.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER

How do I configure django settings to send email to Ibm lotus Notes

I have an html page with a form where I collect some data like mail_sender, mesage_to_send, subject etc.
When a user click on a button that information is submitted to a views.py function:
from django.core.mail import send_mail
def register_new_request(request):
email_person = request.POST.get('email_person', False)
send_mail('Test', 'Here is the message.', email_person,['tosomeone#example.com'], fail_silently=False)
return HttpResponseRedirect("/")
How i configure in my settings.py to send email to lotus notes via that function?
After some research i found that are some variables that need to be configured like :
EMAIL_USE_TLS = True
EMAIL_HOST = ''
EMAIL_PORT = ''
EMAIL_HOST_USER = ''
EMAIL_HOST_PASSWORD = ''
Can tell me someone how exactly should look that variables? Thanks !
django.core.email sends SMTP mail. There is absolutely nothing special about sending SMTP email to Lotus Notes users. It looks exactly like sending email to any other email user on the Internet.
If your real question is about sending to a Lotus Notes user from within that user's internal network, presumably by submitting your SMTP message directly to a Lotus Domino server, then you need to talk to the administrator(s) responsible for the servers and find out (a) if SMTP is enabled on at least one internal Domino server, (b) what host and port to use to connect via SMTP to the Domino server, (c) is SSL/TLS supported or required for the SMTP connection to the Domino server (d) whether authentication is required, and if yes what user name and password your code should use.

Django email

I am using the Gmail SMTP server to send out emails from users of my website.
These are the default settings in my settings.py
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'example#example.com'
EMAIL_HOST_PASSWORD = 'pwd'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
SERVER_EMAIL = EMAIL_HOST_USER
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
If I want a user to send an email, I am overriding these settings and sending the email using Django's email sending methods. When an exception occurs in the system, I receive an email from the example#example.com. Sometimes I receive an email from some logged in user. Which could also possibly mean that when a user receives an email sent from my website it has a sent address different from the actual user.
What should be done to avoid this situation?
Django only uses settings.DEFAULT_FROM_EMAIL when any of the mail sending functions pass None or empty string as the sender address. This can be verified in django/core/mail.py.
When there is an unhandled exception Django calls the mail_admins() function in django/core/mail.py which always uses settings.SERVER_EMAIL and is only sent to addresses listed in settings.ADMINS. This can also be verified in django/core/mail.py.
The only other place Django itself sends e-mails is if settings.SEND_BROKEN_LINK_EMAILS is True, then CommonMiddleware will send mail to all addresses listed in settings.MANAGERS and the e-mail sender is settings.SERVER_EMAIL.
Therefore, the only time a regular user will receive e-mail from your site is when you call send_mail(). So, always pass a real address as the from_mail argument and you will avoid users receiving email from settings.SERVER_EMAIL or settings.DEFAULT_FROM_EMAIL.
Side note: django-registration is at least one example of a Django pluggable that will send mail from settings.DEFAULT_FROM_EMAIL so in cases like this you need to make sure it is a proper e-mail address such as support#yoursite.com or webmaster#yoursite.com.

Categories

Resources