Answers.Body: <p>Check your user permissions. All too often something that works for me via the GUI, fails in the automation.  </p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-12T19:10:59.260Z
Answers.Text: Thank you! I initially thought it was permissions. Is this symptom (0 intended recipients) possibly caused by lack of permissions? Which permissions should I look for?
Answers.UserDisplayName: LunkRat
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-12T20:16:46.323Z
Answers.Text: Permission to access the contacts as the user authenticated in the request where node is published. If the node is being published eg from `node/add` by a logged-in editor, their CiviCRM permissions should apply. If from a logged-out request, including cron when fired as anonymous user or a Drush execution without the `-u` (`--user`) flag, then expect no contact access. You can switch users easily enough ([safely impersonating another user](https://www.drupal.org/node/218104)).
Answers.UserDisplayName: Chris Burgess
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-16T16:44:45.887Z
Answers.Text: Thanks for the comments, it was helpful. You were correct that it was Rules Scheduler executing the component code without access to contacts. Cheers.
Answers.UserDisplayName: LunkRat
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-06-12T19:09:03.630Z
Answers.LastActivityDate: 2015-06-12T19:09:03.630Z
Answers.UserDisplayName: DaveD
Body: <p>I'm creating some CiviMail mailings using the API. Sometimes my mailings get created and scheduled, but when they execute, they are marked as "completed" but show as having <code>Intended Recipients: 0</code> in the mailing report, even though the recipient group is correctly populated:</p>
  
  <p><img src="https://i.sstatic.net/GE0RJ.png" alt="CiviMail mailing report"></p>
  
  <p>When I create the exact mailing manually through the GUI, I get the proper intended recipients and the email is sent. </p>
  
  <p>The code I'm using can be found in my <a href="https://github.com/LunkRat/civimail_tools" rel="nofollow noreferrer">CiviMail Tools</a> Drupal module that provides rules integration for CiviMail using the CiviCRM API. </p>
  
  <p>So, I have a rule component set up that creates a CiviMail mailing when a node is published. If I execute or schedule (rules scheduler) the rule component manually, the mailing gets intended recipients. However, if the component is scheduled automatically by an event trigger, then the CiviMail mailing is created and scheduled, but has <code>Intended Recipients: 0</code> as in the above screenshot. </p>
  
  <p>Any idea what might cause this? Again, it only happens when Drupal schedules my rule component based on an event trigger. If I schedule it manually, it all works. Very strange and difficult to troubleshoot!</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-06-12T02:44:19.947Z
Comments.Text: Recommend posting your code for mailing creation via API and your CivICRM version as part of the question.
Comments.UserDisplayName: Chris Burgess
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-06-12T15:06:08.583Z
Comments.Text: Thanks @ChrisBurgess I've added a link to my code, found here: https://github.com/LunkRat/civimail_tools and added more background on the issue.
Comments.UserDisplayName: LunkRat
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-06-15T16:32:31.093Z
Comments.Text: @ChrisBurgess can you offer any insight into why my rules code works when I execute the component manually but not when scheduled via Rules Scheduler? Any suggestions welcome. Thank you.
Comments.UserDisplayName: LunkRat
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-06-15T19:33:53.987Z
Comments.Text: When you execute manually, you're logged in? And when Rules Scheduler is called from cron current user is anonymous? If so this behaviour is what you'd expect. Suggest checking current user when your linked code is called to see.
Comments.UserDisplayName: Chris Burgess
Tags: <civimail><api>
Title: CiviMail mailing sometimes has 0 intended recipients
ThreadText: # CiviMail mailing sometimes has 0 intended recipients
  
  (Asked by: username_0 on 2015-06-11)
  
  I'm creating some CiviMail mailings using the API. Sometimes my mailings get created and scheduled, but when they execute, they are marked as "completed" but show as having `Intended Recipients: 0` in the mailing report, even though the recipient group is correctly populated:
  
  ![CiviMail mailing report](https://i.sstatic.net/GE0RJ.png)
  
  When I create the exact mailing manually through the GUI, I get the proper intended recipients and the email is sent.
  
  The code I'm using can be found in my [CiviMail Tools](https://github.com/username_0/civimail_tools) Drupal module that provides rules integration for CiviMail using the CiviCRM API.
  
  So, I have a rule component set up that creates a CiviMail mailing when a node is published. If I execute or schedule (rules scheduler) the rule component manually, the mailing gets intended recipients. However, if the component is scheduled automatically by an event trigger, then the CiviMail mailing is created and scheduled, but has `Intended Recipients: 0` as in the above screenshot.
  
  Any idea what might cause this? Again, it only happens when Drupal schedules my rule component based on an event trigger. If I schedule it manually, it all works. Very strange and difficult to troubleshoot!
  
  **Tags:** civimail, api
  
  > **Comments:**
  > **username_1**: Recommend posting your code for mailing creation via API and your CivICRM version as part of the question.  
  > **username_0**: Thanks @ChrisBurgess I've added a link to my code, found here: https://github.com/username_0/civimail_tools and added more background on the issue.  
  > **username_0**: @ChrisBurgess can you offer any insight into why my rules code works when I execute the component manually but not when scheduled via Rules Scheduler? Any suggestions welcome. Thank you.  
  > **username_1**: When you execute manually, you're logged in? And when Rules Scheduler is called from cron current user is anonymous? If so this behaviour is what you'd expect. Suggest checking current user when your linked code is called to see.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2015-06-12. Score: 3 | Accepted answer
  
  Check your user permissions. All too often something that works for me via the GUI, fails in the automation.
  
  > **Comments:**
  > **username_0**: Thank you! I initially thought it was permissions. Is this symptom (0 intended recipients) possibly caused by lack of permissions? Which permissions should I look for?  
  > **username_1**: Permission to access the contacts as the user authenticated in the request where node is published. If the node is being published eg from `node/add` by a logged-in editor, their CiviCRM permissions should apply. If from a logged-out request, including cron when fired as anonymous user or a Drush execution without the `-u` (`--user`) flag, then expect no contact access. You can switch users easily enough ([safely impersonating another user](https://www.drupal.org/node/218104)).  
  > **username_0**: Thanks for the comments, it was helpful. You were correct that it was Rules Scheduler executing the component code without access to contacts. Cheers.  
  
  ---
  
  