CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Programmatically Adding CiviMail Jobs
Pages: [1]

Author Topic: Programmatically Adding CiviMail Jobs  (Read 895 times)

kurosevic

  • I’m new here
  • *
  • Posts: 25
  • Karma: 1
Programmatically Adding CiviMail Jobs
January 04, 2011, 12:50:57 pm
Hi all,

I'm trying to programmatically add a CiviMail Job on a daily basis using a premade mail template that I've saved in CiviMail.

I believe this would need to be in a module and would need to call a function to create that Job. But I'm pretty confused about how to actually add a new job AND pull that template using only code.

After that's figured out, I suppose i'd just use scheduled cron jobs to get it sent at a specific time each day.

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Programmatically Adding CiviMail Jobs
January 08, 2011, 12:50:08 am
Hi,

Don't think there is an api yet for that, but I like your idea.  Have you investigated how easy it would be?

The params you'd need would be:
- sender info
- subject
- text + html body
- recipients
- schedule date
- flags for tracking open/click & reply

If some are too complicated, might be easier to keep the default values (eg for the flags), whoever needs something more advanced would be more than welcome to patch.

In your case, you'd create a new job with a scheduled date of sending (what is on the last step of the wizard when you do it manually), the cronjob will take care of it.

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

kurosevic

  • I’m new here
  • *
  • Posts: 25
  • Karma: 1
Re: Programmatically Adding CiviMail Jobs
January 08, 2011, 08:10:47 am
what function(s) handle the creation of the mailer and the job now?

the other thing that was just brought to my attention is that civimail was not completing jobs properly when there were too many recipients (3.3.1), and the solution was to break it down into smaller jobs (3.3.2), when exactly does that breaking down process happen? and would creating a job with code interfere with that?

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Programmatically Adding CiviMail Jobs
January 08, 2011, 06:27:51 pm

As mentioned on IRC, Most of the code for the below is in:

CRM/Mailing/BAO/{Mailing,Job}.php
CRM/Mailing/Form/*.php

I think we've done a pretty good job putting most/all of the funcitonality in the BAO's

The child jobs aree created when the job is ready to be delivered (i.e. when the scheduled time is past). This should not affect your code

oob
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

kurosevic

  • I’m new here
  • *
  • Posts: 25
  • Karma: 1
Re: Programmatically Adding CiviMail Jobs
January 10, 2011, 11:20:14 am
After looking into those files, I found something promising, but there is entirely too many referenced variables and functions for me to be sure that it does what I think it does without having a conversation with whoever wrote these functions.

In the BAO Mailing.php file, I found that the functions in the CRM_Mailing_BAO_Mailing class called "add" and "create" seem to be relevant.

Add:
Code: [Select]
/**
     * function to add the mailings
     *
     * @param array $params reference array contains the values submitted by the form
     * @param array $ids    reference array contains the id
     *
     * @access public
     * @static
     * @return object
     */
    static function add( &$params, &$ids )

and Create:
Code: [Select]
/**
     * Construct a new mailing object, along with job and mailing_group
     * objects, from the form values of the create mailing wizard.
     *
     * @params array $params        Form values
     * @return object $mailing      The new mailing object
     * @access public
     * @static
     */
    public static function create( &$params, &$ids )

Additionally, I investigated the Job.php file, also located in BAO, and found that, while there are obviously relevant variables to things like dates, status, types, job limits, and such, it doesn't appear that Job.php really performs the task of actually adding a job to the queue. Instead I would guess that it is happening in mailer, perhaps in create / add, which was mentioned above.

Is any of that true?

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Programmatically Adding CiviMail Jobs
January 10, 2011, 02:16:08 pm

check the foll functions in BAO/Job.php

runJobs_pre: takes a parent job and creates and schedules child jobs

runJobs: queues and delivers the mail from each child job

runJobs_post: determines if all child jobs of a parent job are complete

might be best to have a conversation on IRC with your specific questions

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

kurosevic

  • I’m new here
  • *
  • Posts: 25
  • Karma: 1
Re: Programmatically Adding CiviMail Jobs
January 10, 2011, 02:29:01 pm
What function creates the parent job?

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Programmatically Adding CiviMail Jobs
January 10, 2011, 02:43:19 pm

happens in the postProcess in CRM/Mailing/Form/Schedule.php

ideally we should clean that code up and move it to

CRM/Mailing/BAO/Job.php

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Programmatically Adding CiviMail Jobs

This forum was archived on 2017-11-26.