[php]smtp.class.php

Source: Internet
Author: User
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 _r; A real source mailbox, typically the same as the user name of the SMTP server, or it may be sent unsuccessfully due to the SMTP server's settings
var $mailformat = 0; Message Format 0 = Plain Text 1=html Mail
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";

$All = "From:". $from. " \ r \ n ";
$All. = "To:". $to. " \ r \ n ";
$All. = "Subject:". $subject. " \ r \ n ";
if ($this->mailformat==1) $All. = "content-type:text/html;\r\n";
else $All. = "content-type:text/plain;\r\n";
$All. = "charset=gb2312\r\n\r\n";
$All. = $body;
/*
If the content of the $all is added, it can be implemented to send MIME messages.
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 ();

if (!eregi ("235", $this->result_str)) {
$this->result_str = "SMTP authentication failed";
$this->debug_show ($this->result_str);
return 0;
}

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

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

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

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

if (!eregi ("+", $this->result_str)) {
$this->result_str = "message sent Failed";
$this->debug_show ($this->result_str);
return 0;
}

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

End, close connection
return 1;
}

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);
}

}//end Class
?>


First, fill in the contents of the above program in the file, such as:
var $ host= "smtp.sina.com.cn"; Host
var $port = "25"; Port is typically 25
var $user = "Users"; SMTP-certified Accounts
var $pass = "users123; Authentication password

Then put this file on your site, as long as your site's program access to the line,.
And then to use the e-mail program included in such as: Include (' smtp.class.php ');
And then use it in this email program.
$mails =new Smtp_mail ();
if ($mail->send ("Sender's Address", "Recipient's email address", "subject", "content")) {
echo "sent successfully!";
}else{
echo "Mail server busy, please try again later";
}

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