Sending Emails With Smtp Python, Problem With Def Block
I'm new to python, I'm working with python 3. I need to send an email with generated message. Everything is ok with message (I can print it) but somehow in that configuration, with
Solution 1:
Problem was in message type. I changed it to MIMEText and it works now.
import random
import string
import smtplib
from email.mime.text import MIMEText
port = 2525
smtp_server = "smtp.mailtrap.io"
login = "my mailtrap login"
password = "my mailtrap pass"
sender = "from@smtp.mailtrap.io"
receiver = "to@smtp.mailtrap.io"
def randomString(stringLength=10):
lettersDigits = string.ascii_lowercase + "0123456789"
return ''.join(random.choice(lettersDigits) for i in range(stringLength))
def makeMessage(subject, content):
message = MIMEText(content)
message["Subject"] = subject
message["From"] = sender
message["To"] = receiver
return message
def randomMessage():
return makeMessage(randomString(), randomString())
def sendMessage(message):
with smtplib.SMTP(smtp_server, port) as server:
server.login(login, password)
server.sendmail(sender, receiver, message.as_string())
Post a Comment for "Sending Emails With Smtp Python, Problem With Def Block"