web-dev-qa-db-ja.com

複数の受信者を送信する方法sendgridV3 api Python

誰でも助けてください、私はsendgrid v3apiを使用しています。しかし、複数の受信者にメールを送信する方法が見つかりません。少し早いですがお礼を。

    import sendgrid
    from sendgrid.helpers.mail import *

    sg = sendgrid.SendGridAPIClient(apikey="SG.xxxxxxxx")
    from_email = Email("FROM EMAIL ADDRESS")
    to_email = Email("TO EMAIL ADDRESS")
    subject = "Sending with SendGrid is Fun"
    content = Content("text/plain", "and easy to do anywhere, even with Python")
    mail = Mail(from_email, subject, to_email, content)
    response = sg.client.mail.send.post(request_body=mail.get())
    print(response.status_code)
    print(response.body)
    print(response.headers)

複数の受信者にメールを送信したい。 to_mail = "xxx @ gmail.com、yyy @ gmail.com"のように。

8
P113305A009D8M

Sendgridv3で複数のrecicpentにメールを送信します。

        import time
        import sendgrid
        import os

        print "Send email to multiple user"

        sg = sendgrid.SendGridAPIClient(apikey='your sendrid key here')
        data = {
        "personalizations": [
            {
            "to": [{
                    "email": "[email protected]"
                }, {
                    "email": "[email protected]"
                }],
            "subject": "Multiple recipent Testing"
            }
        ],
        "from": {
            "email": "[email protected]"
        },
        "content": [
            {
            "type": "text/html",
            "value": "<h3 style=\"color: #ff0000;\">These checks are silenced please check dashboard. <a href=\"http://sensu.mysensu.com/#/silenced\" style=\"color: #0000ff;\">Click HERE</a></h3>  <br><br> <h3>For any query please contact DevOps Team<h3>"
            }
        ]
        }
        response = sg.client.mail.send.post(request_body=data)
        print(response.status_code)
        if response.status_code == 202:
            print "email sent"
        else:
            print("Checking body",response.body)

https://libraries.io/github/sendwithus/sendgrid-python

5
Adiii

以下の方法でコードを更新できます。 Sendgridのパッケージに含まれているmail_example.pyにも同じものがあります。

personalization = get_mock_personalization_dict()
mail.add_personalization(build_personalization(personalization))

def get_mock_personalization_dict():
    """Get a dict of personalization mock."""
    mock_pers = dict()
    mock_pers['to_list'] = [Email("[email protected]",
                              "Example User"),
                            Email("[email protected]",
                              "Example User")]
    return mock_pers

def build_personalization(personalization):
    """Build personalization mock instance from a mock dict"""
    mock_personalization = Personalization()
    for to_addr in personalization['to_list']:
        mock_personalization.add_to(to_addr)
    return mock_personalization
3
Subhrajyoti Das

ここにある他の回答のコードでは、電子メールの受信者は、TOフィールドにお互いの電子メールアドレスを表示することに注意してください。これを回避するには、電子メールアドレスごとに個別のPersonalizationオブジェクトを使用する必要があります。

def SendEmail():
    sg = sendgrid.SendGridAPIClient(api_key="YOUR KEY")
    from_email = Email ("FROM EMAIL ADDRESS")

    person1 = Personalization()
    person1.add_to(Email ("EMAIL ADDRESS 1"))
    person2 = Personalization()
    person2.add_to(Email ("EMAIL ADDRESS 2"))

    subject = "EMAIL SUBJECT"
    content = Content ("text/plain", "EMAIL BODY")
    mail = Mail (from_email, subject, None, content)
    mail.add_personalization(person1)
    mail.add_personalization(person2)
    response = sg.client.mail.send.post (request_body=mail.get())

    return response.status_code == 202
3
asmaier

文字列のリストを渡すことができます。

message = Mail(
            from_email='[email protected]',
            to_emails=['[email protected]', '[email protected]'],
            subject='subject',
            html_content='content')

sg = SendGridAPIClient('SENDGRID_API_KEY')
response = sg.send(message)
0
ST7