Esempio n. 1
0
function plaintextEmail($to, $from, $subject, $plainContent, $attachedFiles, $customheaders = false)
{
    // Not ensurely where this is supposed to be set, but defined it false for now to remove php notices
    $plainEncoding = false;
    if ($customheaders && is_array($customheaders) == false) {
        echo "htmlEmail({$to}, {$from}, {$subject}, ...) could not send mail: improper \$customheaders passed:<BR>";
        dieprintr($customheaders);
    }
    // If the subject line contains extended characters, we must encode it
    $subject = Convert::xml2raw($subject);
    $subject = "=?UTF-8?B?" . base64_encode($subject) . "?=";
    // Make the plain text part
    $headers["Content-Type"] = "text/plain; charset=utf-8";
    $headers["Content-Transfer-Encoding"] = $plainEncoding ? $plainEncoding : "quoted-printable";
    $plainContent = $plainEncoding == "base64" ? chunk_split(base64_encode($plainContent), 60) : QuotedPrintable_encode($plainContent);
    // Messages with attachments are handled differently
    if ($attachedFiles) {
        // The first part is the message itself
        $fullMessage = processHeaders($headers, $plainContent);
        $messageParts = array($fullMessage);
        // Include any specified attachments as additional parts
        foreach ($attachedFiles as $file) {
            if (isset($file['tmp_name']) && isset($file['name'])) {
                $messageParts[] = encodeFileForEmail($file['tmp_name'], $file['name']);
            } else {
                $messageParts[] = encodeFileForEmail($file);
            }
        }
        // We further wrap all of this into another multipart block
        list($fullBody, $headers) = encodeMultipart($messageParts, "multipart/mixed");
        // Messages without attachments do not require such treatment
    } else {
        $fullBody = $plainContent;
    }
    // Email headers
    $headers["From"] = validEmailAddr($from);
    // Messages with the X-SilverStripeMessageID header can be tracked
    if (isset($customheaders["X-SilverStripeMessageID"]) && defined('BOUNCE_EMAIL')) {
        $bounceAddress = BOUNCE_EMAIL;
        // Get the human name from the from address, if there is one
        if (preg_match('/^([^<>]+)<([^<>])> *$/', $from, $parts)) {
            $bounceAddress = "{$parts['1']}<{$bounceAddress}>";
        }
    } else {
        $bounceAddress = $from;
    }
    // $headers["Sender"] 		= $from;
    $headers["X-Mailer"] = X_MAILER;
    if (!isset($customheaders["X-Priority"])) {
        $headers["X-Priority"] = 3;
    }
    $headers = array_merge((array) $headers, (array) $customheaders);
    // the carbon copy header has to be 'Cc', not 'CC' or 'cc' -- ensure this.
    if (isset($headers['CC'])) {
        $headers['Cc'] = $headers['CC'];
        unset($headers['CC']);
    }
    if (isset($headers['cc'])) {
        $headers['Cc'] = $headers['cc'];
        unset($headers['cc']);
    }
    // Send the email
    $headers = processHeaders($headers);
    $to = validEmailAddr($to);
    // Try it without the -f option if it fails
    if (!($result = @mail($to, $subject, $fullBody, $headers, "-f{$bounceAddress}"))) {
        $result = mail($to, $subject, $fullBody, $headers);
    }
    if ($result) {
        return array($to, $subject, $fullBody, $headers);
    }
    return false;
}
Esempio n. 2
0
 protected function buildBasicMail($to, $from, $subject)
 {
     if (preg_match('/(\'|")(.*?)\\1[ ]+<[ ]*(.*?)[ ]*>/', $from, $from_splitted)) {
         // If $from countain a name, e.g. "My Name" <me@acme.com>
         $this->mailer->SetFrom($from_splitted[3], $from_splitted[2]);
     } else {
         $this->mailer->SetFrom($from);
     }
     $to = validEmailAddr($to);
     $this->mailer->ClearAddresses();
     $this->mailer->AddAddress($to, ucfirst(substr($to, 0, strpos($to, '@'))));
     // For the recipient's name, the string before the @ from the e-mail address is used
     $this->mailer->Subject = $subject;
 }