Send an e-mail with a socket (using an SMTP server that requires authentication) _php Tutorial

Source: Internet
Author: User

* Name: send email with socket
* Description: This class enables direct mail to be sent directly using an SMTP server that needs to be verified, refer to the article "email with socket" Author: Limodou
* This article earlier, he is to use the SMTP server to send mail, and now basically the SMTP server need to verify, so the class in this article
The meaning is not very big! Also reference the [RFC 1869] and PHP manuals!! And what's different from the above is that I'm not using the Fsockopen () function
See for yourself!! I just passed the test, very cool!!
In fact, to rewrite this class can be sent directly with the attachment of mail, look forward to ... I'll write it 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 some days I will be perfect plus the ability to send attachments!!

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


Please respect other people's labor success, please keep this copyright information, thank you!
Author: Little Dewdrop 3.3, Welcome to contact me for the 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 Accounts
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 ("Initialize failed, please check your network connection 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 ("Initialize failed, please check your network connection and parameters");
}
$this->result_str = "Server Answer:". Socket_read ($this->socket, 1024). "";
$this->debug_show ($this->result_str);


}
function Debug_show ($STR)
{
if ($this->debug)
{
echo $str. "

\ r \ n ";
}
}
function Send ($from, $to, $subject, $body)
{
if ($from = = "" | | $to = = "")
{
Exit ("Please enter your email 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 the content of the $all to add processing, you can send a MIME message
But we need to add a lot of programs.
*/


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:". Socket_read ($this->socket, 1024). "";
$this->debug_show ($this->result_str);
}
}
This is I do test, I use is smtp.163.com, then your mailbox also must be 163.com, or others don't let you send!!
When you use this class, you change it to your own mailbox.
$SMTP = new Smtp_mail ("smtp.163.com", "25", "Your 163.com account", "Your password");
If you need to display the session information, please change the
$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");
?>

http://www.bkjia.com/PHPjc/314294.html www.bkjia.com true http://www.bkjia.com/PHPjc/314294.html techarticle * Name: Send e-mail with socket * Description: This class implements direct mail using the SMTP server that requires authentication, referring to the article "email with socket" Author: ...

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