Example #1
0
 private function _send_by_mail(org_openpsa_invoices_invoice_dba $invoice)
 {
     $customerCard = org_openpsa_widgets_contact::get($invoice->customerContact);
     $contactDetails = $customerCard->contact_details;
     $invoice_label = $invoice->get_label();
     $invoice_date = date($this->_l10n_midcom->get('short date'), $invoice->date);
     // generate pdf, only if not existing yet
     $pdf_files = org_openpsa_helpers::get_attachment_urls($invoice, "pdf_file");
     if (count($pdf_files) == 0) {
         org_openpsa_invoices_handler_pdf::render_and_attach_pdf($invoice);
     }
     $mail = new org_openpsa_mail();
     // define replacements for subject / body
     $mail->parameters = array("INVOICE_LABEL" => $invoice_label, "INVOICE_DATE" => $invoice_date, "FIRSTNAME" => $contactDetails["firstname"], "LASTNAME" => $contactDetails["lastname"]);
     $mail->to = $contactDetails["email"];
     $mail->from = $this->_config->get('invoice_mail_from_address');
     $mail->subject = $this->_config->get('invoice_mail_title');
     $mail->body = $this->_config->get('invoice_mail_body');
     // attach pdf to mail
     if ($mail->can_attach()) {
         $pdf_files = org_openpsa_helpers::get_attachment_urls($invoice, "pdf_file");
         if (count($pdf_files) > 0) {
             foreach ($pdf_files as $guid => $url) {
                 $att = array();
                 $att['name'] = basename($url) . ".pdf";
                 $att['mimetype'] = "application/pdf";
                 $fp = fopen($url, "r");
                 if (!$fp) {
                     //Failed to open attachment for reading, skip the file
                     continue;
                 }
                 $att['content'] = '';
                 while (!feof($fp)) {
                     $att['content'] .= fread($fp, 4096);
                 }
                 fclose($fp);
                 debug_add("adding attachment '{$att['name']}' to attachments array of invoice mail");
                 $mail->attachments[] = $att;
                 unset($att);
             }
         }
     }
     if (!$mail->send()) {
         $this->_request_data['message']['message'] = sprintf($this->_l10n->get('unable to deliver mail: %s'), $mail->get_error_message());
         return false;
     } else {
         $invoice->set_parameter($this->_component, 'sent_by_mail', time());
         return $this->_mark_as_sent($invoice);
     }
 }
Example #2
0
 private function _send_email($httpcode, $msg)
 {
     if (!isset($GLOBALS['midcom_config']['error_actions'][$httpcode]['email']) || empty($GLOBALS['midcom_config']['error_actions'][$httpcode]['email'])) {
         // No recipient specified, skip
         return;
     }
     if (!midcom::get('componentloader')->is_installed('org.openpsa.mail')) {
         debug_add("Email sending library org.openpsa.mail, used for error notifications is not installed", MIDCOM_LOG_WARN);
         return;
     }
     $mail = new org_openpsa_mail();
     $mail->to = $GLOBALS['midcom_config']['error_actions'][$httpcode]['email'];
     $mail->from = "\"MidCOM error notifier\" <webmaster@{$_SERVER['SERVER_NAME']}>";
     $mail->subject = "[{$_SERVER['SERVER_NAME']}] {$msg}";
     $mail->body = "{$_SERVER['SERVER_NAME']}:\n{$msg}";
     $stacktrace = $this->get_function_stack();
     $mail->body .= "\n" . implode("\n", $stacktrace);
     if (!$mail->send()) {
         debug_add("failed to send error notification email to {$mail->to}, reason: " . $mail->get_error_message(), MIDCOM_LOG_WARN);
     }
 }
Example #3
0
 /**
  * Internal helper for processing the batch change of passwords
  */
 private function _process_batch_change()
 {
     // Set the mail commo parts
     $mail = new org_openpsa_mail();
     $mail->from = $this->_config->get('message_sender');
     $mail->encoding = 'UTF-8';
     // Success switch
     $success = true;
     // Get the context prefix
     $prefix = midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ANCHORPREFIX);
     // Change every user or continue to next on failure - failures will show UI messages
     foreach ($_POST['midcom_admin_user'] as $id) {
         try {
             $person = new midcom_db_person($id);
         } catch (midcom_error $e) {
             midcom::get('uimessages')->add($this->_l10n->get('midcom.admin.user'), sprintf($this->_l10n->get('failed to get the user with id %s'), $id), 'error');
             $success = false;
             continue;
         }
         // This shortcut is used in case of errors
         $person_edit_url = "<a href=\"{$prefix}__mfa/asgard_midcom.admin.user/edit/{$person->guid}\">{$person->name}</a>";
         // Cannot send the email if address is not specified
         if (!$person->email) {
             midcom::get('uimessages')->add($this->_l10n->get('midcom.admin.user'), sprintf($this->_l10n->get('no email address defined for %s'), $person_edit_url), 'error');
             continue;
         }
         // Recipient
         $mail->to = $person->email;
         // Store the old password
         $person->set_parameter('midcom.admin.user', 'old_password', $person->password);
         // Get a new password
         $password = midcom_admin_user_plugin::generate_password(8);
         $mail->body = $_POST['body'];
         $mail->subject = $_POST['subject'];
         $mail->parameters = array('PASSWORD' => $password, 'FROM' => $this->_config->get('message_sender'), 'LONGDATE' => strftime('%c'), 'SHORTDATE' => strftime('%x'), 'TIME' => strftime('%X'), 'PERSON' => $person);
         // Send the message
         if ($mail->send()) {
             // Set the password
             $person->password = "******";
             if (!$person->update()) {
                 midcom::get('uimessages')->add($this->_l10n->get('midcom.admin.user'), sprintf($this->_l10n->get('failed to update the password for %s'), $person_edit_url));
                 $success = false;
             }
         } else {
             throw new midcom_error("Failed to send the mail, SMTP returned error " . $mail->get_error_message());
         }
     }
     // Show UI message on success
     if ($success) {
         midcom::get('uimessages')->add($this->_l10n->get('midcom.admin.user'), $this->_l10n->get('passwords updated and mail sent'));
     }
 }
Example #4
0
            // This is a new person
            $persons_new[$email] = $user;
        }
    }
    echo "<p>" . count($persons_found) . " users are already in database and " . count($persons_new) . " are new</p>\n";
    foreach ($persons_new as $email => $name) {
        $mail = new org_openpsa_mail();
        $mail->to = $email;
        $mail->from = $_POST['from'];
        $mail->subject = $_POST['subject'];
        $mail->body = $_POST['message'];
        // Make replacements to body
        $mail->parameters = array('USER' => $name);
        $ret = $mail->send();
        if (!$ret) {
            echo "<p>failed to send notification email to {$mail->to}, reason: " . $mail->get_error_message() . "</p>\n";
        } else {
            echo "<p>Sent notification to {$name} ({$email})</p>\n";
        }
    }
} else {
    ?>
    <form method="post">
        <label>
            <span>Directory path for the user files</span>
            <input type="text" name="directory" />
        </label>
        <label>
            <span style="display: block;">Message From address</span>
            <input type="text" name="from" value="" />
        </label>
Example #5
0
 /**
  * can be called by various handlers
  *
  * @param string password: leave blank for auto generated
  */
 public function create_account($person_guid, $username, $usermail, $password = "", $send_welcome_mail = false, $auto_relocate = true)
 {
     //quick validation
     if (empty($person_guid)) {
         $this->errstr = "cannot identify user: no guid given";
         return false;
     }
     if (empty($username)) {
         $this->errstr = "cannot create account: no username given";
         return false;
     }
     if ($send_welcome_mail && empty($usermail)) {
         $this->errstr = "cannot deliver welcome mail: no usermail adress given";
         return false;
     }
     // Check if we get the person
     $this->_person = new midcom_db_person($person_guid);
     $this->_person->require_do('midgard:update');
     //need to generate password?
     if (empty($password)) {
         $generated_password = true;
         $password = $this->generate_safe_password($this->_config->get("min_password_length"));
     } else {
         $generated_password = false;
     }
     $this->_account = new midcom_core_account($this->_person);
     //an account already existing?
     if ($this->_account->get_password()) {
         $this->errstr = "Creating new account for existing account is not possible";
         return false;
     }
     //try creating
     $success = $this->set_account($username, $password);
     if (!$success) {
         $this->errstr = "couldnt set account, reason: " . $this->errstr;
         return false;
     }
     //send welcome mail?
     if ($send_welcome_mail) {
         $mail = new org_openpsa_mail();
         $mail->to = $usermail;
         $mail->from = $this->_config->get('welcome_mail_from_address');
         $mail->subject = $this->_config->get('welcome_mail_title');
         $mail->body = $this->_config->get('welcome_mail_body');
         // Make replacements to body
         $mail->parameters = array("USERNAME" => $username, "PASSWORD" => $password);
         if (!$mail->send()) {
             $this->errstr = "Unable to deliver welcome mail: " . $mail->get_error_message();
             return false;
         }
     } else {
         /*
          * no welcome mail was sent:
          * if the password was auto generated show it in an ui message
          */
         if ($generated_password) {
             midcom::get('uimessages')->add($this->_l10n->get('org.openpsa.user'), sprintf($this->_l10n->get("account_creation_success"), $username, $password), 'ok');
         }
     }
     if ($auto_relocate) {
         // Relocate to group view
         $prefix = midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ANCHORPREFIX);
         midcom::get()->relocate("{$prefix}view/{$this->_person->guid}/");
         // This will exit
     } else {
         if (!empty($this->errstr)) {
             throw new midcom_error('Could not create account: ' . $this->errstr);
         }
         return true;
     }
 }
Example #6
0
        <p>
            Backend: <select name="backend">
                        <option value="try_default">Component default(s)</option>
                        <option value="mail_smtp">PEAR Mail/SMTP</option>
                        <option value="mail_sendmail">PEAR Mail/Sendmail</option>
                        <option value="mail">PHP mail()</option>
                     </select><br/>
            From: <input name="from" size=50 type="text" value="*****@*****.**"/><br/>
            To: <input name="to" size=50 type="text" value="*****@*****.**" /><br/>
            Subject: <input name="subject" size=50 type="text" value="Testing o.o.mail with special chars (ÄäÖöÅå€)"/><br/>
            Message:<br/>
            <textarea rows=40 cols=80 name="body">Test body with special chars (Ää Öö Åå €)

/Test person</textarea><br/>
            <input type="submit" value="Send" />
        </p>
    </form>
<?php 
} else {
    $mail = new org_openpsa_mail($_POST['backend']);
    $mail->subject = $_POST['subject'];
    $mail->body = $_POST['body'];
    $mail->to = $_POST['to'];
    $mail->from = $_POST['from'];
    $ret = $mail->send();
    echo "mail->send returned {$ret}<br>\n";
    if (!$ret) {
        echo $mail->get_error_message();
    }
}
echo "</p>\n";
Example #7
0
 private function _send_email_member($member, &$subject, &$content, &$from, &$data_array)
 {
     if ($this->_check_member_deny($member, 'email')) {
         return;
     }
     if (!isset($GLOBALS['org_openpsa_directmarketing_campaign_message_send_i'])) {
         $GLOBALS['org_openpsa_directmarketing_campaign_message_send_i'] = 0;
     }
     $GLOBALS['org_openpsa_directmarketing_campaign_message_send_i']++;
     $token = $this->_create_email_token();
     $person =& org_openpsa_contacts_person_dba::get_cached($member->person);
     debug_add("Called for member #{$member->id}, (person #{$person->id}: {$person->rname})");
     //Make sure we have some backend and parameters for it defined
     if (!isset($data_array['mail_send_backend'])) {
         $data_array['mail_send_backend'] = 'try_default';
     }
     if (!isset($data_array['mail_send_backend_params'])) {
         $data_array['mail_send_backend_params'] = array();
     }
     //Check for bounce detector usage
     if (array_key_exists('bounce_detector_address', $data_array) && !empty($data_array['bounce_detector_address'])) {
         $bounce_address = str_replace('TOKEN', $token, $data_array['bounce_detector_address']);
         $mail->headers['Return-Path'] = $bounce_address;
         //Force bouncer as backend if default specified
         if (!array_key_exists('mail_send_backend', $data_array) || empty($data_array['mail_send_backend']) || $data_array['mail_send_backend'] == 'try_default') {
             $data_array['mail_send_backend'] = 'bouncer';
         }
     }
     $mail = new org_openpsa_mail($data_array['mail_send_backend'], $data_array['mail_send_backend_params']);
     $mail->to = $person->email;
     $subject = $member->personalize_message($subject, self::EMAIL_TEXT, $person);
     if ($this->test_mode) {
         $mail->subject = "[TEST] {$subject}";
     } else {
         $mail->subject = $subject;
     }
     $mail->from = $from;
     if (isset($data_array['reply-to'])) {
         $mail->headers['Reply-To'] = $data_array['reply-to'];
     }
     //Set some List-xxx headers to avoid auto-replies and in general to be a good netizen
     $mail->headers['List-Id'] = "<{$this->guid}@{$_SERVER['SERVER_NAME']}>";
     $mail->headers['List-Unsubscribe'] = '<' . $member->get_unsubscribe_url(false, $person) . '>';
     debug_add('mail->from: ' . $mail->from . ', mail->to: ' . $mail->to . ', mail->subject: ' . $mail->subject);
     switch ($this->orgOpenpsaObtype) {
         case self::EMAIL_TEXT:
             $mail->body = $member->personalize_message($content, $this->orgOpenpsaObtype, $person);
             break;
         case self::EMAIL_HTML:
             $mail->html_body = $member->personalize_message($content, $this->orgOpenpsaObtype, $person);
             if (array_key_exists('htmlemail_force_text_body', $data_array) && strlen($data_array['htmlemail_force_text_body']) > 0) {
                 $mail->body = $member->personalize_message($data_array['htmlemail_force_text_body'], $this->orgOpenpsaObtype, $person);
             }
             // Allow sensing only HTML body if requested
             if (array_key_exists('htmlemail_onlyhtml', $data_array) && !empty($data_array['htmlemail_onlyhtml'])) {
                 $mail->allow_only_html = true;
             }
             // Skip embedding if requested
             if (array_key_exists('htmlemail_donotembed', $data_array) && !empty($data_array['htmlemail_donotembed'])) {
                 // Skip embedding, do something else ??
             } else {
                 //The mail class uses a caching scheme to avoid fetching embedded objects again.
                 $mail->embed_images($this);
             }
             //Handle link detection
             if (array_key_exists('link_detector_address', $data_array) && !empty($data_array['link_detector_address'])) {
                 $link_address = str_replace('TOKEN', $token, $data_array['link_detector_address']);
                 $mail->html_body = $this->_insert_link_detector($mail->html_body, $link_address);
             }
             break;
         default:
             debug_add('Invalid message type, aborting', MIDCOM_LOG_ERROR);
             return array(false, $mail);
     }
     //Go trough DM2 types array for attachments
     reset($data_array['dm_types']);
     foreach ($data_array['dm_types'] as $field => $typedata) {
         if (empty($typedata->attachments_info)) {
             continue;
         }
         // If you don't want to add the image as an attachment to the field, add show_attachment customdata-definition to
         // schema and set it to false
         if (isset($typedata->storage->_schema->fields[$field]) && is_array($typedata->storage->_schema->fields[$field]) && isset($typedata->storage->_schema->fields[$field]['customdata']) && is_array($typedata->storage->_schema->fields[$field]['customdata']) && isset($typedata->storage->_schema->fields[$field]['customdata']['show_attachment']) && $typedata->storage->_schema->fields[$field]['customdata']['show_attachment'] === false) {
             continue;
         }
         foreach ($typedata->attachments_info as $attachment_data) {
             $att = array();
             $att['name'] = $attachment_data['filename'];
             $att['mimetype'] = $attachment_data['mimetype'];
             $fp = $attachment_data['object']->open('r');
             if (!$fp) {
                 //Failed to open attachment for reading, skip the file
                 continue;
             }
             $att['content'] = '';
             /* PONDER: Should we cache the content somehow so that we only need to read it once per request ??
                We would save some file opens at the expense of keeping the contents in memory (potentially very expensive) */
             while (!feof($fp)) {
                 $att['content'] .= fread($fp, 4096);
             }
             fclose($fp);
             debug_add("adding attachment '{$att['name']}' from field '{$field}' to attachments array");
             $mail->attachments[] = $att;
             unset($att);
         }
     }
     $status = $mail->send();
     if ($status) {
         debug_add('Mail sent to: ' . $mail->to);
         if (!$this->test_mode) {
             $member->create_receipt($this->id, org_openpsa_directmarketing_campaign_messagereceipt_dba::SENT, $token);
         }
     } else {
         $message = sprintf('FAILED to send mail to: %s, reason: %s', $mail->to, $mail->get_error_message());
         debug_add($message, MIDCOM_LOG_WARN);
         if (!$this->test_mode) {
             $params = array(array('domain' => 'org.openpsa.directmarketing', 'name' => 'send_error_message', 'value' => $message));
             $member->create_receipt($this->id, ORG_OPENPSA_MESSAGERECEIPT_FAILURE, $token, $params);
         }
         if ($this->send_output) {
             midcom::get('uimessages')->add(midcom::get('i18n')->get_string('org.openpsa.directmarketing', 'org.openpsa.directmarketing'), sprintf(midcom::get('i18n')->get_string('FAILED to send mail to: %s, reason: %s', 'org.openpsa.directmarketing'), $mail->to, $mail->get_error_message()), 'error');
         }
     }
     unset($mail);
     return $status;
 }
Example #8
0
 /**
  * Sends the 'content' version of the message as email to subscribers via org.openpsa.mail
  */
 public function send_email($message)
 {
     if (empty($this->recipient->email)) {
         return false;
     }
     $mail = new org_openpsa_mail();
     $mail->to = $this->recipient->email;
     $growl_to = $mail->to;
     if (array_key_exists('growl_to', $message)) {
         $growl_to = $message['growl_to'];
         unset($message['growl_to']);
     }
     $sender = null;
     if (array_key_exists('from', $message) && !empty($message['from'])) {
         midcom::get('auth')->request_sudo();
         $user = midcom::get('auth')->get_user($message['from']);
         $sender =& $user->get_storage();
         midcom::get('auth')->drop_sudo();
         // Avoid double dump
         unset($message['from']);
     }
     $default_sender = $this->_config->get('default_sender');
     if (!is_null($sender) && $sender->email) {
         $mail->from = '"' . $sender->name . '" <' . $sender->email . '>';
     } else {
         if (!is_null($default_sender) && !empty($default_sender)) {
             $mail->from = $default_sender;
         } else {
             $mail->from = '"OpenPSA Notifier" <noreply@' . $_SERVER['SERVER_NAME'] . '>';
         }
     }
     if (array_key_exists('title', $message)) {
         // PHP5-TODO: Must be copy by value
         $mail->subject = $message['title'];
         // Avoid double dump
         unset($message['title']);
     } else {
         $mail->subject = 'org.openpsa.notifications message (no title provided)';
     }
     if (array_key_exists('attachments', $message)) {
         // PHP5-TODO: Must be copy by value
         $mail->attachments = $message['attachments'];
         // Do not dump attachments as content
         unset($message['attachments']);
     }
     if (array_key_exists('content', $message)) {
         $mail->body = $message['content'];
     } else {
         // No explicit content defined, dump all keys
         foreach ($message as $key => $value) {
             // TODO (nice-to-have): RFC "fold" the value
             $mail->body .= "{$key}: {$value}\n";
         }
     }
     $ret = $mail->send();
     if (!$ret) {
         debug_add("failed to send notification email to {$mail->to}, reason: " . $mail->get_error_message(), MIDCOM_LOG_WARN);
     } else {
         midcom::get('uimessages')->add(midcom::get('i18n')->get_string('org.openpsa.notifications', 'org.openpsa.notifications'), sprintf(midcom::get('i18n')->get_string('notification sent to %s', 'org.openpsa.notifications'), $growl_to));
     }
     return $ret;
 }