You are here

function mailchimp_campaign_send_campaign in Mailchimp 8

Same name and namespace in other branches
  1. 7.5 modules/mailchimp_campaign/mailchimp_campaign.module \mailchimp_campaign_send_campaign()
  2. 7.2 modules/mailchimp_campaign/mailchimp_campaign.module \mailchimp_campaign_send_campaign()
  3. 7.3 modules/mailchimp_campaign/mailchimp_campaign.module \mailchimp_campaign_send_campaign()
  4. 7.4 modules/mailchimp_campaign/mailchimp_campaign.module \mailchimp_campaign_send_campaign()
  5. 2.x modules/mailchimp_campaign/mailchimp_campaign.module \mailchimp_campaign_send_campaign()

Sends a Mailchimp campaign.

Parameters

\Drupal\mailchimp_campaign\Entity\MailchimpCampaign $campaign: The Mailchimp campaign to send.

Return value

bool TRUE if campaign is sent successfully.

1 call to mailchimp_campaign_send_campaign()
MailchimpCampaignSendForm::submitForm in modules/mailchimp_campaign/src/Form/MailchimpCampaignSendForm.php
This is the default entity object builder function. It is called before any other submit handler to build the new entity object to be used by the following submit handlers. At this point of the form workflow the entity is validated and the form state…

File

modules/mailchimp_campaign/mailchimp_campaign.module, line 189
Module file for mailchimp_campaign.

Code

function mailchimp_campaign_send_campaign(MailchimpCampaign $campaign) {

  /* @var \Mailchimp\MailchimpCampaigns $mc_campaign */
  $mc_campaign = mailchimp_get_api_object('MailchimpCampaigns');

  // Send campaign.
  try {
    if (!$mc_campaign) {
      throw new Exception('Cannot send campaign without Mailchimp API. Check API key has been entered.');
    }
    $mc_campaign
      ->send($campaign->mc_data->id);
    $result = $mc_campaign
      ->getCampaign($campaign->mc_data->id);
    if ($result->status == MAILCHIMP_STATUS_SENDING || $result->status == MAILCHIMP_STATUS_SENT) {

      // Log action, and notify the user.
      \Drupal::logger('mailchimp_campaign')
        ->notice('Mailchimp campaign {name} has been sent.', [
        'name' => $campaign
          ->label(),
      ]);
      $controller = \Drupal::entityTypeManager()
        ->getStorage('mailchimp_campaign');
      $controller
        ->resetCache([
        $campaign
          ->getMcCampaignId(),
      ]);
      $cache = \Drupal::cache('mailchimp');
      $cache
        ->invalidate('campaigns');
      $cache
        ->invalidate('campaign_' . $campaign->mc_data->id);
      return TRUE;
    }
  } catch (\Exception $e) {
    \Drupal::messenger()
      ->addError($e
      ->getMessage());
    \Drupal::logger('mailchimp_campaign')
      ->error('An error occurred while sending to this campaign: {message}', [
      'message' => $e
        ->getMessage(),
    ]);
  }
  return FALSE;
}