function SMTP::data

5.x SMTP.php public SMTP::data($msg_data)

Send an SMTP DATA command. Issues a data command and sends the msg_data to the server, finializing the mail transaction. $msg_data is the message that is to be send with the headers. Each header needs to be on a single line followed by a <CRLF> with the message headers and the message body being separated by an additional <CRLF>. Implements RFC 821: DATA <CRLF>.

Parameters

string $msg_data Message data to send:

Return value

bool

File

inc/PHPMailer/src/SMTP.php, line 641

Class

SMTP
PHPMailer RFC821 SMTP email transport class. Implements RFC 821 SMTP commands and provides some utility methods for sending mail to an SMTP server.

Code

public function data($msg_data) {
  //This will use the standard timelimit
  if (!$this->sendCommand('DATA', 'DATA', 354)) {
    return false;
  }

  /* The server is ready to accept data!
   * According to rfc821 we should not send more than 1000 characters on a single line (including the LE)
   * so we will break the data up into lines by \r and/or \n then if needed we will break each of those into
   * smaller lines to fit within the limit.
   * We will also look for lines that start with a '.' and prepend an additional '.'.
   * NOTE: this does not count towards line-length limit.
   */

  // Normalize line breaks before exploding
  $lines = explode("\n", str_replace(["\r\n" "\r"], "\n", $msg_data));

  /* To distinguish between a complete RFC822 message and a plain message body, we check if the first field
   * of the first line (':' separated) does not contain a space then it _should_ be a header and we will
   * process all lines before a blank line as headers.
   */

  $field = substr($lines[0], 0, strpos($lines[0], ':'));
  $in_headers = false;
  if (!empty($field) and strpos($field, ' ') === false) {
    $in_headers = true;
  }

  foreach ($lines as $line) {
    $lines_out =[];
    if ($in_headers and $line == '') {
      $in_headers = false;
    }
    //Break this line up into several smaller lines if it's too long
    //Micro-optimisation: isset($str[$len]) is faster than (strlen($str) > $len),
    while (isset($line[self::MAX_LINE_LENGTH])) {
      //Working backwards, try to find a space within the last MAX_LINE_LENGTH chars of the line to break on
      //so as to avoid breaking in the middle of a word
      $pos = strrpos(substr($line, 0, self::MAX_LINE_LENGTH), ' ');
      //Deliberately matches both false and 0
      if (!$pos) {
        //No nice break found, add a hard break
        $pos = self::MAX_LINE_LENGTH - 1;
        $lines_out[] = substr($line, 0, $pos);
        $line = substr($line, $pos);
      }
      else {
        //Break at the found point
        $lines_out[] = substr($line, 0, $pos);
        //Move along by the amount we dealt with
        $line = substr($line, $pos + 1);
      }
      //If processing headers add a LWSP-char to the front of new line RFC822 section 3.1.1
      if ($in_headers) {
        $line = "\t" . $line;
      }
    }
    $lines_out[] = $line;

    //Send the lines to the server
    foreach ($lines_out as $line_out) {
      //RFC2821 section 4.5.2
      if (!empty($line_out) and $line_out[0] == '.') {
        $line_out = '.' . $line_out;
      }
      $this->client_send($line_out . static::LE, 'DATA');
    }
  }

  //Message data has been sent, complete the command
  //Increase timelimit for end of DATA command
  $savetimelimit = $this->Timelimit;
  $this->Timelimit = $this->Timelimit * 2;
  $result = $this->sendCommand('DATA END', '.', 250);
  $this->recordLastTransactionID();
  //Restore timelimit
  $this->Timelimit = $savetimelimit;

  return $result;
}