Python Send email from a delegated account - python

I am using python to send a scorecard to users on a monthly basis. I would like to use the delegated Gmail account to send email to users. Attached images for your reference.
I can send email to users using my personal account. But I can't send email to users using the Delegated account.
Attached error message screenshot here. I google error message and I couldn't find any posts related to the error message.
Gmail Delegated Account Image
Python Script Image
Python Error Message Image

You probably need to let less secure apps access your account.
See the related Google answer: https://support.google.com/accounts/answer/6010255?hl=en
Btw, that's written in the "support.google.com" link.

Related

Obtaining `gmail.compose` privileges for single G Suite email account to send emails via GMail API

I am trying to compose drafts programmatically via the GMail API, using this tutorial and the following line of code:
service.users().drafts().create(userId=user_id, body=message).execute()
but I receive the following error:
An error occurred: <HttpError 403 when requesting https://www.googleapis.com/gmail/v1/users/me/drafts?alt=json returned "Insufficient Permission">
Note that directly sending messages with gmail.send privileges does not work for me - I want to thread messages.
In the API console, I tried enabling the following scope ../auth/gmail.compose, but there is a warning message that Because you've added a sensitive scope, your consent screen requires verification by Google before it's published.
I have submitted my app for verification, but I just received an email stating that "We've extended the deadline to fully complete your verification from May 15th to June 26th, 2019".
Actually, no users will use my G Suite email accounts (so my app cannot be used to spy on their messages) and I only need access to a single email account info#example.com to send notifications to users who use my site on example.com.
Is there any way to get server-side access to my info#example.com account using my password and programmatically draft messages without having to go through the lengthy and unnecessary app verification process?
It turns out that you can have an "internal", rather than a client facing "public" app, for which all GMail scopes are automatically available.
Here is the response from the Google team that describes the steps (it worked for me):
It appears your app is only used by the people in your domain.
If this is correct, please reply to this email to confirm that is the
case. We will then reject your request so that you are able to update
your project from public to internal.
In addition, you will need to associate your project with your
organization by following the steps below:
If you have not already done so, create an Organization by following
the Quickstart Using Organizations instructions.
Migrate the project into the organization you created as shown in
Migrating Existing Projects into the Organization. Once you have
associated your project with your organization, you and the project
users in your organization can use the app to directly access OAuth
scopes. No verification will be required.

How to use Gmail API to create a client_secret.json that only allows sending emails?

Is it possible to use the gmail API to create a client_secret.json file that will only enable sending of emails from an address? I don't see anything in the gmail API documentation on this subject. Correct me if I am wrong, but with the secret json file generated in this tutorial, the user would be able to read messages in the inbox also, correct?
That is, I want to have an OAuth app that allows users with the client_secret.json to send emails, but not to read any of the emails in the inbox, outbox, etc.

Outlook RestAPI is not working

Outlook restapi is not working
I am folllowing the doc https://dev.outlook.com/RestGettingStarted/Tutorial/python
I got a developer preview account (devxxxx#outlook.com) from outlook and I created an app in https://apps.dev.microsoft.com/
So my idea is to view contacts of people from their outlook account by connecting to this app through oauth2.
When I connect with the same account (devxxxx#outlook.com), I am getting correct credentials(like access token, token_id, etc.) and I can communicate with rest API through these credentiasls and is working fine, I can able to viw the contacts.
But when I try to connect through different account (ex: geo.jacobxxx#outlook.com) as usual I am getting correct credentials(like access token, token_id, etc.), But I am not able to play with rest API, I am getting error response as
404: {"error":{"code":"MailboxNotEnabledForRESTAPI","message":"REST API is not yet supported for this mailbox."}}
Please help.
You can only connect to the specific account that you got from the Outlook Dev team via the Outlook REST APIs. You won't be able to connect to accounts that have not yet been enabled for the Outlook.com preview.
We are working as fast as we can to get everyone enabled, but for the time being you'd need to test and develop only against that account we gave you, and other accounts that have been moved over.

How to send an email in GAE on behalf of a yahoo, hotmail, or .edu user?

On GAE, a non-gmail user can create a Google Account using their non-gmail email and log into a google app engine application. However, sending from that email does not appear to work all the time.
For example, suppose foobar#yahoo.com creates a google account and they log in. Then, GAE should be able to send email from foobar#yahoo.com during a user request.
The problem is that this does not appear to work for yahoo email accounts and others. In my experience, only the following users can have email sent on their behalf:
Currently logged in gmail users
Currently logged in users with emails that run on google apps
Administrators
However, you cannot send email on behalf of users with the following email address:
yahoo and hotmail users
.gov or .mil users
most .edu users, although I think some schools use google apps and they work.
If I send from those email addresses, I get the following error:
message.send();
File "/base/python_runtime/python_lib/versions/1/google/appengine/api/mail.py", line 799, in send
raise ERROR_MAP[e.application_error](e.error_detail)
InvalidSenderError: Unauthorized sender
Am I missing something here?
I know there are similar questions out there on this topic but I don't think they nail them.
Your application has no right to originate email from addresses you don't own.
Doing so is called spoofing and there has been a lot of work done in the last 8 years to prevent spoofing: DKIM, SPF...
If you need to send email you need to have your own email address(es)/domain to send the email from for this application and you'll need to handle bounces as well.
Update:
Google Accounts
Google accounts can be created using 3rd party domain email addresses (e.g. hotmail, yahoo, *.edu, etc). There will be an email confirmation step to verify the email address, but there may be no greater relationship than this. The email address is the "username" and there's a password created with Google that has nothing to do with the 3rd party domain.
Google accounts don't have to have a mail service component. You can create a Google account and not have Gmail.
Google email on a 3rd party domain
Google Apps can be run "on" 3rd party domains, this can, but not necessarily include Google email.
Google email could be run on any domain without any other "Apps", they offer email outsourcing which is quite attractive to the Education sector. In this scenario, Google is authoritative for email for that domain.
On behalf of mailing
Google email has the functionality to set up "On Behalf of" emailing. This requires configuration. A confirmation email is sent to the target account. Once created the email is sent using the originating account's email address in the mail envelope, so any delivery status messages (delays, rejections) will be returned to this account. The originating email address will also be in the sender header. The address that you're sending on behalf of will appear in the from header in the messages, but otherwise, with regards to security settings and validation, it's a Google email from Google.
A quick search has returned some tech blog websites that suggest that they are considering retiring this feature in favour of supporting third party SMTP services.
Third party SMTP server
So someone could configure their Google Email account with the SMTP server, username and password of their mail service provider and use it to send email via their Google mail interface through the valid servers of their mail service provider. This email would therefore genuinely "originate" from that domain's infrastructure.
I've not read the documentation for this GAE function to send email. However, I can see that if someone is signed into their Google account and that account has a mail service, then it could be possible to send email from that account using an API.
However, Google will not generate email "From" 3rd party domains for which it has not been assigned authority over. The email would not be valid; it would be "spoofed". It may not comply with a variety of security enhancements, could be used maliciously and would bring them into disrepute.
I hope this makes the situation clearer for you.
If the documentation is lacking in this regard, it could be that those who wrote it, being so familiar with email, might find it hard to imagine someone would think it were possible.
For me it looks like Google has changed something. I've had an application running 1,5 years without problems (and changes) but suddenly on 2011-05-03 sending emails for example on behalf of Yahoo users stopped working.
This change also affected other non-Google (non-developer users).
If foobar#yahoo.com creates a Google account and logs in, you will be able to send on behalf of their Gmail user, but not their Yahoo user. Same for Hotmail. As for .gov, .mil or .edu, there's nothing special about these TLDs. If they are Google Apps domains, you can send from them, otherwise you cannot.
Note, though, that you can specify a Reply-To address when sending mail. This can be any email address whatsoever, so if you use a reply-to address of foobar#yahoo.com on outbound mail and the recipient clicks reply, this should be the address that's populated as the recipient.

Connecting to Google app email servers in Python to send from an alias

I'm looking to send many emails via Python and would like to connect to Google's email servers to send it from my company email address (it's for work). I've got working code for sending the email through the old company email (user#work.com) which is our login, but can't figure out how to send it through the alias (user#betterdomain.com) we use frequently.
Long story short, logging in with my regular Google Apps account won't do and I need to sign in with an alias (nickname) instead. Alternatively, I can sign with the regular account (user#work.com) but send via a different email (user#betterdomain.com).
Working in python and pretty new to programming, but am a good listener! (It's like this question: Google Apps - Send email from a nickname but in Python).
If I understand properly, what you are trying to do is get the Google App Engine send an email on behalf of logged in user, who is using his Google Apps Account name to login. Is it right?
In that case, the mail API for App Engine informs about the mail.send_mail() wherein From: address can be the current user if signed in with Google Accounts. If this is not what you are looking for, some clarification might be required.

Categories