python 通过mailchimp发送邮件

我们查看官方apihttps://mailchimp.com/developer/api/可以看到我们只需要Marketing API就可以了

根据他的指南我们可以看到官方的包,以及各个版本的包

首先下载官方对接的sdk

pip install mailchimp-marketing

首先我们要通过创建活动去发送邮件

第一步我们需要

"api_key": "YOUR_API_KEY",
 "server": "YOUR_SERVER_PREFIX"

这个是我们访问官方服务的授权值

在后台可以配置

第二步,创建list

第三步,推入客户数据

第三步,创建segment

第四步,创建活动

最后触发活动

demo如下

# import mailchimp_marketing as MailchimpMarketing
# from mailchimp_marketing.api_client import ApiClientError
import uuid
import shortuuid

#创建list
# try:
#   client = MailchimpMarketing.Client()
#   client.set_config({
#     "api_key": "*************************",
#     "server": "**"
#   })
#
#   response = client.lists.create_list({"name": "TEST", "permission_reminder": "permission_reminder", "email_type_option": True, "contact": {"company": "Beautyforever Official Store", "address1": "9820 Bell Ranch Dr Apt #101", "city": "Santa Fe Springs", "state": "CA", "zip": "90670", "country": "US"}, "campaign_defaults": {"from_name": "Beautyforever Hair", "from_email": "bf@beautyforever.com", "subject": "Don't Miss Member's Day Savings", "language": "English"}})
#   print(response)
# except ApiClientError as error:
#   print("Error: {}".format(error.text))


#创建list之后推入数据,之后创建semgents,之后添加数据
import mailchimp_marketing as MailchimpMarketing
from mailchimp_marketing.api_client import ApiClientError

try:
    client = MailchimpMarketing.Client()
    client.set_config({
        "api_key": "*************************",
        "server": "**"
    })
    #获取list
    # response = client.lists.get_list("*****")
    # print(response)
    #推送客户到list
    # response = client.lists.batch_list_members("******", {"members": [{
    #     'email_address':'979***293@qq.com',"email_type":"html","status":'subscribed',"merge_fields":{"FNAME":'ma','LNAME':"fenglei",
    # "PHONE":'+861518*****31',"PRONAME":""" <table>
    #                 <tr>
    #                     <td>
    #                         <a style="outline: none; text-decoration: none; display: block; width:295px;height: 295px;margin:20px 10px ;background-color: #ffdcf0;" title="Link 1" href="https://www.unice.com/unice-hair-pre-plucked-virgin-hair-body-wave-hd-lace-closure-wigs-amazing-lace-melted-match-all-skin-color-bettyou-series.html?utm_source=HYemail&amp;utm_medium=email&amp;utm_campaign=remindHYU_20201005_ESsale_FlashDeal_EveryDay" target="email"><img style="width: 275px; height:275px;padding: 10px;" src="https://www.unice.com/media/wysiwyg/2020trigger/20201013clearance_02.jpg" alt="off" /></a>
    #                     </td>
    #                     <td>
    #                         <h1 style=" font-size: 14px; line-height:30px;width: 280px; font-weight:normal; text-align: center;display: -webkit-box;-webkit-box-orient: vertical; -webkit-line-clamp: 4; overflow: hidden;margin: 0;padding: 0;">UNice Hair Pre Plucked Virgin Hair Body Wave HD Lace Closure Wigs Amazing Lace Melted Match All Skin Color Bettyou Series</h1>
    #                         <h2 style="width:275px; font-weight:normal;text-align: center;margin: 0;padding: 0;line-height: 45px; "><span style="font-weight: 600;font-size: 40px;">$228.3</span> &nbsp;  <S style="font-size: 20px;color: #716d6c;">$304.43</S> &nbsp; </h2>
    #                         <a style="outline: none; text-decoration: none; display: block;" title="Link 1" href="https://www.unice.com/unice-hair-pre-plucked-virgin-hair-body-wave-hd-lace-closure-wigs-amazing-lace-melted-match-all-skin-color-bettyou-series.html?utm_source=HYemail&amp;utm_medium=email&amp;utm_campaign=remindHYU_20201005_ESsale_FlashDeal_EveryDay" target="email">
    #                         <h3 style="background-color: #fb2d83;color: #ffffff;width: 280px;height: 36px;font-size: 16px;margin: 0 auto;line-height: 36px;text-align: center;">Reward Yourself With Extra $30 OFF</h3></a>
    #                     </td>
    #                 </tr>
    #             </table>"""},
    # }]})
    # print(response)
    #创建list-segments并且添加用户
    # response = client.lists.create_segment("****", {"name": '2020-12-07','static_segment':['26*****679@qq.com','979*****3@qq.com']})
    # print(response)

    #添加用户到segments暂时没用
    # response = client.lists.batch_segment_members(['9796****3@qq.com'], "7*****e98", "2*****")
    # print(response)

    #get_template_id ******
    # response = client.templates.list()
    # print(response)
    # 创建campaigns
    response = client.campaigns.create({"type": "plaintext",'recipients':{"segment_opts":{"saved_segment_id":*****},"list_id":'******'},
                                        "settings":{"subject_line":"Don't Miss Member's Day Savings","preview_text":"- 50% OFF Festive Hairstyles",
                                                    "title":"{}".format(shortuuid.uuid()),"from_name":'Beautyforever Hair',"reply_to":"bf@beautyforever.com",
                                                    "template_id":********,
    #
    }})
    print(response)
    #发送
    response = client.campaigns.send("*******")
    print(response)
except ApiClientError as error:
    print("Error: {}".format(error.text))

 




posted @ 2021-06-29 17:34  你的多啦A梦啊  阅读(248)  评论(0)    收藏  举报