Send e-mail with socket (with SMTP server that needs to be validated) _php Foundation

Source: Internet
Author: User

?

* Name: send email with socket
* Description: This class implements the direct use of the SMTP server that needs to be authenticated to send mail directly, reference article "send email with socket" Author: Limodou
* This article is relatively early, he is using not to verify the SMTP server to send mail, now basically the SMTP server needs to verify, so this article in the class
The meaning is not very big! Also refer to [RFC 1869] and PHP manual!! And what's different is that I'm not using the Fsockopen () function
See for yourself!! I just passed the test, it's cool!!
In fact, this class can be rewritten to directly send the message with the attachment, look forward to ... I'll write it out for you in a few days!
I just debugging through, if you do not malicious to tease this program, he is still very obedient, after a few days I will be perfect for him plus the function of sending attachments!!

Even if your virtual host does not support mail functions, now you are not afraid! Come on, try this!


Please respect the success of other people's work, please retain this copyright information, thank you!
Author: Small Dew 3.3, welcome to contact me to exchange PHP QQ6550382
Mail:cleverzhang@tom.com
*/
Set_time_limit (120);
Class Smtp_mail
{
var $host; Host
var $port; Port is typically 25
var $user; SMTP-Certified Account number
var $pass; Authentication password
var $debug = false; Do you want to display and server session information?
var $conn;
var $result _str; Results
var $in; Commands sent by the client
var $from; Source Mailbox
var $to; Target Mailbox
var $subject; Theme
var $body; Content
function Smtp_mail ($host, $port, $user, $pass, $debug =false)
{
$this->host = $host;
$this->port = $port;
$this->user = Base64_encode ($user);
$this->pass = Base64_encode ($pass);
$this->debug = $debug;
$this->socket = socket_create (Af_inet, Sock_stream, sol_tcp); Please refer to the manual for specific usage
if ($this->socket)
{
$this->result_str = "Create socket:". Socket_strerror (Socket_last_error ());
$this->debug_show ($this->result_str);
}
Else
{
Exit ("Initialization failed, please check your network connections and parameters");
}
$this->conn = Socket_connect ($this->socket, $this->host, $this->port);
if ($this->conn)
{
$this->result_str = "Create socket Connection:". Socket_strerror (Socket_last_error ());
$this->debug_show ($this->result_str);
}
Else
{
Exit ("Initialization failed, please check your network connections and parameters");
}
$this->result_str = "Server answer: <font color= #cc0000 >". Socket_read ($this->socket, 1024). " </font> ";
$this->debug_show ($this->result_str);


}
function Debug_show ($STR)
{
if ($this->debug)
{
echo $str. " <p>\r\n ";
}
}
function Send ($from, $to, $subject, $body)
{
if ($from = = "" | | $to = = "")
{
Exit ("Please enter the mailbox address");
}
if ($subject = = "") $sebject = "Untitled";
if ($body = = "") $body = "no content";
$this->from = $from;
$this->to = $to;
$this->subject = $subject;
$this->body = $body;

$All = "From:" $this->from. " \ n ";
$All. = "To:" $this->to. " \ n ";
$All. = "Subject:" $this->subject. " \ n ";
$All. = $this->body;
/*
If you add $all to the content, you can implement a MIME message
But we need to add a lot of procedures.
*/


The following are the and server sessions
$this->in = "EHLO helo\r\n";
$this->docommand ();

$this->in = "AUTH login\r\n";
$this->docommand ();

$this->in = $this->user. " \ r \ n ";
$this->docommand ();

$this->in = $this->pass. " \ r \ n ";
$this->docommand ();

$this->in = "MAIL from:". $this->from. " \ r \ n ";
$this->docommand ();

$this->in = "RCPT to:". $this->to. " \ r \ n ";
$this->docommand ();

$this->in = "data\r\n";
$this->docommand ();

$this->in = $All. " \r\n.\r\n ";
$this->docommand ();

$this->in = "quit\r\n";
$this->docommand ();

End, close connection

}
Function DoCommand ()
{
Socket_write ($this->socket, $this->in, strlen ($this->in));
$this->debug_show ("Client command:". $this->in);
$this->result_str = "Server answer: <font color= #cc0000 >". Socket_read ($this->socket, 1024). " </font> ";
$this->debug_show ($this->result_str);
}
}
//This is my test, I use the smtp.163.com, then your mailbox must be 163.com, or else people do not let you hair!!
//You can change it to your own mailbox when you use this class
$smtp   =   new Smtp_mail ("smtp.163.com", "25", "Your 163.com account", "Your password" );
//If you need to display session information, please change the above to
//$SMTP   =   new Smtp_mail ("smtp.163.com", "25", "Your 163.com account", "Your password" , true);
$smtp->send ("Your 163.com account @163.com", "Destination address", "Hello", "Hello");
?>

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.