You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用MailChimp API在PHP中向指定邮箱发送邮件

使用MailChimp API发送邮件的完整PHP实现方案

嘿,我看了你给出的部分MailChimp API代码,这里帮你补全并梳理清楚两种可行的实现方案,适配你的需求——给Clients列表里的指定邮箱rajbiswas176@gmail.com发送邮件:


方案一:使用MailChimp Marketing API(列表营销场景)

如果你是要基于已有的Clients列表发送营销类邮件,这个方案更贴合MailChimp的原生逻辑,需要分三步完成:创建营销活动、配置邮件内容、触发发送。

完整代码

<?php
// 替换为你实际的API密钥和Clients列表ID
$apikey = 'your_actual_api_key';
$list_id = 'your_clients_list_id';

// 收件人信息
$to_emails = array('rajbiswas176@gmail.com');
$to_names = array('Raj');

// 构建邮件核心内容
$message = array(
    'html' => 'Yo, this is the <b>html</b> portion',
    'text' => 'Yo, this is the *text* portion',
    'subject' => 'Your Custom Subject Line Here', // 补全邮件主题
    'from_email' => 'verified_sender@example.com', // 必须是MailChimp验证过的发件人邮箱
    'from_name' => 'Your Brand Name' // 发件人显示名称
);

// 初始化cURL请求
$ch = curl_init();
// 自动获取API密钥对应的 data center(比如us12)
$data_center = substr($apikey, strpos($apikey, '-') + 1);
curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $apikey);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);

// 第一步:创建定向发送的营销活动(只发给指定邮箱用户)
$campaign_data = json_encode(array(
    'type' => 'regular',
    'recipients' => array(
        'list_id' => $list_id,
        'segment_opts' => array(
            'match' => 'all',
            'conditions' => array(
                array(
                    'condition_type' => 'EmailAddress',
                    'field' => 'email_address',
                    'op' => 'eq',
                    'value' => $to_emails[0]
                )
            )
        )
    ),
    'settings' => array(
        'subject_line' => $message['subject'],
        'from_name' => $message['from_name'],
        'reply_to' => $message['from_email'],
        'title' => 'Custom Campaign for Raj'
    )
));
curl_setopt($ch, CURLOPT_URL, "https://{$data_center}.api.mailchimp.com/3.0/campaigns");
curl_setopt($ch, CURLOPT_POSTFIELDS, $campaign_data);
$campaign_response = curl_exec($ch);
$campaign_result = json_decode($campaign_response, true);

if (isset($campaign_result['id'])) {
    $campaign_id = $campaign_result['id'];
    
    // 第二步:设置营销活动的邮件内容
    $content_data = json_encode(array(
        'html' => $message['html'],
        'text' => $message['text']
    ));
    curl_setopt($ch, CURLOPT_URL, "https://{$data_center}.api.mailchimp.com/3.0/campaigns/{$campaign_id}/content");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $content_data);
    curl_exec($ch);
    
    // 第三步:触发邮件发送
    curl_setopt($ch, CURLOPT_URL, "https://{$data_center}.api.mailchimp.com/3.0/campaigns/{$campaign_id}/actions/send");
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array()));
    $send_response = curl_exec($ch);
    
    $send_result = json_decode($send_response, true);
    if (isset($send_result['status']) && $send_result['status'] === 'sent') {
        echo "邮件已成功发送给Raj!";
    } else {
        echo "发送失败:" . json_encode($send_result);
    }
} else {
    echo "创建营销活动失败:" . json_encode($campaign_result);
}

curl_close($ch);
?>

关键注意点

  • API密钥与数据中心:MailChimp的API URL需要匹配密钥后缀的 data center(比如us12),代码里已自动提取,不用手动修改。
  • 发件人验证:from_email必须是你在MailChimp后台完成验证的邮箱,否则请求会被拒绝。
  • 定向筛选:通过segment_opts精准定位指定邮箱用户,确保不会误发给列表里的其他用户。

方案二:使用MailChimp Transactional Email(原Mandrill,单封邮件场景)

如果只是想给单个用户发送非营销类的邮件(比如通知、验证码),这个方案更简单直接,不需要创建营销活动,直接调用发送接口即可。

完整代码

<?php
// 替换为你的Transactional Email API密钥(注意:这和Marketing API密钥不是同一个)
$apikey = 'your_mandrill_api_key';

// 收件人信息
$to = array(
    array(
        'email' => 'rajbiswas176@gmail.com',
        'name' => 'Raj',
        'type' => 'to'
    )
);

// 构建邮件内容
$message = array(
    'html' => 'Yo, this is the <b>html</b> portion',
    'text' => 'Yo, this is the *text* portion',
    'subject' => 'Your Custom Subject Line',
    'from_email' => 'verified_sender@example.com', // 需在Transactional后台验证
    'from_name' => 'Your Brand',
    'to' => $to,
    'headers' => array('Reply-To' => 'reply@example.com'),
    'track_opens' => true, // 可选:开启打开跟踪
    'track_clicks' => true // 可选:开启点击跟踪
);

// 初始化并发送请求
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://mandrillapp.com/api/1.0/messages/send.json');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array(
    'key' => $apikey,
    'message' => $message,
    'async' => false // 同步发送,立即获取结果
)));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch);
curl_close($ch);

// 处理响应结果
$response = json_decode($result, true);
if ($response[0]['status'] === 'sent') {
    echo "邮件已成功发送给Raj!";
} else {
    echo "发送失败:" . $response[0]['reject_reason'];
}
?>

关键注意点

  • 独立API密钥:Transactional Email有单独的API密钥,需要在MailChimp后台启用该服务后获取,和Marketing API密钥不通用。
  • 更轻量的场景:适合单次发送非营销邮件,流程更简洁,不需要依赖列表。

内容的提问来源于stack exchange,提问作者RajB009

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 04:20:20