Skip to content

Instantly share code, notes, and snippets.

@elhardoum
Last active October 21, 2019 01:50
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save elhardoum/179bcb2c1d4ec2aed83a6d85738ae383 to your computer and use it in GitHub Desktop.
Save elhardoum/179bcb2c1d4ec2aed83a6d85738ae383 to your computer and use it in GitHub Desktop.
<?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 = 'test@example.com'; // 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 = 'you@yours.com';
# 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;
@th-santos
Copy link

Excellent job!

Redundant boolean literals should be removed from expressions to improve readability:
$success = is_bool($result) && $result;

More info here: https://sbforge.org/sonar/rules/show/squid:S1125?layout=false

Thanks!

@elhardoum
Copy link
Author

Excellent job!

Redundant boolean literals should be removed from expressions to improve readability:
$success = is_bool($result) && $result;

More info here: https://sbforge.org/sonar/rules/show/squid:S1125?layout=false

Thanks!

Thank you very much Thiago! I updated the code.

@aminvincent
Copy link

what will happend when never update campaign content if i wanna get content staticly from mailchimp template?

@elhardoum
Copy link
Author

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.

@aminvincent
Copy link

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