Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?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;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement