Last active
October 21, 2019 01:50
-
-
Save elhardoum/179bcb2c1d4ec2aed83a6d85738ae383 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require __DIR__ . '/vendor/autoload.php'; | |
use \DrewM\MailChimp\MailChimp; | |
$mc = new MailChimp('<your-mailchimp-api-key>'); | |
// list ID | |
// When a user unsubscribes from the list, they cannot be subscribed again | |
// via the API, so use a unique list for this mailing purpose | |
$list_id = 'fb05b906b8'; | |
$email_address = '[email protected]'; // where to send | |
$template_id = 36037; // input your template ID | |
$subject = 'Hello there!'; | |
$message = '<h2>Hooray!</h2><p>It worked!</p>'; | |
$from_name = 'From Name'; | |
$from_email = '[email protected]'; | |
# 0. subscribe user if not subscribed | |
$subscriber_hash = $mc->subscriberHash( $email_address ); | |
$result = $mc->get("lists/{$list_id}/members/{$subscriber_hash}"); | |
if ( ! isset( $result['status'] ) || 'subscribed' !== $result['status'] ) { | |
$result = $mc->post("lists/{$list_id}/members", [ | |
'email_address' => $email_address, | |
'status' => 'subscribed', | |
]); | |
} | |
// Check if user subscribed | |
// $is_subscribed = isset($result['status']) && 'subscribed' === $result['status']; | |
# 1. create campaign | |
$result = $mc->post('campaigns', [ | |
'type' => 'regular', | |
'recipients' => [ | |
'list_id' => $list_id, | |
'segment_opts' => [ | |
'match' => 'all', | |
'conditions' => [ | |
[ | |
'condition_type' => 'EmailAddress', | |
'field' => 'EMAIL', | |
'op' => 'is', | |
'value' => $email_address | |
] | |
] | |
], | |
], | |
'settings' => [ | |
'subject_line' => $subject, | |
'from_name' => $from_name, | |
'reply_to' => $from_email, | |
'template_id' => $template_id, | |
], | |
]); | |
if ( ! isset( $result['id'] ) || ! $result['id'] ) | |
return; | |
$campaign_id = $result['id']; | |
// 2. update campaign | |
$result = $mc->put("campaigns/{$campaign_id}/content", [ | |
'template' => [ | |
'id' => $template_id, | |
'sections' => [ | |
'std_content00' => $message | |
] | |
], | |
]); | |
// 3. send campaign | |
$result = $mc->post("campaigns/{$campaign_id}/actions/send"); | |
$success = is_bool($result) && $result; |
what will happend when never update campaign content if i wanna get content staticly from mailchimp template?
I haven't test this, but I think you should be able to skip step 3 (update campaign) since you don't have dynamic content to replace, and since the previous step assigns the
template_id
while creating the new campaign.
I already did this and skip 3 step (update campaign content) but it must send check list first before send a campaign. It worked perfectly. Thanks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I haven't test this, but I think you should be able to skip step 3 (update campaign) since you don't have dynamic content to replace, and since the previous step assigns the
template_id
while creating the new campaign.