php使用socket调用http和smtp协议实例小结(2)


MAIL FROM:<发件人邮箱>
RCPT TO:<收件人邮箱>
##填写信件内容
DATA
MIME-Version:1.0
To: 收件人邮箱
From: 发件人邮箱
Subject: 邮件主题

邮件内容

fsocketopen发送邮件

fsocketopenpfsocketopen是php机制本身的函数,可以直接调用,不需要安装扩展。以上两个函数可以打开一个网络连接或者一个Unix套接字连接,然后我们可以使用fwrite,fread,fclose,feof等函数直接操作该网络连接。

<?php
$user = "root@163.com";
$password = "root";
$mailto = "1234567@qq.com";
$subject = "fsocketopen发送邮件";
$body = "收到邮件了么,亲";
$sock = fsockopen("smtp.163.com",25);
fputs($sock,"HELO localhost\r\n");
echo fgets($sock, 512)."<br/>";
fputs($sock,"AUTH LOGIN ".base64_encode($user)."\r\n");
echo fgets($sock, 512)."<br/>";
fputs($sock,base64_encode($password)."\r\n");
echo fgets($sock, 512)."<br/>";
fputs($sock,"MAIL FROM:<${user}>\r\n");
echo fgets($sock, 512)."<br/>";
fputs($sock,"RCPT TO:<${mailto}>\r\n");
echo fgets($sock, 512)."<br/>";
fputs($sock,"DATA\r\n");
echo fgets($sock, 512)."<br/>";
$header = "MIME-Version:1.0\r\n";
$header .= "To: ${mailto}\r\n";
$header .= "From: ${user}\r\n";
$header .= "Subject: ${subject}\r\n";
fputs($sock, $header . "\r\n" . $body);
echo fgets($sock, 512)."<br/>";
fputs($sock, "\r\n.\r\n");
echo fgets($sock, 512)."<br/>";
fputs($sock,"QUIT\r\n");
fclose($sock);

sockets扩展发送邮件

sockets扩展的功能更加强大,可以搭建tcp/ip服务器,不过我们使用smtp协议发送邮件的例子,只需要使用到socket客户端的功能。

 <?php
$user = "root@163.com";
$password = "root";
$mailto = "1234567@qq.com";
$subject = "sockets扩展发送邮件";
$body = "收到邮件了么,亲";
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($socket, "smtp.163.com",25);
socket_write($socket,"HELO localhost\r\n");
echo socket_read($socket, 512)."<br/>";
socket_write($socket,"AUTH LOGIN ".base64_encode($user)."\r\n");
echo socket_read($socket, 512)."<br/>";
socket_write($socket,base64_encode($password)."\r\n");
echo socket_read($socket, 512)."<br/>";
socket_write($socket,"MAIL FROM:<${user}>\r\n");
echo socket_read($socket, 512)."<br/>";
socket_write($socket,"RCPT TO:<${mailto}>\r\n");
echo socket_read($socket, 512)."<br/>";
socket_write($socket,"DATA\r\n");
echo socket_read($socket, 512)."<br/>";
$header = "MIME-Version:1.0\r\n";
$header .= "To: ${mailto}\r\n";
$header .= "From: ${user}\r\n";
$header .= "Subject: ${subject}\r\n";
socket_write($socket, $header . "\r\n" . $body);
echo socket_read($socket, 512)."<br/>";
socket_write($socket, "\r\n.\r\n");
echo socket_read($socket, 512)."<br/>";
socket_write($socket,"QUIT\r\n");
fclose($sock);


      

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/5682.html