Python smtplib: Unable to send sms messages to phone - python

I am trying to send messages to my phone using the SMTP protocol. If I log into my Google Account (for which I've enabled less secure apps) I'm able to send a message to '5551234567#tmomail.net'. The subject and body of the email arrive on my phone as a text message.
However, when I try to do the same with Python's smtplib library, I don't get a message. Here's the code I'm using:
import smtplib
# Establish a secure session with gmail's outgoing SMTP server using your gmail account
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
# the account that will send the emails
server.login('me#gmail.com', 'password')
# sendmail(from, to, msg)
server.sendmail('me#gmail.com', '5551234567#tmomail.net', 'hey there!')
Does anyone know what I can do to get the text message to come through from the smtplib? Any suggestions are very welcome!

Try to check the link below.
If seems like you for forgot
server.ehlo()
How to send an email with Gmail as provider using Python?
Please let us know if you see python message in SENT emails folder of Gmail Inbox.
If yes, try to find a differences between one you sent from browser and one you sent from python API.

Related

How can I send email from python with 2 Factor Authentication?

I have created python file that can send email using less secure apps turned on, but I need it off. How can I send email with 2FA?
# import simple mail transfer protocol library
import smtplib
# import EmailMessage method
from email.message import EmailMessage
contacts = ['<email#email.com>', '<email2#email.com>']
EMAIL_ADDRESS = '<my_gmail>'
EMAIL_PASSWORD = '<my_gmail_password>'
# Create empty Email Message object
msg = EmailMessage()
msg['Subject'] = 'Automated python email sender 5'
msg['From'] = EMAIL_ADDRESS
msg['To'] = contacts
msg.set_content('<sample_content>')
# contact manager we will make sure our connection is closed automatically without us doing it manually
# 465 is the port number for plain SMTP_SSL
# SMTP_SSL means you do not have to use ehlo() ans starttls()
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
smtp.send_message(msg)
What should I add to make 2FA work?
I believe you would need to set up an App passwords let you sign in to your Google Account from apps on devices that don't support 2-Step Verification. Learn more
Its basically something thats setup in the users Google account.
Another thing to look at would be Display captcha
if that doesn't work you might need to look into Xoauth2
if you require 2fa
If you want to support 2fa then you should not be using the SMTP server and should be going though the Gmail API and using Oauth2 which will prompt a user to connect via their google account and google will control the 2fa themselves. SMTP servers are not designed to handel 2fa
Try setting up App Password for your Gmail. and used that app password for smtp login.
This will help Set Up APP-Password
Under security you have to generate a password for your application:
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login('demo#gmail.com', 'password generated by google')
server.sendmail('demo#gmail.com', 'demo#icloud.com', 'Mail sent from program')
print('Mail Sent')
as you can see, you enter your email but instead of typing your own password you enter the password generated by google for your application

Any idea of how to spoof an email sender in python using smtplib?

I was wasting another day of my life messing around with python and I was thinking about how to spoof an email address using smtplib in python. I thought it could be possible to spoof the from address by using the code below but it didn't work. I have seen projects on github that seem to be able to actually spoof the email but if i were to create a program of my own that does this, how exactly would it work? Any ideas? Code:
import smtplib
username = (mygmailusername)
password = (mypassword)
fakefrom = "donaldtrump#gmail.com"
toEmail = (toAddress)
server = smtplib.SMTP("smtp.gmail.com",587)
server.starttls()
server.login(username,password)
server.sendmail(fakefrom,toEmail,"this is the fbi. OPEN UP")
server.close()
Most providers check the account with the sender address, and disallow if the sender address dosn't match with sender. Try if you have a setup box network, to use your provider network smtp server without authentification on smtp 25 port, and most of time the mail can be sent. But with that method, many receiver servers will tag directly this mail as spam/dangerous. You can try that directly in your message tool (outlook,...) it s the same behavior as you code.
Sorry I'm a bit late. The problem, here lies with the second-to-last line:
server.sendmail(fakefrom,toEmail,"this is the fbi. OPEN UP")
You are trying to use the 'fake from' address to actually send the email which isn't what you want to be doing. You just want to 'spoof' it and make the recipient think that the email came from a different address. You do that by defining the sender details in the message body.
The code that you would need to use to make this work would be:
import smtplib
username = (mygmailusername)
password = (mypassword)
fake_from = "donaldtrump#gmail.com"
fake_name = "Donald Trump"
to_email = (toAddress)
to_name = (toName)
subject = "Bonjour"
content = "This is the fbi. OPEN UP"
message = f"From: {fake_name} <{fake_from}>\nTo: {to_name} <{to_email}>\nSubject: {subject}\n\n{content}"
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login(username, password)
server.sendmail(username, to_email, message.encode())
server.close()

Gmail API Oauth2 Send from different address

I am able to send e-mails using Python and the google libraries (googleapiclient, google_auth_oauthlib.flow, and google.auth.transport.requests). I'm trying to get an alternate "from" or "reply-to" address, and it's not working. My code for creating a message is like so:
# Returns an e-mail message suitable for submission through the gmail api.
def create_message(sender, to, subject, message_text):
message = MIMEMultipart()
message['to'] = to
#message['from'] = sender
message['from'] = 'derp#nowhere.com'
message['subject'] = subject
message.attach(MIMEText(message_text, 'html'))
return {'raw': base64.urlsafe_b64encode(message.as_string().encode()).decode()}
As you can see, I've adjusted the from attribute of the message. When this message is sent, it is from the gmail account owner, not "derp". How do I make the message come from a different address?
Add the e-mail address as whom you want to issue e-mail as a "Send Mail As" account: https://mail.google.com/mail/u/2/#settings/accounts. It is necessary for that other account to verify he has control of that e-mail address.
It appears not to be possible to "spoof" e-mails using gmail oauth without this verification process, though if you use the deprecated "less secure apps", it is still possible.

Python sending e-mail with SMTP without authentication

I'm making a script that notifies people about some pending tickets in JIRA. These notifications are sent by e-mail, I already got the notification to trigger, but I'm having problems sending the emails.
I can send them using gmail but when I tried to do it with my official account (the one that the company gave me) I am not able to send them. IT guys already provided me the 'localhost' because they use SMTP relays and the port, but they keep telling me that I should start SMTP without authentication, I'm not very sure of how to do this.
The example I found on internet was this:
import smtplib
fromaddr = 'Axel.Sa#mydomain.com'
toaddrs = ['Axel.Sa#mydomain.com']
msg = '''
From: {fromaddr}
To: {toaddr}
Subject: testin'
This is a test
.
'''
msg = msg.format(fromaddr=fromaddr, toaddr=toaddrs[0])
server = smtplib.SMTP('localhost:25')
server.starttls()
server.ehlo("mydomain.com")
server.mail(fromaddr)
server.rcpt(toaddrs[0])
server.data(msg)
server.quit()
But I keep getting this error, If someone can tell me the proper way of sending emails by SMTP without authentication I will be very grateful.
Check this stack:
How to send an email without login to server in Python
change your smtplib.SMTP('localhost:25') to smtplib.SMTP('localhost', 25)

Can't send email with smtp

I want to know why this code can't send email.
import smtplib
content = 'test'
mail = smtplib.SMTP('smtp.gmail.com',587)
mail.ehlo()
mail.starttls()
mail.login('surapon#gmail.com','222222')
mail.sendmail('surapon#gmail.com','youremail#gmail.com',content)
mail.quit
It shows:
SMTPAuthenticationError: (535, '5.7.8 Username and Password not accepted. Learn more at\n5.7.8 support.google.com/mail/answer/14257 ho10sm6301275pbc.27 - gsmtp')
Later, it shows:
SMTPAuthenticationError: (534, '5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbu87\n5.7.14 wdTx8uq_F_WXKXEVia5I3DTMdhzuJL967nviDbOqgBU9lHzjzIHX69az6PFAzff6lA2uGJ\n5.7.14 qCqwJzys1OcoqMzMNUx5o5ja_a3XHatcxE-jqsHjqWCwYR1WVUEmBfGvUIBzgm7iUyGOXq\n5.7.14 RdYOqEx5GLAe05yUhGq-z-JphFKH-x-aA0TwEc-hyEnecghY1ZLtMMsowPhFGa1XGPnNO3\n5.7.14 8XE4yhQctKtYySbTSiQqBUmmV4qE> Please log in via your web browser and\n5.7.14 then try again.\n5.7.14 Learn more at\n5.7.14 support.google.com/mail/answer/78754 eu5sm6412101pac.37 - gsmtp')
I created yagmail as a package to make it really simple to send emails.
Please try the following:
import yagmail
yag = yagmail.SMTP('surapon#gmail.com', 'password')
yag.send('youremail#gmail.com', subject = 'hi', contents = content)
There are some useful other tricks you can do with the package, such as never having to enter the password again (while still being secure), and it makes it extremely easy to send attachments.
Install with either
pip install yagmail # python 2
pip3 install yagmail # python 3
and for more information please have a look at github.
Due to security issues gmail blocks accessing mail via code or program
But still you can use gmail to send mail via code if you do the following things
What i have done in code :
1.Added a error object to get the error message
import smtplib
try:
content = 'test'
mail = smtplib.SMTP('smtp.gmail.com',587)
mail.ehlo()
mail.starttls()
mail.login('surapon#gmail.com','222222')
mail.sendmail('surapon#gmail.com','youremail#gmail.com',content)
mail.quit
print "Successfully sent email"
except smtplib.SMTPException,error:
print str(error)
print "Error: unable to send email"
If u ran this code u would see a error message like this stating that google is not allowing u to login via code
Things to change in gmail:
1.Login to gmail
2.Go to this link https://www.google.com/settings/security/lesssecureapps
3.Click enable then retry the code
Hopes it help :)
But there are security threats if u enable it
This is because google considers python SMTPLIB less secure. The link in the error message leads you to the answer.
Within that link there is another link to allowing "less secure" apps to send mail.
https://support.google.com/accounts/answer/6010255
This allows you to specifically allow your application to send email. They provide very little information on what constitutes the security issue.
The link they provide to give explicit access is
http://www.google.com/settings/security/lesssecureapps within the earlier link.
I'll explain what you're trying to do. You're attempting to do an SMTP with the following credentials:'smtp.gmail.com',587
Then you're attempting to login with your gmail creds. Totally wrong.
What you want to do is:
import smtplib
content = 'test'
me = 'surapon#gmail.com'
you = ['someOther#gmail.com']
mail = smtplib.SMTP('localhost')
msg['Subject'] = 'Hello'
msg['From'] = me
msg['To'] = you[0]
#mail.ehlo()
#mail.starttls()
#mail.login('surapon#gmail.com','222222')
mail.sendmail(me, you, msg.as_string())
mail.quit()
Do not include the #gmail.com in your credentials. I used your code with my username (without #gmail.com) and password and was able to send a message.
import smtplib
content = 'test'
mail = smtplib.SMTP('smtp.gmail.com',587)
mail.ehlo()
mail.starttls()
mail.login('surapon','222222')
mail.sendmail('surapon#gmail.com','youremail#gmail.com',content)
mail.quit

Categories

Resources