Esempio n. 1
0
function sendMailMessage($email, $uid, $pwd, $ufn)
{
    global $signupemail_message;
    global $emailsubject, $emailsender;
    global $site_name, $site_start, $site_url;
    $manager_url = $site_url . "manager/";
    $message = sprintf($signupemail_message, $uid, $pwd);
    // use old method
    // replace placeholders
    $message = str_replace("[+uid+]", $uid, $message);
    $message = str_replace("[+pwd+]", $pwd, $message);
    $message = str_replace("[+ufn+]", $ufn, $message);
    $message = str_replace("[+sname+]", $site_name, $message);
    $message = str_replace("[+saddr+]", $emailsender, $message);
    $message = str_replace("[+semail+]", $emailsender, $message);
    $message = str_replace("[+surl+]", $manager_url, $message);
    $headers = "From: " . $emailsender . "\r\n";
    $headers .= "X-Mailer: Content Manager - PHP/" . phpversion();
    $headers .= "\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/plain; charset=utf-8\r\n";
    $headers .= "Content-Transfer-Encoding: quoted-printable\r\n";
    $subject = "=?UTF-8?Q?" . $emailsubject . "?=";
    $message = quoted_printable($message);
    if (ini_get('safe_mode') == FALSE) {
        if (!mail($email, $subject, $message, $headers, "-f {$emailsender}")) {
            webAlert("{$email} - {$_lang['error_sending_email']}");
            exit;
        }
    } elseif (!mail($email, $subject, $message, $headers)) {
        webAlert("{$email} - {$_lang['error_sending_email']}");
        exit;
    }
}
 /** send the message using the prepared information (To:, Subject:, the message and attachments etc.)
  *
  * This actually sends the message (using PHP's mail() command), using all the prepared data & headers etc.
  *
  * Depending on the contents of the message and the number of added attachments, there are the following
  * possibilities.
  *
  *  1. the message is a single plain 7bit ASCII-text with lines shorter than 76 characters, no attachments
  *  2. the message is a text with either 8bit values OR lines longer than 76 characters but still no attachments
  *  3. there are attachments too
  *
  * Ad 1: no need for MIME in that case; simply use the message as-is, using the default
  *  - 7bit encoding and US-ASCII charset. Easy
  *
  * Ad 2: need MIME but not multipart: headers to add:
  *  - MIME-Version: 1.0
  *  - Content-Type: text/plain; charset="UTF-8"
  *  - Content-Transfer-Encoding: quoted-printable
  *
  * Ad 3. need MIME Multipart; headers to add:
  *  - MIME-Version: 1.0
  *  - Content-Type: multipart/mixed; boundary="$boundary"
  *
  * and in the body we need to construct the sequence of message and 1 or more attachments,
  * each with their own headers like
  *  - Content-Type: text/plain; charset="US-ASCII"
  *  - Content-Transfer-Encoding: 7bit
  *    or
  *  - Content-Type: text/plain; charset="UTF-8"
  *  - Content-Transfer-Encoding: quoted-printable
  *
  * and for the attachments (could be more than 1):
  *  - Content-Type: application/octet-stream; name="$name"
  *  - Content-Transfer-Encoding: base64
  *  - Content-Disposition: attachment; filename="$name"
  *
  * and of course with a $boundary between the various parts of the message
  *
  * Note that we listen to the caller most of the time, ie. if the caller specifies an attachment
  * is of type 'application/x-zip', who are we to question that (in {@link add_attachment()}. However,
  * if a message is clearly 7bit ASCII with short lines and of type 'text' (text/plain or text/html or
  * some other text-subtype), we do change the charset to US-ASCII and encoding to 7bit, to make
  * the message just a little bit more readable for the receiver.
  *
  * Finally, we use the mail() command to actually send the message. We add an additional
  * parameter which should instruct sendmail to use the from-address also as the return path
  * (if that is allowed and the webserver is a 'trusted user').
  *
  * @return bool TRUE on success, FALSE on failure (actually the return value of mail())
  * @uses is_7bit()
  * @uses rfc5322_address()
  * @uses rfc2047_qstring()
  * @uses rfc5322_message_id()
  */
 function send()
 {
     $remaining = $this->max_length - strlen('To: ');
     // quirks in mail(): the (single) to: address MUST be on one line!
     $mailto = $this->rfc5322_address($this->mailto['addr'], $remaining, $this->mailto['name'], TRUE, $this->charset, $this->minimal, $this->max_length, '');
     // don't use CR,LF in eol
     $remaining = $this->max_length - strlen('Subject: ');
     $subject = $this->rfc2047_qstring($this->subject, $remaining, $this->charset, $this->minimal, $this->max_length, $this->eol);
     $headers = $this->headers;
     // we always use a copy of $this->headers to 'play' with, keep original 'as-is'
     $remaining = $this->max_length - strlen('From: ');
     $headers['From'] = $this->rfc5322_address($this->mailfrom['addr'], $remaining, $this->mailfrom['name'], FALSE, $this->charset, $this->minimal, $this->max_length, $this->eol);
     if (strcasecmp($this->mailfrom['addr'], $this->mailreplyto['addr']) != 0) {
         $remaining = $this->max_length - strlen('Reply-To: ');
         $headers['Reply-To'] = $this->rfc5322_address($this->mailreplyto['addr'], $remaining, $this->mailreplyto['name'], FALSE, $this->charset, $this->minimal, $this->max_length, $this->eol);
     }
     if (sizeof($this->mailcc) > 0) {
         $remaining = $this->max_length - strlen('Cc: ') - 1;
         //  -1 is for the glue
         $headers['Cc'] = '';
         $glue = '';
         foreach ($this->mailcc as $mailcc) {
             $headers['Cc'] .= $glue . $this->rfc5322_address($mailcc['addr'], $remaining, $mailcc['name'], FALSE, $this->charset, $this->minimal, $this->max_length - 1, $this->eol);
             if ($glue == '') {
                 $glue = ',';
             }
         }
     }
     $headers['X-Mailer'] = sprintf('Website@School Mailer %s (%s, v%s)', WAS_RELEASE, WAS_RELEASE_DATE, WAS_VERSION);
     $headers['Message-ID'] = $this->rfc5322_message_id();
     if (sizeof($this->attachments) < 1) {
         if ($this->is_7bit($this->message['body']) && strcasecmp($this->message['mimetype'], 'text/plain') == 0 && wordwrap($this->message['body'], 78) == $this->message['body']) {
             // and lines no longer than 78 bytes
             //
             // Case 1: 7bit ASCII-text and no attachment
             //
             $body = str_replace(array("\r\n", "\n\r", "\r"), "\n", $this->message['body']);
             $message = str_replace("\n", $this->eol, $body);
             $headers['MIME-Version'] = '1.0';
             $headers['Content-Type'] = 'text/plain; charset="US-ASCII"';
             $headers['Content-Transfer-Encoding'] = '7bit';
         } else {
             //
             // Case 2: message is not plain ASCII text or has long lines: go encode
             //
             $mimetype = $this->message['mimetype'];
             $charset = $this->message['charset'];
             $encoding = $this->message['encoding'];
             $headers['MIME-Version'] = '1.0';
             if (strncasecmp($mimetype, 'text/', 5) == 0) {
                 $headers['Content-Type'] = sprintf('%s; charset="%s"', $mimetype, $charset);
             } else {
                 $headers['Content-Type'] = $mimetype;
             }
             if (strcasecmp($encoding, 'quoted-printable') == 0) {
                 $headers['Content-Transfer-Encoding'] = 'quoted-printable';
                 $message = quoted_printable($this->message['body']);
             } else {
                 $headers['Content-Transfer-Encoding'] = 'base64';
                 $message = wordwrap(base64_encode($message['body']), $this->max_length, $this->eol, TRUE);
             }
         }
     } else {
         //
         // Case 3: a full-fledged MIME-message with attachments and all
         //
         $boundary = sprintf('----=_%s_%d_%d', strftime('%Y%m%d%H%M%S'), intval(getmypid()), get_unique_number());
         $headers['MIME-Version'] = '1.0';
         $headers['Content-Type'] = sprintf('multipart/mixed; boundary="%s"', $boundary);
         $preamble = 'This is a multi=part message in MIME format.';
         $message = $preamble . $this->eol . sprintf('%s--%s%s', $this->eol, $boundary, $this->eol);
         $mimetype = $this->message['mimetype'];
         $charset = $this->message['charset'];
         $encoding = $this->message['encoding'];
         if ($this->is_7bit($this->message['body']) && strncasecmp($mimetype, 'text/', 5) == 0 && wordwrap($this->message['body'], 78) == $this->message['body']) {
             // no lines longer than 78 bytes
             //
             // 3A 1: comparable to case 1: 7bit ASCII body with short lines
             //
             $body = str_replace(array("\r\n", "\n\r", "\r"), "\n", $this->message['body']);
             $charset = 'US-ASCII';
             $encoding = '7bit';
             $message .= sprintf('Content-Type: %s; charset="%s"', $mimetype, $charset) . $this->eol . sprintf('Content-Transfer-Encoding: %s', $encoding) . $this->eol . $this->eol . str_replace("\n", $this->eol, $body);
         } else {
             //
             // 3A 2: comparable to case 2: message is not plain ASCII or has long lines: go encode
             //
             if (strncasecmp($mimetype, 'text/', 5) == 0) {
                 $message .= sprintf('Content-Type: %s; charset="%s"', $mimetype, $charset) . $this->eol;
             } else {
                 $message .= sprintf('Content-Type: %s', $mimetype) . $this->eol;
             }
             if (strcasecmp($encoding, 'quoted-printable') == 0) {
                 $message .= 'Content-Transfer-Encoding: quoted-printable' . $this->eol . $this->eol . quoted_printable($this->message['body']);
             } else {
                 $message .= 'Content-Transfer-Encoding: base64' . $this->eol . $this->eol . wordwrap(base64_encode($message['body']), $this->max_length, $this->eol, TRUE);
             }
         }
         //
         // 3B -- attachments, each with its own properties (we believe the caller)
         //
         foreach ($this->attachments as $attachment) {
             $message .= sprintf('%s--%s%s', $this->eol, $boundary, $this->eol);
             $name = $attachment['name'];
             $mimetype = $attachment['mimetype'];
             $charset = $attachment['charset'];
             $encoding = $attachment['encoding'];
             if (strncasecmp($mimetype, 'text/', 5) == 0) {
                 $message .= sprintf('Content-Type: %s; charset="%s"', $mimetype, $charset) . $this->eol;
             } else {
                 $message .= sprintf('Content-Type: %s; name="%s"', $mimetype, $name) . $this->eol;
             }
             $message .= sprintf('Content-Disposition: attachment; filename="%s"', $name) . $this->eol;
             if (strcasecmp($encoding, 'quoted-printable') == 0) {
                 $message .= 'Content-Transfer-Encoding: quoted-printable' . $this->eol . $this->eol . quoted_printable($attachment['body']);
             } else {
                 $message .= 'Content-Transfer-Encoding: base64' . $this->eol . $this->eol . wordwrap(base64_encode($attachment['body']), $this->max_length, $this->eol, TRUE);
             }
         }
         $message .= sprintf('%s--%s--%s', $this->eol, $boundary, $this->eol);
     }
     // Join all the headers but do NOT add an EOL after the last header
     $mailheaders = '';
     $glue = '';
     foreach ($headers as $k => $v) {
         $mailheaders .= $glue . $k . ': ' . $v;
         if ($glue == '') {
             $glue = $this->eol;
         }
     }
     // finally send the message
     $additional = '-f' . $this->mailfrom['addr'];
     $retval = mail($mailto, $subject, $message, $mailheaders, $additional);
     return $retval;
 }