Php method for sending mail by SMTP based on socket

Source: Internet
Author: User
Tags config socket server port

This article mainly introduces the PHP based on the socket to implement SMTP mail method, the example of PHP using socket to implement the SMTP mail message principle and skills, with a certain reference value, the need for friends can refer to the

-->

This article is an example of how PHP enables SMTP to send mail based on sockets. Share to everyone for your reference. The specific analysis is as follows:

PHP uses sockets to send mail through SMTP.
With the php-sockets extension of PHP, you can send messages in plain text and HTML format. The code is as follows:

The code is as follows:
<?php
/**
* Mail Send Class
* Support for sending plain text messages and HTML-formatted messages
* @example
* $config = Array (
* "from" => "* * *",
* "to" => "* * *",
* "Subject" => "test",
* "Body" => "<b>test</b>",
* "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 message transfer agent user name
* @access Private
*/
Private $_username;
/**
* @var message transfer agent password
* @access Private
*/
Private $_password;
/**
* @var Message transfer proxy server address
* @access protected
*/
protected $_sendserver;
/**
* @var The message transfer Proxy server port
* @access protected
*/
protected $_port=25;
/**
* @var Sender
* @access protected
*/
protected $_from;
/**
* @var Recipients
* @access protected
*/
protected $_to;
/**
* @var Theme
* @access protected
*/
protected $_subject;
/**
* @var message body
* @access protected
*/
protected $_body;
/**
* @var is an HTML-formatted message
* @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 the message transfer agent
* @param string $server the IP or domain name of the proxy server
* @param int $port proxy server port, SMTP default port 25th
* @param int $localPort Local Port
* @return Boolean
*/
Public Function Setserver ($server, $port =25) {
if (!isset ($server) | | | empty ($server) | | |!is_string ($server)) {
$this->_errormessage = "The one is a invalid parameter";
return false;
}
if (!is_numeric ($port)) {
$this->_errormessage = "The two is a invalid parameter";
return false;
}
$this->_sendserver = $server;
$this->_port = $port;
return true;
}
/**
* Set up Mail
* @access Public
* @param array $config mail configuration information
* Contains authentication information for the sender, recipient, subject, content, and message transfer agent of the message.
* @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 mail
* @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 (); There's no need to shut down, SMTP command: After the quit issue, the server closes the connection and the local socket resource is automatically released
Echo ' Mail ok! ';
return true;
}
/**
* Return error message
* @return String
*/
Public Function error () {
if (!isset ($this->_errormessage)) {
$this->_errormessage = "";
}
return $this->_errormessage;
}
/**
* Back 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 Expect the server to return a response?
* @param boolean
*/
protected function SendCommand ($command, $code) {
Echo ' Send command: '. $command. ', expected code: '. $code. ' <br/> ';
Send command to Server
try{
if (Socket_write ($this->_socket, $command, strlen ($command)) {
Read Server return
$data = Trim (Socket_read ($this->_socket, 1024));
Echo ' response: '. $data. ' <br/><br/> ';
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 a Socket resource
$this->_socket = socket_create (Af_inet, Sock_stream, Getprotobyname (' TCP '));
if (! $this->_socket) {
$this->_errormessage = Socket_strerror (Socket_last_error ());
return false;
}
Connecting to a 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;
}
/**
* Close 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" => "<b>test</b>",
"username" => "XXXXX",
"Password" => "Hu Jintao",
"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 help you with your PHP program design.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.