smtp协议附php示例

作者:zarte    发布时间: 2021-01-18

smtp邮件

基础方法定义

$this->delimiter = "\r\n";
function sendMessage($msg, $expectedResult = false)
{
if ($msg !== false && !empty($msg))
{
fputs($this->smtpSocket, $msg . "\r\n");
}
if ($expectedResult !== false)
{
$result = '';
while ($line = @fgets($this->smtpSocket, 1024))
{
$result .= $line;
if (preg_match('#^(\d{3}) #', $line, $matches))
{
break;
}
}
$this->smtpReturn = intval($matches[1]);
return ($this->smtpReturn == $expectedResult);
}
return true;
}

smtp协议流程

建立连接

$this->smtpSocket = @fsockopen($this->smtpHost, $this->smtpPort, $errno, $errstr, 30);

发送hellow

$this->sendMessage("HELO smtp.qq.com",250);

发送登录请求

$this->sendMessage("AUTH LOGIN",334);

发送用户名

$this->sendMessage(base64_encode($this->user),334);

发送密码

$this->sendMessage(base64_encode($this->pass)."",235);

发送发件人

$this->sendMessage("MAIL FROM:<".$from.">",250);

发送收件人(多个)

$this->sendMessage("RCPT TO:<".$to.">",250);

开始发送邮件内容

$this->do_command("DATA",354);

正式发送,可以多次调用

日期
$this->sendMessage('Date: ' . gmdate('r'), false);
收件人
$this->sendMessage('To: ' . $this->toemail, false);
头部
$this->sendMessage(trim($this->headers), false); // trim to prevent double \r\n
标题
$this->sendMessage('Subject: ' . $this->subject, false);
// catch any single dots on their own $this->message = preg_replace('#^.' . $this->delimiter . '#m', '..' . $this->delimiter, $this->message);

$this->sendMessage($this->message, false);

结束

$this->sendMessage("\r\n.", 250)

断开

$this->sendMessage('QUIT', 221);

上一篇:  php ftp扩展ftp_cdup方法部分ftp服务器异常

下一篇:  mysql InnoDB与MyISAM引擎插入性能测试

加载更多