Sending emails using Python smtplib and EmailMessage() - python

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")

Related

SMTPAuthenticationError code 535 with python

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']}")

Why doesn't my bot of sending Emails using smtplib work?

see the code below, every time i try to use this function, it does not work and i kinda don't know why, so somebody could give me a hand?
import smtplib
def send_email():
server = smtplib.SMTP('smtp.hotmail.com', 587)
server.ehlo()
server.starttls()
server.ehlo()
server.login('bot_piloto.test#hotmail.com', '*******')
subject = 'You have to see this, it is about your university'
body = 'look, these are yours pendenting homeworks: ' + name_teacher2 + ' and these are yours expired homeworks: '+ name_teacher
sender: 'bot_piloto.test#hotmail.com'
receiver: 'bot_piloto.test#hotmail.com'
msg = f"Subject: {subject}\n\n{body}"
server.sendmail(
sender,
receiver,
msg
)
server.quit()
This is pretty different from your original code, but this is the method thay I've found works for me. Use it or don't, but this method is pretty reliable.
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import ssl
def send_email():
subject = 'You have to see this, it is about your university'
body = 'look, these are yours pendenting homeworks: ' + name_teacher2 + ' and these are yours expired homeworks: '+ name_teacher
sender = 'bot_piloto.test#hotmail.com'
receiver = 'bot_piloto.test#hotmail.com'
msg = MIMEMultipart()
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = receiver
body = MIMEText(body, "plain")
msg.attach(body)
#Create secure connection with server and send email
context = ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.hotmail.com", 587, context=context) as server:
server.login(sender, "*******")
server.sendmail(
sender,
receiver,
msg.as_string()
)

python smtpd.SMTPServer: How to send reply message in process_message?

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()

send email using python using Gmail

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

Send email via python in c++

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.

Categories