Google Appengine sending emails: [Error] unauthorized sender - python

I'm trying to use google app engine's mail service on my site. It's showing some error whenever I visit the page that sends the email. The error says that I am using an unauthorized sender for the message. Here's the code that sends the email:
mail.send_mail(sender="myapp#appspot.gserviceaccount.com",
to=input_dict["email"],
subject="Mondays user activation",
body=content)
When I try out the site locally (using dev_appserver.py) it doesn't show the error, but it doesn't send the email (Note: I have to add the --enable_sendmail option when I try it locally). The error only shows up when I publish the site.
Does anybody know what I'm doing wrong? Thanks in advance for your help!

What is myapp#appspot.gserviceaccount.com? You might not be able to send mail from that address.
App Engine applications can send email messages on behalf of the app's
administrators, and on behalf of users with Google Accounts.
The email address of the sender, the From address. The sender address
must be one of the following types:
The address of a registered administrator for the application. You can add administrators to an application using the Administration
Console.
The address of the user for the current request signed in with a Google Account. You can determine the current user's email address
with the Users API. The user's account must be a Gmail account, or be
on a domain managed by Google Apps.
Any valid email receiving address for the app (such as xxx#APP-ID.appspotmail.com).
Any valid email receiving address of a domain account, such as support#example.com. Domain accounts are accounts outside of the
Google domain with email addresses that do not end in #gmail.com or
#APP-ID.appspotmail.com.
https://developers.google.com/appengine/docs/python/mail/sendingmail

First follow these steps
https://cloud.google.com/appengine/docs/python/mail/#who_can_send_mail
Then you need to manually add the sender email in cloud console
How to add an authorized sender

You may also have to add the email address you which to send the email from to the App Engine application settings Email API authorized senders.
See https://cloud.google.com/appengine/docs/python/mail/#Python_Sending_mail

Add the unauthorized email address as an administrator here:
https://console.developers.google.com/project/_/permissions/projectpermissions

Related

Is there a way for an application to send emails using SMTP with an account which is 2FA enabled while also not having access to the app password?

Our Flask application currently supports sending emails via SMTP (using Python's smtplib library), but the sender email available to us has 2FA enabled and app password disabled.
Given that we cannot use app passwords and 2FA is enabled, is it possible to send via SMTP or is there any other way of sending emails from this particular account using any service that works with 2FA?
When we try sending via SMTP using username/password, we get below error:
smtplib.SMTPAuthenticationError: (535, b'5.7.139 Authentication unsuccessful, the request did not meet the criteria to be authenticated successfully. Contact your administrator. [PN3PR01CA0027.INDPRD01.PROD.OUTLOOK.COM]')

Sending email on behalf of Google Apps user

According to the documentation it is possible to send an email message using GAE on behalf of the currently logged in user, if that user has a Gmail or Google Apps account:
For security purposes, the sender address of a message must be the
email address of an administrator for the application or any valid
email receiving address for the app (see Receiving Mail). The sender
can also be the Google Account email address of the current user who
is signed in, if the user's account is a Gmail account or is on a
domain managed by Google Apps.
The following code works for sending emails on behalf of Gmail users but not Google Apps users. Attempting to send mail from a Google Apps user results in an 'Unauthorized sender' error.
current_user = users.get_current_user()
message = mail.EmailMessage()
message.sender = current_user.email()
message.subject = 'subject text'
message.to = 'joe#example.com'
message.body = 'body text'
if message.is_initialized():
try:
message.send()
except Exception, e:
logging.error('Unable to send email update: %s' % e)
else:
logging.error('Email message improperly initialized')
What am I missing? Are there other dependencies that I should be aware of?
EDIT:
Full stacktrace:
Unauthorized sender
Traceback (most recent call last):
File "/base/data/home/apps/s~core-comps/1.358275951854397525/handler_cs_ticket.py", line 274, in sendEmailCopyToClient
message.send()
File "/base/python_runtime/python_lib/versions/1/google/appengine/api/mail.py", line 900, in send
raise ERROR_MAP[e.application_error](e.error_detail)
InvalidSenderError: Unauthorized sender
It looks like the problem is that your application is using Federated Login, which is an experimental feature and doesn't work with sending on behalf of Google Apps accounts. You can change this on the "Application Settings" page in the admin console.
I'll add this to the documentation.

send activation email django-registration (Amazon SES)

How to send activation email with username by using django-registration.I've configured Amazon SES on server.
Here is my settings
AWSAccessKeyId='my_key'
AWSSecretKey='my_key'
EMAIL_BACKEND = 'django_ses.SESBackend`
When User trying to register. They get following error message. Will you please help me ? Thank you
NoAuthHandlerFound at /accounts/register/
No handler was ready to authenticate. 1 handlers were checked. ['HmacAuthV3Handler'] Check your credentials
I'm doing something wrong? please help
Latest UPDATE
'Check your credentials' % (len(names), str(names)))
NoAuthHandlerFound: No handler was ready to authenticate. 1 handlers were checked. ['HmacAuthV3Handler'] Check your credentials
OLD UPDATE
I've made some changes in settings.py
AWS_ACCESS_KEY_ID='my_key'
AWS_SECRET_ACCESS_KEY='my_key'
Now I'm getting following error.
BotoServerError: 400 Bad Request
<ErrorResponse xmlns="http://ses.amazonaws.com/doc/2010-12-01/">
<Error>
<Type>Sender</Type>
<Code>MessageRejected</Code>
<Message>Email address is not verified.</Message>
</Error>
<RequestId>37ceac2e-3861-11e1-acd0-19854590b66c</RequestId>
</ErrorResponse>
You need to verify the email address that you are sending the emails from before amazon will accept the emails.
From the Amazon SES page:
Verify Email Addresses: Before you can send email via Amazon SES, you need to verify that you own the address from which you’ll be sending email. To start the verification process, visit the AWS Management Console.
Before you check whether django-registration can send emails, make sure that sending emails works from the shell
./manage.py shell
from django.core.mail import send_mail
verified_address = "from#example.com"
send_mail('Subject here', 'Here is the message.', verified_address,
['to#example.com'], fail_silently=False)
Any error messages might give you a clue what to try next. If send_mail works, then django registration is probably using the wrong address.

How do I send an email from a non-gmail account using the appengine

I have successfully sent an email using the Google App Engine. However the only email address I can get to work is the gmail address I have listed as the admin of the site. I'm running the app on my own domain (bought and maintained using Google Apps). I would like to send the email from my own domain. Here's the code (or something similar to it):
from google.appengine.api import mail
sender = "myaddress#google.com"
sender_i_want = "myaddress#mygoogleapp.com"
mail.send_mail(sender=sender,
to="Albert Johnson <Albert.Johnson#example.com>",
subject="Your account has been approved",
body=some_string_variable)
And the error I get when I try to send it from my own domain is "InvalidSenderError: Unauthorized sender". I own the domain, I do in fact authorize using my domain to send the mail, I just need to know how to convince the App Engine that this is true.
That's a restriction of App Engine's mail API:
The sender address can be either the email address of a registered administrator for the application, or the email address of the current signed-in user (the user making the request that is sending the message).
If you've got Google Apps running on that domain, you should have (or be able to create) an #thatdomain.com email addresses that you can register as an administrator of the App Engine app in question, which will then let you send email "from" that address.

Sending email from an web2py on GAE

I am trying to send a mail from my web2py app hosted on GoogleAppEngine. But it is not working. I used the mail function that was given with the web2py. Does anybody how to do this?
I read in the GAE Documentation that python mail library would not work with GAE and GAE mail library has to be used. Does it also applies to web2py mail?
Thanks
The web2py gluon.tools.Mail class (that is used by the Auth module too) works on GAE and non-GAE out of the box. You just need to pass the correct settings:
mail=Mail()
mail.settings.server="smtp.example.com:25" or "gae"
mail.settings.sender="you#example.com"
mail.settings.tls=True or False
mail.settings.login="you:password"
It supports multiple encodings, MIME and attachments.
The web2py gluon.tools.Mail class works on GAE.
See code snippet gluon.tools
line 310
try:
if self.settings.server == 'gae':
from google.appengine.api import mail
result = mail.send_mail(sender=self.settings.sender, to=to,
subject=subject, body=text)
This is the correct settings to work on GAE
mail=Mail()
mail.settings.server="gae"
mail.settings.sender="you#example.com" #This must be the email address of a registered
#administrator for the application, or the address
#of the current signed-in user.
mail.settings.login="you:password"
See
http://code.google.com/intl/en/appengine/docs/python/mail/emailmessagefields.html
sender
The email address of the sender, the From address. This must be the email address of a registered administrator for the application, or the address of the current signed-in user. Administrators can be added to an application using the Administration Console. The current user's email address can be determined with the Users API.
Sorry! My english is very poor. I hope to help.
Celso Godinho (celso.gcosta#gmail.com)
Brazil World Cup champion soccer 2010
You should use the native App Engine mailer:
http://code.google.com/appengine/docs/python/mail/sendingmail.html

Categories

Resources