コード例 #1
0
/**
 * This function sends an email message based on the supplied email data.
 *
 * @param EmailData $p_email_data
 * @return bool
 */
function email_send($p_email_data)
{
    global $g_phpMailer;
    $t_email_data = $p_email_data;
    $t_recipient = trim($t_email_data->email);
    $t_subject = string_email(trim($t_email_data->subject));
    $t_message = string_email_links(trim($t_email_data->body));
    $t_debug_email = config_get('debug_email');
    $t_mailer_method = config_get('phpMailer_method');
    $t_log_msg = 'ERROR: Message could not be sent - ';
    if (is_null($g_phpMailer)) {
        if ($t_mailer_method == PHPMAILER_METHOD_SMTP) {
            register_shutdown_function('email_smtp_close');
        }
        $mail = new PHPMailer(true);
    } else {
        $mail = $g_phpMailer;
    }
    if (isset($t_email_data->metadata['hostname'])) {
        $mail->Hostname = $t_email_data->metadata['hostname'];
    }
    # @@@ should this be the current language (for the recipient) or the default one (for the user running the command) (thraxisp)
    $t_lang = config_get('default_language');
    if ('auto' == $t_lang) {
        $t_lang = config_get('fallback_language');
    }
    $mail->SetLanguage(lang_get('phpmailer_language', $t_lang));
    # Select the method to send mail
    switch (config_get('phpMailer_method')) {
        case PHPMAILER_METHOD_MAIL:
            $mail->IsMail();
            break;
        case PHPMAILER_METHOD_SENDMAIL:
            $mail->IsSendmail();
            break;
        case PHPMAILER_METHOD_SMTP:
            $mail->IsSMTP();
            // SMTP collection is always kept alive
            $mail->SMTPKeepAlive = true;
            if (!is_blank(config_get('smtp_username'))) {
                # Use SMTP Authentication
                $mail->SMTPAuth = true;
                $mail->Username = config_get('smtp_username');
                $mail->Password = config_get('smtp_password');
            }
            if (!is_blank(config_get('smtp_connection_mode'))) {
                $mail->SMTPSecure = config_get('smtp_connection_mode');
            }
            $mail->Port = config_get('smtp_port');
            break;
    }
    $mail->IsHTML(false);
    # set email format to plain text
    $mail->WordWrap = 80;
    # set word wrap to 50 characters
    $mail->Priority = $t_email_data->metadata['priority'];
    # Urgent = 1, Not Urgent = 5, Disable = 0
    $mail->CharSet = $t_email_data->metadata['charset'];
    $mail->Host = config_get('smtp_host');
    $mail->From = config_get('from_email');
    $mail->Sender = config_get('return_path_email');
    $mail->FromName = config_get('from_name');
    $mail->AddCustomHeader('Auto-Submitted:auto-generated');
    $mail->AddCustomHeader('X-Auto-Response-Suppress: All');
    if (OFF !== $t_debug_email) {
        $t_message = 'To: ' . $t_recipient . "\n\n" . $t_message;
        $t_recipient = $t_debug_email;
    }
    try {
        $mail->AddAddress($t_recipient, '');
    } catch (phpmailerException $e) {
        log_event(LOG_EMAIL, $t_log_msg . $mail->ErrorInfo);
        $t_success = false;
        $mail->ClearAllRecipients();
        $mail->ClearAttachments();
        $mail->ClearReplyTos();
        $mail->ClearCustomHeaders();
        return $t_success;
    }
    $mail->Subject = $t_subject;
    $mail->Body = make_lf_crlf("\n" . $t_message);
    if (isset($t_email_data->metadata['headers']) && is_array($t_email_data->metadata['headers'])) {
        foreach ($t_email_data->metadata['headers'] as $t_key => $t_value) {
            switch ($t_key) {
                case 'Message-ID':
                    /* Note: hostname can never be blank here as we set metadata['hostname']
                       in email_store() where mail gets queued. */
                    if (!strchr($t_value, '@') && !is_blank($mail->Hostname)) {
                        $t_value = $t_value . '@' . $mail->Hostname;
                    }
                    $mail->set('MessageID', "<{$t_value}>");
                    break;
                case 'In-Reply-To':
                    $mail->AddCustomHeader("{$t_key}: <{$t_value}@{$mail->Hostname}>");
                    break;
                default:
                    $mail->AddCustomHeader("{$t_key}: {$t_value}");
                    break;
            }
        }
    }
    try {
        $t_success = $mail->Send();
        if ($t_success) {
            $t_success = true;
            if ($t_email_data->email_id > 0) {
                email_queue_delete($t_email_data->email_id);
            }
        } else {
            # We should never get here, as an exception is thrown after failures
            log_event(LOG_EMAIL, $t_log_msg . $mail->ErrorInfo);
            $t_success = false;
        }
    } catch (phpmailerException $e) {
        log_event(LOG_EMAIL, $t_log_msg . $mail->ErrorInfo);
        $t_success = false;
    }
    $mail->ClearAllRecipients();
    $mail->ClearAttachments();
    $mail->ClearReplyTos();
    $mail->ClearCustomHeaders();
    return $t_success;
}
コード例 #2
0
ファイル: email_queue.php プロジェクト: Kirill/mantisbt
        echo "Sending emails...<br />";
        email_send_all();
        echo "Done";
    } else {
        if ($f_to == 'sendordelall') {
            echo "Sending or deleting emails...<br />";
            email_send_all(true);
            echo "Done";
        } else {
            $t_email_data = email_queue_get((int) $f_to);
            // check if email was found.  This can fail if another request picks up the email first and sends it.
            echo 'Sending email...<br />';
            if ($t_email_data !== false) {
                if (!email_send($t_email_data)) {
                    echo 'Email Not Sent - Deleting from queue<br />';
                    email_queue_delete($t_email_data->email_id);
                } else {
                    echo 'Email Sent<br />';
                }
            } else {
                echo 'Email not found in queue<br />';
            }
        }
    }
}
$t_ids = email_queue_get_ids();
if (count($t_ids) > 0) {
    echo '<table><tr><th>' . lang_get('id') . '</th><th>' . lang_get('email') . '</th><th>' . lang_get('timestamp') . '</th><th>Send Or Delete</th></tr>';
    foreach ($t_ids as $t_id) {
        $row = email_queue_get($t_id);
        echo '<tr><td>' . $row->email_id . '</td><td>' . $row->email . '</td><td>' . date(config_get('complete_date_format'), $row->submitted) . '</td><td>', html_button('email_queue.php', 'Send Or Delete', array('send' => $row->email_id)), '</td></tr>';
コード例 #3
0
function email_send($p_email_data)
{
    global $g_phpMailer_smtp;
    $t_email_data = $p_email_data;
    $t_recipient = trim($t_email_data->email);
    $t_subject = string_email(trim($t_email_data->subject));
    $t_message = string_email_links(trim($t_email_data->body));
    $t_debug_email = config_get('debug_email');
    # Visit http://phpmailer.sourceforge.net
    # if you have problems with phpMailer
    $mail = new PHPMailer();
    $mail->PluginDir = PHPMAILER_PATH;
    if (isset($t_email_data->metadata['hostname'])) {
        $mail->Hostname = $t_email_data->metadata['hostname'];
    }
    # @@@ should this be the current language (for the recipient) or the default one (for the user running the command) (thraxisp)
    $t_lang = config_get('default_language');
    if ('auto' == $t_lang) {
        $t_lang = config_get('fallback_language');
    }
    $mail->SetLanguage(lang_get('phpmailer_language', $t_lang), PHPMAILER_PATH . 'language' . DIRECTORY_SEPARATOR);
    # Select the method to send mail
    switch (config_get('phpMailer_method')) {
        case 0:
            $mail->IsMail();
            break;
        case 1:
            $mail->IsSendmail();
            break;
        case 2:
            $mail->IsSMTP();
            # SMTP collection is always kept alive
            #
            $mail->SMTPKeepAlive = true;
            # @@@ yarick123: It is said in phpMailer comments, that phpMailer::smtp has private access.
            # but there is no common method to reset PHPMailer object, so
            # I see the smallest evel - to initialize only one 'private'
            # field phpMailer::smtp in order to reuse smtp connection.
            if (is_null($g_phpMailer_smtp)) {
                register_shutdown_function('email_smtp_close');
            } else {
                $mail->smtp = $g_phpMailer_smtp;
            }
            break;
    }
    $mail->IsHTML(false);
    # set email format to plain text
    $mail->WordWrap = 80;
    # set word wrap to 50 characters
    $mail->Priority = $t_email_data->metadata['priority'];
    # Urgent = 1, Not Urgent = 5, Disable = 0
    $mail->CharSet = $t_email_data->metadata['charset'];
    $mail->Host = config_get('smtp_host');
    $mail->From = config_get('from_email');
    $mail->Sender = escapeshellcmd(config_get('return_path_email'));
    $mail->FromName = config_get('from_name');
    if (!is_blank(config_get('smtp_username'))) {
        # Use SMTP Authentication
        $mail->SMTPAuth = true;
        $mail->Username = config_get('smtp_username');
        $mail->Password = config_get('smtp_password');
    }
    if (OFF !== $t_debug_email) {
        $t_message = 'To: ' . $t_recipient . "\n\n" . $t_message;
        $mail->AddAddress($t_debug_email, '');
    } else {
        $mail->AddAddress($t_recipient, '');
    }
    $mail->Subject = $t_subject;
    $mail->Body = make_lf_crlf("\n" . $t_message);
    if (isset($t_email_data->metadata['headers']) && is_array($t_email_data->metadata['headers'])) {
        foreach ($t_email_data->metadata['headers'] as $t_key => $t_value) {
            $mail->AddCustomHeader("{$t_key}: {$t_value}");
        }
    }
    if (!$mail->Send()) {
        $t_success = false;
    } else {
        $t_success = true;
        if ($t_email_data->email_id > 0) {
            email_queue_delete($t_email_data->email_id);
        }
    }
    if (!is_null($mail->smtp)) {
        # @@@ yarick123: It is said in phpMailer comments, that phpMailer::smtp has private access.
        # but there is no common method to reset PHPMailer object, so
        # I see the smallest evel - to initialize only one 'private'
        # field phpMailer::smtp in order to reuse smtp connection.
        $g_phpMailer_smtp = $mail->smtp;
    }
    return $t_success;
}