Home > Software engineering >  ValueError at /success Invalid address in django EmailMultiAlternatives
ValueError at /success Invalid address in django EmailMultiAlternatives

Time:10-26

I want sent to multiple email but i got this raise ValueError('Invalid address "%s"' % addr) ValueError: Invalid address "['[email protected]', '[email protected]', '[email protected]']"

        email_id = ["[email protected]","[email protected]","[email protected]"]
        username = name
        email = email_id
        ######################### mail system ####################################
        htmly = get_template('email/Email.html')
        d = { 
            's_id' : s_id,
            'username': username, 
            'tran_id' : tran_id,
            'amount'  : amount

            }
        subject, from_email, to = 'welcome', '[email protected]', email
        html_content = htmly.render(d)
        msg = EmailMultiAlternatives(subject, html_content, from_email, [to])
        msg.attach_alternative(html_content, "text/html")
        msg.send()

CodePudding user response:

You send array type to an another array, So array dimension was changed. Please check like this.

        email_id = ["[email protected]","[email protected]","[email protected]"]
        username = name
        email = email_id
        ######################### mail system ####################################
        htmly = get_template('email/Email.html')
        d = { 
            's_id' : s_id,
            'username': username, 
            'tran_id' : tran_id,
            'amount'  : amount

            }
        subject, from_email, to = 'welcome', '[email protected]', email
        html_content = htmly.render(d)
        msg = EmailMultiAlternatives(subject, html_content, from_email, to)
        msg.attach_alternative(html_content, "text/html")
        msg.send()
    

CodePudding user response:

As other pointed out, you shouldn't enclose to in square brackets because it was already a list of strings, which is exactly what the parameter expects. See this example:

msg = EmailMultiAlternatives("subject", html_content, "from@@gmail.com", ["to@@gmail.com", "to2@@gmail.com", "to3@@gmail.com"])  
  • Related