Sending emails via SmtpLib Python

Let's say there is a code:

mail = smtplib.SMTP('smtp.gmail.com', 587)
mail.ehlo()
mail.starttls()
mail.login('[email protected]','example123')                          
mail.sendmail('[email protected]','[email protected]','Message Text')
mail.close()

The question is: how do I add a title to this email without using MIMETEXT?

Author: insolor, 2018-05-20

2 answers

Try this:

mail = smtplib.SMTP('smtp.gmail.com', 587)
mail.ehlo()
mail.starttls()
mail.login('[email protected]','example123') 
FROM = '[email protected]'
TO = '[email protected]'
BODY = "\r\n".join((
"From: %s" % FROM,
"To: %s" % TO,
"Subject: %s" % SUBJECT ,
"",
text
))
mail.sendmail(FROM,[TO],BODY)
mail.close()
 1
Author: Pashok, 2018-05-21 11:14:59

Try this code then. Here I don't see the MIME text)

# -*- coding: utf-8 -*-
    import smtplib
    def mail(sender, subject, message, to):
        smtp_server = 'smtp.yandex.ru'
        smtp_port = 25
        smtp_pasword = 'pew_pew99'
        mail_lib = smtplib.SMTP(smtp_server, smtp_port)
        mail_lib.login(sender, smtp_pasword)
        # В случае если функции передан не список с получателями
        # а обычную строку
        if isinstance(to, str):
            to = ','.join(to)
        msg = 'From: %s\r\nTo: %s\r\nContent-Type: text/html; charset="utf-8"\r\nSubject: %s\r\n\r\n' % (sender, to, subject)
        msg += message
        mail_lib.sendmail(sender, to, msg)
    # отправляем письмо
    message = """
    Привет из Python!
    """
    mail('[email protected]', 'Yeah Bitch! Magnets!', message, '[email protected]')
 0
Author: Pashok, 2018-05-21 11:31:28