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.
Related
I am designing a program which the person (be user, admin, client, etc), register and this I sent an email with the authentication route and thus ends the registration process.
the person enters the page and places his email and password.
the app generates an access token and saves the email as identity, takes the email and sends the token with a link.
when clicking on the link redirects to the page, the app searches for the access token and thus allow the user to finish their registration process (name, surname, address, telephone, etc).
I am using python together with the flask framework. so far I have to do the access token and the authentication required to access the different routes. my problem is to send the link with the token to the person's email.
for sending mail I am using the SMTPLIB and this is the code
user = 'xxxxxxxxxx#gmail.com'
password = 'xxxxxxxxx'
remitente = 'xxxxxxxxxx#gmail.com'
destinatario = destinatario
asunto = 'Registro Exitoso'
mensaje = 'ok: nuevoenlace'
gmail = smtplib.SMTP('smtp.gmail.com', 587)
gmail.starttls()
gmail.login(user, password)
header = MIMEMultipart()
header['From'] = remitente
header['To'] = destinatario
header['Subject'] = asunto
mensaje = MIMEText(mensaje, 'html')
header.attach(mensaje)
gmail.sendmail(remitente, destinatario, header.as_string())
gmail.quit()
ok, my problem is to send the token by mail along with authentication link, this redirects me to a protected endpoint and finishes the entry process
If sending the email is the problem, here it says here you need to call
gmail.ehlo()
before calling gmail.starttls().
Find more information here: https://docs.python.org/3/library/smtplib.html
If this is not the problem, please provide more information or Stacktrace etc.
After registration on our service user is sent by email with confirmation link.
But when it is sent to Gmail or other mail services it usually drops to spam.
Here is the code:
def email_user(self, subject, message, from_email=None):
send_mail(subject, message, from_email, [self.email])
def activate_email(self, email=None):
if email: self.email = email
self.is_activated = False
self.activation_code = hashlib.sha256(str(self.email) + os.urandom(256)).hexdigest()[:32]
self.save()
subject = u'Welcome to the {0}!'.format(settings.SITE_NAME)
message = render_to_string('users/emails/activation.html', {'activation_code': self.activation_code, 'site_name': settings.SITE_NAME, 'site_domain': settings.SITE_DOMAIN})
self.email_user(subject, message, settings.SITE_EMAIL)
How to add DKIM or other license to this email in order to make Google trust to our server?
We're using Zimbra mail server on our site domain.
P.S. I found this snippet: https://djangosnippets.org/snippets/1995/
Is it suitable somehow in my case or not?
Thank you!
How your mail is treated depends first and foremost on the configuration of the email server which sends the messages generated by your application, and the DNS records associated with it.
Google's guidelines for bulk senders are a great place to start. Check that your mail server (and the emails themselves) comply with the rules.
DKIM is one of these guidelines, so yes: adding DKIM signatures will help. A few other points in the guide:
"Use the same address in the 'From:' header on every bulk mail you send." If you used different From headers while testing or something, this could be the problem.
Publish an SPF record.
Publish a DMARC policy.
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
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.
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.