PHP based on socket implementation of SMTP send mail method, socketsmtp_php tutorial

Source: Internet
Author: User

PHP based on the socket implementation of SMTP send mail method, SOCKETSMTP


The example in this article describes how PHP sends mail based on the socket implementation of SMTP. Share to everyone for your reference. The specific analysis is as follows:

PHP uses a socket to send mail via SMTP.
With PHP's php-sockets extension, you can send plain text and HTML-formatted messages. The code is as follows:
Copy CodeThe code is as follows:
<?php
/**
* Mail Sending Class
* Supports sending plain text messages and HTML-formatted messages
* @example
* $config = Array (
* "from" and "* * * *",
* "To" = "* *",
* "Subject" = "Test",
* "Body" and "="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 user name
* @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 Recipients
* @access protected
*/
protected $_to;
/**
* @var Theme
* @access protected
*/
protected $_subject;
/**
* @var message body
* @access protected
*/
protected $_body;
/**
* @var Whether it 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 a mail transfer agent
* @param string $server IP or domain name of proxy server
* @param int $port The port of the proxy server, 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 = "First one is an invalid parameter";
return false;
}
if (!is_numeric ($port)) {
$this->_errormessage = "First and invalid parameter";
return false;
}
$this->_sendserver = $server;
$this->_port = $port;
return true;
}
/**
* Set up Mail
* @access Public
* @param array $config mail configuration information
* Contains the message sender, recipient, subject, content, message transfer agent authentication information
* @return Boolean
*/
Public Function Setmailinfo ($config) {
if (!is_array ($config) | | count ($config) < 6) {
$this->_errormessage = "parameters is 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 (); In fact, there is no need to close, SMTP command: Quit after the server closed the connection, 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.0\r\n";
$mail. = "content-type:text/html;charset=utf-8\r\n";
$mail. = "from:test<". $this->_from. ">\r\n";
$mail. = "to:<". $this->_to. ">\r\n";
$mail. = "Subject:". $this->_subject. " \r\n\r\n ";
$mail. = $this->_body. "\r\n.\r\n";
}
else{
$mail = "from:test<". $this->_from. ">\r\n";
$mail. = "to:<". $this->_to. ">\r\n";
$mail. = "Subject:". $this->_subject. " \r\n\r\n ";
$mail. = $this->_body. "\r\n.\r\n";
}
$command = Array (
Array ("HELO sendmail\r\n", 250),
Array ("AUTH login\r\n", 334),
Array (Base64_encode ($this->_username). "\ r \ n", 334),
Array (Base64_encode ($this->_password). "\ r \ n", 235),
Array ("MAIL from:<". $this->_from. ">\r\n", 250),
Array ("RCPT to:<". $this->_to. ">\r\n", 250),
Array ("data\r\n", 354),
Array ($mail, 250),
Array ("quit\r\n", 221)
);
return $command;
}
/**
* @access protected
* @param string $command SMTP command sent to the server
* @param int $code 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 return
$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 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" and "="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 is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/963852.html www.bkjia.com true http://www.bkjia.com/PHPjc/963852.html techarticle PHP based on the socket implementation of SMTP send mail method, SOCKETSMTP This article describes the PHP socket-based implementation of SMTP send mail method. Share to everyone for your reference. The specific analysis is as follows: ...

  • 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.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.