【问题标题】:How to send mail to multiple recipients on SMTP form如何在 SMTP 表单上向多个收件人发送邮件
【发布时间】:2020-02-10 07:37:41
【问题描述】:

我已经制作了一个表格,用户可以输入多个电子邮件地址,然后我的程序将向这些电子邮件地址发送一封邮件。我的程序确实收集了输入,但 SMTP 仅将其发送到一个电子邮件地址。我希望它一次将其发送到多个电子邮件地址。

这是我的代码:

def results():
    userdata = request.form
    quantity = userdata['quantity']
    name = userdata['name']
    email = userdata['email']
    print(email)
    msg = Message(subject="Hello", sender='populargifsontwitter@gmail.com', recipients=[email],
                  body="Hi!" + name + "This is a test email I sent with Gmail and Python!")
    # calling mail and the send method and passing the message
    mail.send(msg)
    return render_template('results.html', quantity=quantity, email=email, name=name)

编辑:我被建议拆分它。

这就是我所做的:

email = email.split(",")
    print(email)
    for x in email:
        msg = Message(subject="Hello", sender='populargifsontwitter@gmail.com', recipients= [x], body="Hi!"+name+"This is a test email I sent with Gmail and Python!" )
    #calling mail and the send method and passing the message
        mail.send(msg)

问题:现在电子邮件只会发送给第二个收件人,而不是第一个收件人。我应该怎么做,为什么它只发送给第二个收件人?

【问题讨论】:

  • 您尝试过列表中的电子邮件吗?我建议你使用 mailgun API。
  • recipients 获取一个或多个电子邮件地址的列表。您必须确保在将用户提供的电子邮件分配给recipients 之前对其进行拆分。您是否还使用一封电子邮件对其进行了测试,以确保它可以正常工作。
  • 我试图拆分它,但电子邮件被发送到第二个电子邮件地址而不是第一个。

标签: routes smtp flask-mail


【解决方案1】:

reddit 上有人说 SMTP 服务器有问题。他们建议在循环结束之前发送它之后添加一个小的延迟,它起作用了!

for x in email:
        msg = Message(subject="Hello", sender='janedoe@gmail.com', recipients= [x], body="Hi!"+name+"This is a test email I sent with Gmail and Python!" )
    #calling mail and the send method and passing the message
        time.sleep(2)
        mail.send(msg)
    return render_template('results.html', quantity = quantity, email = email, name = name)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-29
    • 1970-01-01
    • 1970-01-01
    • 2012-09-24
    • 2020-11-16
    • 2020-09-15
    相关资源
    最近更新 更多