Pommo_Mailing::add PHP Method

add() public method

* add Adds a mailing to the database
public add ( &$in )
    function add(&$in)
    {
        $dbo = Pommo::$_dbo;
        // set the start time if not provided
        if (empty($in['start'])) {
            $in['start'] = time();
        }
        if (empty($in['sent'])) {
            $in['sent'] = 0;
        }
        if (!Pommo_Mailing::validate($in)) {
            return false;
        }
        //	Add image to track views
        if (1 == $in['track']) {
            $in['body'] .= '<img src="http://' . $_SERVER['SERVER_NAME'] . Pommo::$_baseUrl . 'track-[[!mailing_id]]-[[!subscriber_id]]' . '.png">';
        }
        $query = "INSERT INTO " . $dbo->table['mailings'] . "\n                SET\n                [fromname='%S',]\n                [fromemail='%S',]\n                [frombounce='%S',]\n                [subject='%S',]\n                [body='%S',]\n                [altbody='%S',]\n                [ishtml='%S',]\n                [mailgroup='%S',]\n                [subscriberCount=%I,]\n                [finished=FROM_UNIXTIME(%I),]\n                [sent=%I,]\n                [charset='%S',]\n                [status=%I,]\n                [track=%I,]\n                started=FROM_UNIXTIME(%i)";
        $query = $dbo->prepare($query, @array($in['fromname'], $in['fromemail'], $in['frombounce'], $in['subject'], $in['body'], $in['altbody'], $in['ishtml'], $in['group'], $in['tally'], $in['end'], $in['sent'], $in['charset'], $in['status'], $in['track'], $in['start']));
        // fetch new mailing_id
        $id = $dbo->lastId($query);
        if (!$id) {
            return false;
        }
        // Save the attachments
        if ($in['attachments']) {
            $attach = explode(',', $in['attachments']);
            foreach ($attach as $key => $attachment) {
                $query = "INSERT INTO " . $dbo->table['mailings_attachments'] . "\n                        SET\n                        [mailing_id='%I',]\n                        [file_id='%I']";
                $query = $dbo->prepare($query, @array($id, $attachment));
                $dbo->query($query);
            }
        }
        // insert current if applicable
        if (!empty($in['status']) && $in['status'] == 1) {
            if (empty($in['code'])) {
                $in['code'] = Pommo_Helper::makeCode();
            }
            $query = "INSERT INTO " . $dbo->table['mailing_current'] . "\n            SET\n            [command='%S',]\n            [serial=%I,]\n            [securityCode='%S',]\n            [current_status='%S',]\n            current_id=%i";
            $query = $dbo->prepare($query, @array($in['command'], $in['serial'], $in['code'], $in['current_status'], $id));
            if (!$dbo->query($query)) {
                return false;
            }
            return $in['code'];
        }
        return $id;
    }

Usage Example

Example #1
0
     } else {
         // temp subscriber created
         $state['tally'] = 1;
         $state['group'] = Pommo::_T('Test Mailing');
         if ($state['ishtml'] == 'off') {
             $state['body'] = $state['altbody'];
             $state['altbody'] = '';
         }
         // create mailing
         $mailing = Pommo_Mailing::make(array(), TRUE);
         $state['status'] = 1;
         $state['current_status'] = 'stopped';
         $state['command'] = 'restart';
         $state['charset'] = $state['list_charset'];
         $mailing = Pommo_Helper::arrayIntersect($state, $mailing);
         $code = Pommo_Mailing::add($mailing);
         // populate queue
         $queue = array($key);
         if (!Pommo_Mail_Ctl::queueMake($queue)) {
             $logger->addErr('Unable to Populate Queue');
         } else {
             if (!Pommo_Mail_Ctl::spawn(Pommo::$_baseUrl . 'ajax/mailings_send4.php?test=TRUE&code=' . $code)) {
                 $logger->addErr('Unable to spawn background mailer');
             } else {
                 $smarty->assign('sent', $_POST['email']);
             }
         }
     }
 } elseif ($current) {
     $logger->addMsg(Pommo::_T('A mailing is currently taking place. Please try again later.'));
     $smarty->assign($_POST);