SEND E-MAIL TO MULTIPLE RECIPIENTS WITH SMTPLIB PYTHON

Asked

Viewed 46 times

-2

I am with a code that sends email to a single recipient. Now, I need that code:

• SEND THE E-MAIL TO MULTIPLE RECIPIENTS AT THE SAME TIME OF UNIQUE FORM.

EXAMPLE: I have 20 recipients, instead of writing an email and putting these 20 email addresses in the field: FOR, I need the script send a single email to these 20 people, one by one.

I believe that if I store the recipients in a list, and make the script run 20 times through a FOR solve my problem, but could not put it into practice. Can you help me ? Follow the code below:

import smtplib
import email.message

def enviar_email():  
    corpo_email = """
    <p>Prezados, tudo bem?</p>
    <p></p>
    <p>Venho através deste e-mail para notificar ...</p>
    """

    msg = email.message.Message()
    msg['Subject'] = "ASSUNTO" #ASSUNTO DO E-MAIL#
    msg['From'] = '[email protected]' #E-MAIL QUE VAI ENVIAR O E-MAIL#
    #
    msg['To'] = '[email protected]'#E-MAIL QUE VAI RECEBER
    password = '123456789' #SENHA DO E-MAIL QUE VAI ENVIAR
    msg.add_header('Content-Type', 'text/html')
    msg.set_payload(corpo_email )

    s = smtplib.SMTP('smtp.gmail.com: 587')
    s.starttls()
    # Login Credentials for sending the mail
    s.login(msg['From'], password)
    s.sendmail(msg['From'], [msg['To']], msg.as_string().encode('utf-8'))
    print('Email enviado')
    enviar_email()

  • Loop with for to run the s.sendmail() changing the [msg['To']] by the address you wish to send. PS: I see no reason to use recursion in your code, a loop with for: already "solve" for sending to few people you could simply use BCC, but for bulk sending, marketing or something like that, the idea itself will give problem, even more than most servers have quota limitation, would have to use a dedicated Mailer (has solutions in the market for this, I just will not suggest something because if not my comment may sound like "SPAM")

  • Gui, how can I do that ? I’m sorry, but I’m layman on the subject yet

  • Felipinho, I edited the first comment.

1 answer

-2

does the following uses a mailing list and makes it run sending for each 1 as in the ex below:

import smtplib
import email.message

def enviar_email(email):  
    corpo_email = """
    <p>Prezados, tudo bem?</p>
    <p></p>
    <p>Venho através deste e-mail para notificar ...</p>
    """

    msg = email.message.Message()
    msg['Subject'] = "ASSUNTO" #ASSUNTO DO E-MAIL#
    msg['From'] = '[email protected]' #E-MAIL QUE VAI ENVIAR O E-MAIL#
    #
    msg['To'] = email #E-MAIL QUE VAI RECEBER
    password = '123456789' #SENHA DO E-MAIL QUE VAI ENVIAR
    msg.add_header('Content-Type', 'text/html')
    msg.set_payload(corpo_email )

    s = smtplib.SMTP('smtp.gmail.com: 587')
    s.starttls()
    # Login Credentials for sending the mail
    s.login(msg['From'], password)
    s.sendmail(msg['From'], [msg['To']], msg.as_string().encode('utf-8'))
    print('Email enviado')



lista=['[email protected]','[email protected]','[email protected]']
for email in lista:
    print("enviando email para: ",email)
    enviar_email(email)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.