I tried with "allow less secure app" and "unlockCaptcha", but still not work.
How to know if the problem is with my account or with the code?
Thanks for attention, here teh code:
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib
# Import the email modules we'll need
from email.message import EmailMessage
# create message object instance
msg = MIMEMultipart()
message = "Prova"
# setup the parameters of the message
password="pass"
msg['From'] = "senders#example.com"
msg['To'] = "receiver#example.com"
msg['Subject'] = "Prova"
# add in the message body
msg.attach(MIMEText(message, 'plain'))
#create server
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.ehlo()
# Login Credentials for sending the mail
server.login(msg['From'], password)
# send the message via the server.
server.sendmail(msg['From'], msg['To'], msg.as_string())
server.quit()
print(f"successfully sent email to {msg['To']}")
Related
I'm trying to send message on email using Python smtplib and EmailMessage().
import smtplib
from email.message import EmailMessage
def email_alert(subject, body, to):
msg = EmailMessage()
msg.set_content(body)
msg['subject'] = subject
msg['to'] = to
user = 'username#gmail.com'
msg['from'] = user
password = 'app_password'
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(user, password)
server.send(msg)
server.quit()
email_alert("hey", "Hello world","another#mail.com")
But error occures "TypeError: memoryview: a bytes-like object is required, not 'EmailMessage'".
What's the problem with the code? I saw video where this code worked.
Working code
import smtplib
from email.message import EmailMessage
def email_alert(subject, body, to):
msg = EmailMessage()
msg.set_content(body)
msg['subject'] = subject
msg['to'] = to
user = 'username#gmail.com'
msg['from'] = user
password = 'app_password'
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(user, password)
server.send_message(msg) # <- UPDATED
server.quit()
email_alert("hey", "Hello world","another#mail.com")
I have a code which was working like a half year ago. It basiclly sends email.
import smtplib
import socket
gmail_user="SENDERMAIL"
gmail_password="SENDERPASS"
to = 'SENDTOTHIS'
email_text = "ADSADSADSA"
try:
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.ehlo()
server.login(gmail_user, gmail_password)
server.starttls()
server.sendmail(gmail_user, to, email_text)
server.close()
#I was using this code below and it was working. I tried above code but it also did not work.
#server = smtplib.SMTP("smtp.gmail.com:587")
#server.ehlo()
#server.starttls()
#server.ehlo()
#server.login(gmail_user, gmail_password)
#server.sendmail(gmail_user, to, email_text)
#server.close()
print("Done")
except Exception as exception:
print(exception)
Here's exception
(534, b'5.7.14
5.7.14 KL7_2qGSLW9IBjP8dKKgP67bEgyKNc5ls76dnVDZcUlVQjJUQb0JX9BIVi_Agb84vKNOKB
5.7.14 fshB0ngZ_Tn8ocDpDHKavRKXmluVjHo5YM7ADKENtWn4aVTxyvaBlbXRGpA1EBh91bdV-o
5.7.14 pwiAWUHXKmRQEuSNSiFcv68DP4a7ghIu9YKnTyqtUEhGd4HgKtxa4Jz0mhSQDjD13UQWYB
5.7.14 -YEL5Sd2h5YxN8kkSAsK-J_hXMbpy7wNyeCov8lq1Aa3spZzgo> Please log in via
5.7.14 your web browser and then try again.
5.7.14 Learn more at
5.7.14 https://support.google.com/mail/answer/78754 f132-v6sm3660398wme.24 - gsmtp')
I did try to
logined gmail
add device to trusted devices
turned on IMAP via gmail
let less secure apps
tried this:
https://support.google.com/mail/answer/7126229?visit_id=636711453029417344-336837064&rd=2#cantsignin
There are to many ways to solve this problem. I hope this code helps.
The only thing you need to do is filling the required variables.
import socket
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
#
message = "Your message" # Type your message
msg = MIMEMultipart()
password = "********" # Type your password
msg['From'] = "from#gmail.com" # Type your own gmail address
msg['To'] = "To#gmail.com" # Type your friend's mail address
msg['Subject'] = "title" # Type the subject of your message
msg.attach(MIMEText(message, 'plain'))
server = smtplib.SMTP('smtp.gmail.com: 587')
server.starttls()
server.login(msg['From'], password)
server.sendmail(msg['From'], msg['To'], msg.as_string())
server.quit()
I can also advise to use a simpler library (a wrapper on top of smtplib, to make sure there are no other factors involved).... like yagmail (disclaimer: I'm the developer).
Try to see if this works:
import yagmail
yag = yagmail.SMTP("username", "password")
yag.send(subject="hi")
In my implementation of smtpd.SMTPServer, I can receive messages via the process_message() callback. How could I send a reply message back to the sender?
This is a script that can send email it works for me. I don't know if is that you want but I hope its help you.
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
fromaddr = "YOUR ADDRESS"
toaddr = "ADDRESS YOU WANT TO SEND TO"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "SUBJECT OF THE MAIL"
body = "YOUR MESSAGE HERE"
msg.attach(MIMEText(body, 'plain'))
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, "YOUR PASSWORD")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()
I have written a python script to send email from my gmail account to my another email. the code is:
import smtplib
fromaddr = 'my#gmail.com'
toaddrs = 'to#gmail.com'
msg = 'my message'
username = 'my#gmail.com'
password = 'myPass'
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
print("Done")
I'm also getting the "Done" output as the email is being sent. But the problem is: I can't seem to receive the email. It doesn't show up in the inbox. I can't find the problem :( Can anyone please help?
Thanks in advance...
Check this out, this works for me perfectly...
def send_mail(self):
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
gmailUser = 'myemail#gmail.com'
gmailPassword = 'P#ssw0rd'
recipient = 'sendto#gmail.com'
message='your message here '
msg = MIMEMultipart()
msg['From'] = gmailUser
msg['To'] = recipient
msg['Subject'] = "Subject of the email"
msg.attach(MIMEText(message))
mailServer = smtplib.SMTP('smtp.gmail.com', 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(gmailUser, gmailPassword)
mailServer.sendmail(gmailUser, recipient, msg.as_string())
mailServer.close()
Enable less secure apps on your gmail account and for Python3 use:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
gmailUser = 'XXXXX#gmail.com'
gmailPassword = 'XXXXX'
recipient = 'XXXXX#gmail.com'
message = f"""
Your message here...
"""
msg = MIMEMultipart()
msg['From'] = f'"Your Name" <{gmailUser}>'
msg['To'] = recipient
msg['Subject'] = "Your Subject..."
msg.attach(MIMEText(message))
try:
mailServer = smtplib.SMTP('smtp.gmail.com', 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(gmailUser, gmailPassword)
mailServer.sendmail(gmailUser, recipient, msg.as_string())
mailServer.close()
print ('Email sent!')
except:
print ('Something went wrong...')
my question is whether I can send an email via python in a c++ program.
That is my actual python script.
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
# From
fromaddr = "...#gmail.com"
# To
toaddr = "..."
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
# subject
msg['Subject'] = "..."
# Text
body = "..."
msg.attach(MIMEText(body, 'plain'))
#smtplib import
import smtplib
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.ehlo()
server.login("username", "password")
text = msg.as_string()
#send email
server.sendmail(fromaddr, toaddr, text)
My plan ist it, that I will open python and import the script via the commandline in the programm.
Is there a better way to do it?
The problem is that c++ open via system("python") the commandline, but now I have to write import mail.py in the command line.
Can I do this automaticly with an other order? I will open python, import mail and quit with one order. Is that possible?
Thank You!
Here is the program for an email.
smtpserver = smtplib.SMTP("smtp.gmail.com",587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo()
smtpserver.login(email, password)
smtpserver.sendmail(email, recipient, text)
print 'Message Sent!'
smtpserver.close()
The variables: email is your email address, password is the password to your email, recipient is the receiver of the email, and text is the text to send.