简体   繁体   中英

PHP mail ignores Bcc

I'm really struggling with this class. I'm sending a mail like this:

try {
    // minimal requirements to be set
    $dummy = new Mailer();
    $dummy->setFrom("MAIL SCRIPT", "info@mail.com");
    $dummy->addRecipient($naam, $email);
    $dummy->fillSubject("Jouw ruiling #" . $_GET['id']);
    $dummy->addCCO("Testuser1", "test@example.com");
    $dummy->addCCO("Testuser2", "test2@example.com");
    $dummy->fillMessage($myMessage);

    // now we send it!
    $dummy->send();
} catch (Exception $e) {
    echo $e->getMessage();
    exit(0);
}

And this is my send() function and my packheader() function:

public function send() {
    if (is_null($this->to)) {
        throw new Exception("Must have at least one recipient.");
    }

    if (is_null($this->from)) {
        throw new Exception("Must have one, and only one sender set.");
    }

    if (is_null($this->subject)) {
        throw new Exception("Subject is empty.");
    }

    if (is_null($this->textMessage)) {
        throw new Exception("Message is empty.");
    }

    $this->packHeaders();

    $sent = mail($this->to, $this->subject, $this->textMessage, $this->headers);
    if(!$sent) {
        $errorMessage = "Server couldn't send the email.";
        throw new Exception($errorMessage);
    } else {
        return true;
    }
}


private function packHeaders() {
    if (!$this->headers) {
        $this->headers = "MIME-Version: 1.0" . PHP_EOL;
        $this->headers .= "Content-Type: text/html; charset=\"utf-8\"" . PHP_EOL;
        $this->headers .= "Content-Transfer-Encoding: 7bit";
        $this->headers .= "From: " . $this->from . PHP_EOL;
        $this->headers .= "Bcc: " . $this->cco . PHP_EOL;

        if (self::STRIP_RETURN_PATH !== TRUE) {
            $this->headers .= "Reply-To: " . $this->replyTo . PHP_EOL;
            $this->headers .= "Return-Path: " . $this->from . PHP_EOL;
        }
    }
}

And here I'm adding the CCO/BCC:

public function addCCO($name, $address) {
    $this->cco .= (is_null($this->cco)) ? ("$name <$address>") : (", " . "$name <$address>");
    return $this;
}

The mail that is send contains the BCC names in the body and not in the headers. Does anyone see the problem? The mail is sent to the $naam and $email correctly, only the BCC is not working.

You missed appending EOL here

$this->headers .= "Content-Transfer-Encoding: 7bit" . PHP_EOL;
                                               //   ^ Here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM