PHP邮件忽略密件抄送

时间:2014-02-28 09:49:27

标签: php

我真的在努力学习这门课。我发这样的邮件:

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);
}

这是我的send()函数和我的packheader()函数:

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;
        }
    }
}

在这里,我正在添加CCO / BCC:

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

发送的邮件包含正文中的BCC名称,而不是标题中的BCC名称。有谁看到这个问题?邮件会正确发送到$naam$email,只有BCC无效。

1 个答案:

答案 0 :(得分:2)

你错过了在这里追加EOL

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