/** * send the message to all the contacts and also insert a * contact activity in each contacts record * * @param array $contactIds the array of contact ids to send the email * @param string $subject the subject of the message * @param string $message the message contents * @param string $emailAddress use this 'to' email address instead of the default Primary address * * @return array (total, added, notAdded) count of emails sent * @access public * @static */ function sendEmail(&$contactIds, &$subject, &$message, $emailAddress) { $session =& CRM_Core_Session::singleton(); $userID = $session->get('userID'); list($fromDisplayName, $fromEmail, $fromDoNotEmail) = CRM_Contact_BAO_Contact::getContactDetails($userID); if (!$fromEmail) { return array(count($contactIds), 0, count($contactIds)); } if (!trim($fromDisplayName)) { $fromDisplayName = $fromEmail; } $from = CRM_Utils_Mail::encodeAddressHeader($fromDisplayName, $fromEmail); // create the meta level record first $email =& new CRM_Core_BAO_EmailHistory(); $email->subject = $subject; $email->message = $message; $email->contact_id = $userID; $email->sent_date = date('Ymd'); $email->save(); $sent = $notSent = 0; foreach ($contactIds as $contactId) { if (CRM_Core_BAO_EmailHistory::sendMessage($from, $contactId, $subject, $message, $emailAddress, $email->id)) { $sent++; } else { $notSent++; } } return array(count($contactIds), $sent, $notSent); }
function send($from, $toDisplayName, $toEmail, $subject, $message, $cc = null, $bcc = null) { require_once 'CRM/Core/DAO/Domain.php'; $dao = new CRM_Core_DAO_Domain(); $dao->id = 1; $dao->find(true); $returnPath = $dao->email_return_path; if (!$returnPath) { $returnPath = CRM_Utils_Mail::_pluckEmailFromHeader($from); } $headers = array(); $headers['From'] = $from; $headers['To'] = CRM_Utils_Mail::encodeAddressHeader($toDisplayName, $toEmail); $headers['Cc'] = $cc; $headers['Bcc'] = $bcc; $headers['Subject'] = CRM_Utils_Mail::encodeSubjectHeader($subject); $headers['Content-Type'] = 'text/plain; charset=utf-8'; $headers['Content-Disposition'] = 'inline'; $headers['Content-Transfer-Encoding'] = '8bit'; $headers['Return-Path'] = $returnPath; $headers['Reply-To'] = $from; $to = array($toEmail); if ($cc) { $to[] = $cc; } if ($bcc) { $to[] = $bcc; } $mailer =& CRM_Core_Config::getMailer(); if ($mailer->send($to, $headers, $message) !== true) { return false; } return true; }