Reputation: 61
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 = '[email protected]'
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","[email protected]")
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.
Upvotes: 1
Views: 4416
Reputation: 3670
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 = '[email protected]'
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","[email protected]")
Upvotes: 3