PHPMailer\PHPMailer

PHPMailer::sendmailSendprotectedWP 1.0

Send mail using the $Sendmail program.

Method of the class: PHPMailer{}

No Hooks.

Returns

true|false.

Usage

// protected - for code of main (parent) or child class
$result = $this->sendmailSend( $header, $body );
$header(string) (required)
The message headers.
$body(string) (required)
The message body.

Notes

  • See: PHPMailer::$Sendmail

PHPMailer::sendmailSend() code WP 7.0

protected function sendmailSend($header, $body)
{
    if ($this->Mailer === 'qmail') {
        $this->edebug('Sending with qmail');
    } else {
        $this->edebug('Sending with sendmail');
    }
    $header = static::stripTrailingWSP($header) . static::$LE . static::$LE;
    //This sets the SMTP envelope sender which gets turned into a return-path header by the receiver
    //A space after `-f` is optional, but there is a long history of its presence
    //causing problems, so we don't use one
    //Exim docs: https://www.exim.org/exim-html-current/doc/html/spec_html/ch-the_exim_command_line.html
    //Sendmail docs: https://www.sendmail.org/~ca/email/man/sendmail.html
    //Example problem: https://www.drupal.org/node/1057954

    //PHP 5.6 workaround
    $sendmail_from_value = ini_get('sendmail_from');
    if (empty($this->Sender) && !empty($sendmail_from_value)) {
        //PHP config has a sender address we can use
        $this->Sender = ini_get('sendmail_from');
    }

    $sendmailArgs = [];

    // CVE-2016-10033, CVE-2016-10045: Don't pass -f if characters will be escaped.
    // Also don't add the -f automatically unless it has been set either via Sender
    // or sendmail_path. Otherwise it can introduce new problems.
    // @see http://github.com/PHPMailer/PHPMailer/issues/2298
    if (!empty($this->Sender) && static::validateAddress($this->Sender) && self::isShellSafe($this->Sender)) {
        $sendmailArgs[] = '-f' . $this->Sender;
    }

    // Qmail doesn't accept all the sendmail parameters
    // @see https://github.com/PHPMailer/PHPMailer/issues/3189
    if ($this->Mailer !== 'qmail') {
        $sendmailArgs[] = '-i';
        $sendmailArgs[] = '-t';
    }

    $resultArgs = (empty($sendmailArgs) ? '' : ' ' . implode(' ', $sendmailArgs));

    $sendmail = trim(escapeshellcmd($this->Sendmail) . $resultArgs);
    $this->edebug('Sendmail path: ' . $this->Sendmail);
    $this->edebug('Sendmail command: ' . $sendmail);
    $this->edebug('Envelope sender: ' . $this->Sender);
    $this->edebug("Headers: {$header}");

    if ($this->SingleTo) {
        foreach ($this->SingleToArray as $toAddr) {
            $mail = @popen($sendmail, 'w');
            if (!$mail) {
                throw new Exception(self::lang('execute') . $this->Sendmail, self::STOP_CRITICAL);
            }
            $this->edebug("To: {$toAddr}");
            fwrite($mail, 'To: ' . $toAddr . "\n");
            fwrite($mail, $header);
            fwrite($mail, $body);
            $result = pclose($mail);
            $addrinfo = static::parseAddresses($toAddr, null, $this->CharSet);
            foreach ($addrinfo as $addr) {
                $this->doCallback(
                    ($result === 0),
                    [[$addr['address'], $addr['name']]],
                    $this->cc,
                    $this->bcc,
                    $this->Subject,
                    $body,
                    $this->From,
                    []
                );
            }
            $this->edebug("Result: " . ($result === 0 ? 'true' : 'false'));
            if (0 !== $result) {
                throw new Exception(self::lang('execute') . $this->Sendmail, self::STOP_CRITICAL);
            }
        }
    } else {
        $mail = @popen($sendmail, 'w');
        if (!$mail) {
            throw new Exception(self::lang('execute') . $this->Sendmail, self::STOP_CRITICAL);
        }
        fwrite($mail, $header);
        fwrite($mail, $body);
        $result = pclose($mail);
        $this->doCallback(
            ($result === 0),
            $this->to,
            $this->cc,
            $this->bcc,
            $this->Subject,
            $body,
            $this->From,
            []
        );
        $this->edebug("Result: " . ($result === 0 ? 'true' : 'false'));
        if (0 !== $result) {
            throw new Exception(self::lang('execute') . $this->Sendmail, self::STOP_CRITICAL);
        }
    }

    return true;
}