Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. <?php
  2.  
  3. require __DIR__ . '/vendor/autoload.php';
  4.  
  5. use \DrewM\MailChimp\MailChimp;
  6.  
  7. $mc = new MailChimp('<your-mailchimp-api-key>');
  8. // list ID
  9. // When a user unsubscribes from the list, they cannot be subscribed again
  10. // via the API, so use a unique list for this mailing purpose
  11. $list_id = 'fb05b906b8';
  12. $email_address = 'test@example.com'; // where to send
  13. $template_id = 36037; // input your template ID
  14. $subject = 'Hello there!';
  15. $message = '<h2>Hooray!</h2><p>It worked!</p>';
  16. $from_name = 'From Name';
  17. $from_email = 'you@yours.com';
  18.  
  19. # 0. subscribe user if not subscribed
  20. $subscriber_hash = $mc->subscriberHash( $email_address );
  21.  
  22. $result = $mc->get("lists/{$list_id}/members/{$subscriber_hash}");
  23.  
  24. if ( ! isset( $result['status'] ) || 'subscribed' !== $result['status'] ) {
  25. $result = $mc->post("lists/{$list_id}/members", [
  26. 'email_address' => $email_address,
  27. 'status' => 'subscribed',
  28. ]);
  29. }
  30.  
  31. // Check if user subscribed
  32. // $is_subscribed = isset($result['status']) && 'subscribed' === $result['status'];
  33.  
  34. # 1. create campaign
  35. $result = $mc->post('campaigns', [
  36. 'type' => 'regular',
  37. 'recipients' => [
  38. 'list_id' => $list_id,
  39. 'segment_opts' => [
  40. 'match' => 'all',
  41. 'conditions' => [
  42. [
  43. 'condition_type' => 'EmailAddress',
  44. 'field' => 'EMAIL',
  45. 'op' => 'is',
  46. 'value' => $email_address
  47. ]
  48. ]
  49. ],
  50. ],
  51. 'settings' => [
  52. 'subject_line' => $subject,
  53. 'from_name' => $from_name,
  54. 'reply_to' => $from_email,
  55. 'template_id' => $template_id,
  56. ],
  57. ]);
  58.  
  59. if ( ! isset( $result['id'] ) || ! $result['id'] )
  60. return;
  61.  
  62. $campaign_id = $result['id'];
  63.  
  64. // 2. update campaign
  65. $result = $mc->put("campaigns/{$campaign_id}/content", [
  66. 'template' => [
  67. 'id' => $template_id,
  68. 'sections' => [
  69. 'std_content00' => $message
  70. ]
  71. ],
  72. ]);
  73.  
  74. // 3. send campaign
  75. $result = $mc->post("campaigns/{$campaign_id}/actions/send");
  76.  
  77. $success = is_bool($result) && $result;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement