Sending email from an web2py on GAE - python

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

Related

Sending email using python script from an Azure server

Using a python script, I am trying to send an email.
The mail part of the script looks like -
import smtplib
sender = 'abc.xyz#company.com'
receiver = 'abc.xyz#company.com'
message = """From: From Person <abc.xyz#company.com>
To: To Person <abc.xyz#company.com>
Subject: Inbound Folder Check - Alert
Unchanged even after an hour, please check
"""
smtpObj = smtplib.SMTP('smtp.office365.com',587)
smtpObj.starttls()
smtpObj.login('def#company.onmicrosoft.com','Password')
smtpObj.sendmail(sender, receiver, message)
The error that I am getting is -
raise SMTPDataError(code, resp) smtplib.SMTPDataError: (554, b'5.2.0
STOREDRV.Submission.Exception:SendAsDeniedE
xception.MapiExceptionSendAsDenied; Failed to process message due to a
permanent exception with message Cannot submit message. ...,
20.52176:140FCC8414001010640 10000, 20.50032:140FCC848417101069010000, 0.35180:0A006586, 255.23226:6E010000,
255.27962:0A000000, 255.27962:0E000000, 255.31418:0A00EB87, 16.55847:69000000, 1
7.43559:0000000090010000000000000D00000000000000, 20.52176:140FCC84140010100A00F 736, 20.50032:140FCC84841710100A00F836, 0.35180:8C010000, 255.23226:40000730, 25
5.27962:32000000, 255.17082:DC040000, 0.27745:9A010000, 4.21921:DC040000, 255.27 962:FA000000, 255.1494:0A00070E, 0.37692:01000100, 0.44092:01000100, 0.41232:010 00100, 0.60208:01000100, 0.37136:01000100, 0.34608:01000100, 0.55056:01000100, 0 .42768:01000100, 0.56112:05000780, 0.52807:00000000, 4.33016:DC040000, 7.40748:0 10000000000010C00000000, 7.57132:000000000000000005000780, 1.63016:32000000, 4.3 9640:DC040000, 8.45434:EC5537826FAD8844ABBA433565F1D57205000780, 5.10786:0000000 031352E32302E313232382E3032303A564931505230314D42323939303A38616433323065392D646
338612D343530642D386265322D62646464616261333932613900602005000780,
255.1750:4B01 0000, 255.31418:0A004C82, 0.22753:50010000, 255.21817:DC040000, 4.60547:DC040000 , 0.21966:B4100000, 4.30158:DC040000 [Hostname=abc]')
Only help I got on the internet was from the page - https://support.microsoft.com/en-us/help/3132755/554-5-2-0-storedrv-deliver-exception-objectnotfoundexception-mapiexcep
And it seems to say clutter is enabled but the folder is not present in the mailbox and suggests to turn off the clutter option which didn’t help.
PS - This works from my local machine within my company network but it is something to do with running it from Azure server.
First of all, please follow the offical document How to set up a multifunction device or application to send email using Office 365 to make sure whether set up correctly.
Secondly, if you were using Azure VM to send mail via SMTP, you can refer to this document Understand outbound SMTP problems in Azure VMs to try to fix your issue.
Meanwhile, for coding, there are some resources that may helps.
Python: Send Email via Office 365
Send email on App Service using Office 365 (O365) in PHP
Sending email from an Azure App Service using an O365 SMTP server in C#
Or using some packages directly like python-o365: GitHub and PyPI.
Finally, there is a service named SendGrid which be used for email service. You can use it to send mail in your scenario on Azure, please refer to the offical tutorial to know How to Send Email Using SendGrid with Azure (C#). For Python, you can code via its REST API or sendgrid python library.

administrator has not consented to use the application -- Azure AD

I am trying to obtain a token from Azure AD from Python client application. I want users to seamlessly authenticate with just a username and password (client_id / secret will be embedded in the app). I registered my app and given it all permissions and hit the "grant permissions" button in the new portal according to this post:
The user or administrator has not consented to use the application - Send an interactive authorization request for this user and resource
I am sending an http post to:
https://login.microsoftonline.com/{tenant_id}/oauth2/token
with the following data:
headers = {
"Content-Type": "application/x-www-form-urlencoded"
}
body = "resource={0}&grant_type=password&username={1}&password={2}&client_id={3}&client_secret={4}&scope=openid".format(app_id_uri,user,password,client_id,client_secret)
I cannot seem to get past this error no matter what I try:
b'{"error":"invalid_grant","error_description":"AADSTS65001: The user or administrator has not consented to use the application with ID \'078c1175-e384-4ac7-9116-efbebda7ccc2\'. Send an interactive authorization request for this user and resource.
Again, my goal:
User enters user / pass and nothing else. App sends user / pass / client_id / client_secret, obtains token.
According to your comment:
The message I'm receiving says to do an interactive request but that is exactly what I'm trying to avoid because this is a python app with no web browser and I'm trying to avoid complexity.
I think you want to build a daemon app or an app only application integrating with Azure AD. You can refer to https://graph.microsoft.io/en-us/docs/authorization/app_only for the general introduction.
Furthermore, you can leverage the ADAL for Python to implement this functionality with a ease. Also, you can refer to client_credentials_sample.py for a quick start.
You should try logging in as an admin to be able to give consent to use the application on your tenant at all.

Google App Engine 1.7.6 Remote API error

Prior to the 1.7.6 dev server update, I was able to use /_ah/remote_api to upload test data to my dev server having to go through the authentication process by not entering a username and password (hitting return twice). Since the update, this now continuously asks for a username and password, regardless of what I put in - often says error incorrect username or password. I am currently targeting localhost:8080,
def auth_func():
return (raw_input('Username:'), getpass.getpass('Password:'))
remote_api_stub.ConfigureRemoteApi(None, '/_ah/remote_api', auth_func,
'localhost:8080')
though there are two new servers including the API server and the admin server. Has anyone else encountered this? if so, how did you fix it?
Thanks!
Jon
Apparently thanks to Tim - If you use the new dev_appserver then you need to sepecify a email like looking username and a single character as a password on the local development server in order for it to accept and move past the login stage.

Google Appengine sending emails: [Error] unauthorized sender

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 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.

Categories

Resources