Sending mail python webapp2 GAE - python

I initially tried using the smptlib library but in order to send an e-mail I need to use SSL which costs me with Google App Engine. Other than that, smtplib was a perfect solution.
I found a link here which explains how to send an email using the GAE API.
However, I can't seem to figure out how one would log in to a gmail account say, in order to send the email.
The purpose of this is to send a verification email to addresses of those who are registering. But in the link it shows how to get the current_user and send the email using their credentials. How would I explicitly enter an email, password, and smtp server like I would do with smtplib in order to send the email from my desired address.
Thanks for any answers!

You can't send from an arbitrary email address. As the overview docs state, you can only send from:
The Gmail or Google Apps Account of the user who is currently signed in
Any email address of the form anything#appname.appspotmail.com or anything#appalias.appspotmail.com
Any email address listed in Email API Authorized Senders found in the App Engine Settings page of the Developers Console
If you are the owner of the email account you want to send from, and it is a GMail account, you can add it to the Email API Authorized Senders list from the App Engine console.
Once that's done, you can just use it as the sender address - you don't need to log into anything.

Related

Firestore authentication phone and email OTP using python

I am trying to add a code in python to send OTP to email and phone for verification but I am not able to find the code. I have found the code for JavaScript using "reCAPTCHA verifier" but not for python. Is their any code to send OTP like available in JavaScript ?
Firebase Authentication can only send a SMS/text OTP from its client-side SDKs, to the currently signed in user. There is no way to send an OTP from server-side code, such as from Firebase's Python Admin SDK.
Also see:
sending OTP to mobile number with firebase [python]

How can I send email through python script when my company email is hosted on Google

Just like many big companies using Office365, my company is using google (gsuite) to host their email domain. I need to send automated emails to multiple people within organisation using a python script. How can that be done?
You can use a 3rd party service like Mailgun, it provides a REST API which if you hit you can trigger emails that it will send from a custom domain you configure on the service.
Its super easy to use for python, I use it for Raspberry Pi projects.
def send_simple_message():
return requests.post(
"https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/messages",
auth=("api", "YOUR_API_KEY"),
data={"from": "Excited User <mailgun#YOUR_DOMAIN_NAME>",
"to": ["bar#example.com", "YOU#YOUR_DOMAIN_NAME"],
"subject": "Hello",
"text": "Testing some Mailgun awesomness!"})
It is a nice alternative to using a corporate SMTP server.
Got it fixed.
In order to send an email from Python, we first need to switch ON "Less secure app access" https://myaccount.google.com/lesssecureapps?utm_source=google-account&utm_medium=web.
This we need to do if we don't have 2 Factor Authentication.
If you use 2 Factor Authentication, then you need to create an App Password and use that particular password while sending an email and not your regular password.
To create an App Password use this link: https://support.google.com/mail/answer/185833?hl=en
Now using sample script like below, we can send an email.
import smtplib
# creates SMTP session
s = smtplib.SMTP('smtp.gmail.com', 587)
# start TLS for security
s.starttls()
# Authentication
s.login("username#domain.com", "app_password")
# message to be sent
message = "Message_you_need_to_send"
# sending the mail
s.sendmail("username#domain.com", "recipient#domain.com", message)
# terminating the session
s.quit()
Google provides Gmail api suite for python and it is the preferred way to access versus smtp login/password
You should refer to their developer console for examples and tutorials

Is there a way to check if a user and a pass are valid in gmail in python 3?

I need to check whether a username and password of a gmail account are valid. How can I do that using python?
Google doesn't allow access to Gmail accounts from third-party apps without the consent of the account owner.
This is of course a good idea to keep Google accounts more secure and protect sensitive information.
See: Google Third-party sites & apps with access to your account
Now, there are a couple of options I can think of:
1. If you are trying to access accounts that you own or that have approved access to your app you could use smptlib to start a SMTP(Simple Mail Transfer Protocol) client session.
After you established a connection you can attempt to login to your account and print messages appropriately.
For Example:
import smtplib
username = "gmailaddress"
password = "yourpassword"
server = smtplib.SMTP('smtp.gmail.com', 587) #smtp settings, change accordingly.
server.ehlo()
server.starttls() # secure connection
try:
server.login(username, password)
print("valid account")
except:
print("invalid account")
2. Google's API - this option, while it may be more suitable will probably also require authorization and further research. Google has an API for many of it's services and even provides examples. See: Google API Python Quickstart
An API provides a way for you to interact with with a companies online services securely and the way they were intended.
The limited access to Google accounts may seem exaggerated but that's how your account stays secure :) Good luck!

Verify pending forwarding address with confirmation code in Gmail API

I'm creating a forwarding address for my Gmail account with Gmail API. I know that if function
service.users().settings().forwardingAddresses().create(userId='me', body=address).execute()
returns a result with a verificationStatus of pending, the recipient has to verify the email with a link or a verification code.
I can enter the verification code here in Gmail Settings>Forwarding , but is there a way where I can enter the verification code with the Gmail API?
According to the Gmail API documentation, if Gmail requires verification for a forwarding address, a verification message is sent to the target email address and returned status is 'pending'. The owner of the target email address must complete the verification process before it can be used.
Unfortunately, the Gmail API does not have a verify method for forwarding addresses. The list of methods and paramaters for the Users.settings.forwardingAddresses method can be found here where it details that on call of forwardingAddresses.create(args):
"...if ownership verification is required, a message will be sent to the recipient and the resource's verification status will be set to pending; otherwise, the resource will be created with verification status set to accepted.

how to have google apps engine send mail- not send a copy of the mail to the sender

I'm using GAE send mail- but I dont want the sender of the mail to get a coppy of the mail.
as for now, when a user is sending mail he gets a mail saying that he sent a mail to someone and the body of the sent mail, how do I disable that?
You can't. Sending email from someone without their knowledge isn't permitted by App Engine.
You can send email from any administrator address; you could add a "donotreply#yourapp.com" type address as an administrator and send email from that address.

Categories