Home > Backend Development > PHP Tutorial > PHP method to implement SMTP to send emails based on socket_PHP tutorial

PHP method to implement SMTP to send emails based on socket_PHP tutorial

WBOY
Release: 2016-07-13 10:04:59
Original
886 people have browsed it

How to use php to implement SMTP to send emails based on sockets

This article mainly introduces the method of php to implement SMTP to send emails based on sockets. An example analysis of how php uses sockets to implement smtp to send emails. The principles and techniques have certain reference value. Friends in need can refer to it

The example in this article describes how PHP implements SMTP to send emails based on sockets. Share it with everyone for your reference. The specific analysis is as follows:

php uses socket to send emails through SMTP.
Using the php-sockets extension of php, you can send emails in plain text and html formats. The code is as follows:

The code is as follows:


/**
* Email sending class
* Supports sending plain text emails and HTML format emails
* @example
* $config = array(
* "from" => "*****",
* "to" => "***",
* "subject" => "test",
* "body" => "test",
* "username" => "***",
* "password" => "****",
* "isHTML" => true
* );
*
* $mail = new MySendMail();
*
* $mail->setServer("smtp.126.com");
*
* $mail->setMailInfo($config);
* if(!$mail->sendMail()) {
* echo $mail->error();
* return 1;
* }
*/
class MySendMail {
/**
* @var Mail transfer agent username
* @access private
*/
private $_userName;
/**
* @var Mail transfer agent password
* @access private
*/
private $_password;
/**
* @var Mail transfer proxy server address
* @access protected
*/
protected $_sendServer;
/**
* @var Mail transfer proxy server port
* @access protected
*/
protected $_port=25;
/**
* @var sender
* @access protected
*/
protected $_from;
/**
* @var recipient
* @access protected
*/
protected $_to;
/**
* @var theme
* @access protected
*/
protected $_subject;
/**
* @var Email text
* @access protected
*/
protected $_body;
/**
* @var Whether it is an email in HTML format
* @access protected
*/
protected $_isHTML=false;
/**
* @var socket resource
* @access protected
*/
protected $_socket;
/**
* @var error message
* @access protected
*/
protected $_errorMessage;
public function __construct($from="", $to="", $subject="", $body="", $server="", $username="", $password="",$isHTML="", $port="") {
if(!empty($from)){
$this->_from = $from;
}
if(!empty($to)){
$this->_to = $to;
}
if(!empty($subject)){
$this->_subject = $subject;
}
if(!empty($body)){
$this->_body = $body;
}
if(!empty($isHTML)){
$this->_isHTML = $isHTML;
}
if(!empty($server)){
$this->_sendServer = $server;
}
if(!empty($port)){
$this->_port = $port;
}
if(!empty($username)){
$this->_userName = $username;
}
if(!empty($password)){
$this->_password = $password;
}
}
/**
* Set up mail transfer agent
* @param string $server The IP or domain name of the proxy server
* @param int $port proxy server port, smtp default port 25
* @param int $localPort local port
* @return boolean
*/
public function setServer($server, $port=25) {
if(!isset($server) || empty($server) || !is_string($server)) {
$this->_errorMessage = "first one is an invalid parameter";
return false;
}
if(!is_numeric($port)){
$this->_errorMessage = "first two is an invalid parameter";
return false;
}
$this->_sendServer = $server;
$this->_port = $port;
return true;
}
/**
* Set up email
* @access public
* @param array $config email configuration information
* Contains verification information of email sender, recipient, subject, content, and email transfer agent
* @return boolean
*/
public function setMailInfo($config) {
if(!is_array($config) || count($config) < 6){
$this->_errorMessage = "parameters are required";
return false;
}
$this->_from = $config['from'];
$this->_to = $config['to'];
$this->_subject = $config['subject'];
$this->_body = $config['body'];
$this->_userName = $config['username'];
$this->_password = $config['password'];
if(isset($config['isHTML'])){
$this->_isHTML = $config['isHTML'];
}
return true;
}
/**
* Send email
* @access public
* @return boolean
*/
public function sendMail() {
$command = $this->getCommand();
$this->socket();
foreach ($command as $value) {
if($this->sendCommand($value[0], $value[1])) {
continue;
}
else{
return false;
}
}
$this->close(); //In fact, there is no need to close here. After the smtp command: QUIT is issued, the server closes the connection and the local socket resources will be automatically released
echo 'Mail OK!';
return true;
}
/**
* Return error message
* @return string
*/
public function error(){
if(!isset($this->_errorMessage)) {
$this->_errorMessage = "";
}
return $this->_errorMessage;
}
/**
* Return to mail command
* @access protected
* @return array
*/
protected function getCommand() {
if($this->_isHTML) {
$mail = "MIME-Version:1.0rn";
$mail .= "Content-type:text/html;charset=utf-8rn";
$mail .= "FROM:test<" . $this->_from . ">rn";
$mail .= "TO:<" . $this->_to . ">rn";
$mail .= "Subject:" . $this->_subject ."rnrn";
$mail .= $this->_body . "rn.rn";
}
else{
$mail = "FROM:test<" . $this->_from . ">rn";
$mail .= "TO:<" . $this->_to . ">rn";
$mail .= "Subject:" . $this->_subject ."rnrn";
$mail .= $this->_body . "rn.rn";
}
$command = array(
array("HELO sendmailrn", 250),
array("AUTH LOGINrn", 334),
array(base64_encode($this->_userName) . "rn", 334),
array(base64_encode($this->_password) . "rn", 235),
array("MAIL FROM:<" . $this->_from . ">rn", 250),
array("RCPT TO:<" . $this->_to . ">rn", 250),
array("DATArn", 354),
array($mail, 250),
array("QUITrn", 221)
);
return $command;
}
/**
* @access protected
* @param string $command SMTP command sent to the server
* @param int $code Is the response expected from the server?
* @param boolean
*/
protected function sendCommand($command, $code) {
echo 'Send command:' . $command . ',expected code:' . $code . '
';
//Send command to server
try{
if(socket_write($this->_socket, $command, strlen($command))){
//Read server returns
$data = trim(socket_read($this->_socket, 1024));
echo 'response:' . $data . '

';
if($data) {
$pattern = "/^".$code."/";
if(preg_match($pattern, $data)) {
return true;
}
else{
$this->_errorMessage = "Error:" . $data . "|**| command:";
return false;
}
}
else{
$this->_errorMessage = "Error:" . socket_strerror(socket_last_error());
return false;
}
}
else{
$this->_errorMessage = "Error:" . socket_strerror(socket_last_error());
return false;
}
}catch(Exception $e) {
$this->_errorMessage = "Error:" . $e->getMessage();
}
}
/**
* Establish a network connection to the server
* @access private
* @return boolean
*/
private function socket() {
if(!function_exists("socket_create")) {
$this->_errorMessage = "extension php-sockets must be enabled";
return false;
}
//Create socket resource
$this->_socket = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));
if(!$this->_socket) {
$this->_errorMessage = socket_strerror(socket_last_error());
return false;
}
//Connect to server
if(!socket_connect($this->_socket, $this->_sendServer, $this->_port)) {
$this->_errorMessage = socket_strerror(socket_last_error());
return false;
}
socket_read($this->_socket, 1024);
return true;
}
/**
* 关闭socket
* @access private
* @return boolean
*/
private function close() {
if(isset($this->_socket) && is_object($this->_socket)) {
$this->_socket->close();
return true;
}
$this->_errorMessage = "no resource can to be close";
return false;
}
}
/**************************** Test ***********************************/
$config = array(
"from" => "XXXXX",
"to" => "XXXXX",
"subject" => "test",
"body" => "test",
"username" => "XXXXX",
"password" => "******",
//"isHTML" => true
);
$mail = new MySendMail();
$mail->setServer("smtp.126.com");
$mail->setMailInfo($config);
if(!$mail->sendMail()) {
echo $mail->error();
return 1;
}

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/963982.htmlTechArticlephp method to implement SMTP to send emails based on socket This article mainly introduces the method of php to implement SMTP to send emails based on socket , the example analyzes the principle of php using socket to implement SMTP to send emails...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template