protected function getCommand()
{
$separator = "----=_Part_" . md5($this->_address . time()) . uniqid();
//分隔符
$command = array(array("HELO sendmail\r\n", 250));
if (!empty($this->_userName)) {
$command[] = array("AUTH LOGIN\r\n", 334);
$command[] = array($this->_userName . "\r\n", 334);
$command[] = array($this->_password . "\r\n", 235);
}
//设置发件人
$command[] = array("MAIL FROM: <" . $this->_address . ">\r\n", 250);
$header = "FROM: " . $this->_from . "\r\n";
//设置收件人
if (!empty($this->_to)) {
$count = count($this->_to);
if ($count == 1) {
$command[] = array("RCPT TO: <" . $this->_to[0] . ">\r\n", 250);
$header .= "TO: <" . $this->_to[0] . ">\r\n";
} else {
for ($i = 0; $i < $count; $i++) {
$command[] = array("RCPT TO: <" . $this->_to[$i] . ">\r\n", 250);
if ($i == 0) {
$header .= "TO: <" . $this->_to[$i] . ">";
} elseif ($i + 1 == $count) {
$header .= ",<" . $this->_to[$i] . ">\r\n";
} else {
$header .= ",<" . $this->_to[$i] . ">";
}
}
}
}
//设置抄送
if (!empty($this->_cc)) {
$count = count($this->_cc);
if ($count == 1) {
$command[] = array("RCPT TO: <" . $this->_cc[0] . ">\r\n", 250);
$header .= "CC: <" . $this->_cc[0] . ">\r\n";
} else {
for ($i = 0; $i < $count; $i++) {
$command[] = array("RCPT TO: <" . $this->_cc[$i] . ">\r\n", 250);
if ($i == 0) {
$header .= "CC: <" . $this->_cc[$i] . ">";
} elseif ($i + 1 == $count) {
$header .= ",<" . $this->_cc[$i] . ">\r\n";
} else {
$header .= ",<" . $this->_cc[$i] . ">";
}
}
}
}
//设置秘密抄送
if (!empty($this->_bcc)) {
$count = count($this->_bcc);
if ($count == 1) {
$command[] = array("RCPT TO: <" . $this->_bcc[0] . ">\r\n", 250);
$header .= "BCC: <" . $this->_bcc[0] . ">\r\n";
} else {
for ($i = 0; $i < $count; $i++) {
$command[] = array("RCPT TO: <" . $this->_bcc[$i] . ">\r\n", 250);
if ($i == 0) {
$header .= "BCC: <" . $this->_bcc[$i] . ">";
} elseif ($i + 1 == $count) {
$header .= ",<" . $this->_bcc[$i] . ">\r\n";
} else {
$header .= ",<" . $this->_bcc[$i] . ">";
}
}
}
}
//主题
$header .= "Subject: =?UTF-8?B?" . $this->_subject . "?=\r\n";
if (isset($this->_attachment)) {
//含有附件的邮件头需要声明成这个
$header .= "Content-Type: multipart/mixed;\r\n";
} elseif (false) {
//邮件体含有图片资源的,且包含的图片在邮件内部时声明成这个,如果是引用的远程图片,就不需要了
$header .= "Content-Type: multipart/related;\r\n";
} else {
//html或者纯文本的邮件声明成这个
$header .= "Content-Type: multipart/alternative;\r\n";
}
//邮件头分隔符
$header .= "\t" . 'boundary="' . $separator . '"';
$header .= "\r\nMIME-Version: 1.0\r\n";
//这里开始是邮件的body部分,body部分分成几段发送
$header .= "\r\n--" . $separator . "\r\n";
$header .= "Content-Type:text/html; charset=utf-8\r\n";
$header .= "Content-Transfer-Encoding: base64\r\n\r\n";
$header .= $this->_body . "\r\n";
$header .= "--" . $separator . "\r\n";
//加入附件
if (!empty($this->_attachment)) {
$count = count($this->_attachment);
for ($i = 0; $i < $count; $i++) {
$header .= "\r\n--" . $separator . "\r\n";
$header .= "Content-Type: " . $this->getMIMEType($this->_attachment[$i]) . '; name="=?UTF-8?B?' . base64_encode(basename($this->_attachment[$i])) . '?="' . "\r\n";
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= 'Content-Disposition: attachment; filename="=?UTF-8?B?' . base64_encode(basename($this->_attachment[$i])) . '?="' . "\r\n";
$header .= "\r\n";
$header .= $this->readFile($this->_attachment[$i]);
$header .= "\r\n--" . $separator . "\r\n";
}
}
//结束邮件数据发送
$header .= "\r\n.\r\n";
$command[] = array("DATA\r\n", 354);
$command[] = array($header, 250);
$command[] = array("QUIT\r\n", 221);
return $command;
}