Use PHP to send source code through socket and support sending Authentication

Source: Internet
Author: User

Use PHP to directly open the socket port for sending small messagesProgram
It is characterized by the use of SMTP authentication, which can better meet the needs of general web site construction.

The entire source code is included in this article, and the same content can be downloaded at the following address.
Indicate the source for reprinting.

Http://www.knowsky.com/download/phpsendmail.rar

/*************************************** ***/
/* PHP webmail sender */
/* Www.ebugs.org */
/* Copyright rich in im286.com 2005.03.07 */
/* Indicate the source for reprinting */
/*************************************** ***/

// Configure the sender authentication username, password, SMTP server address, and sender's mailbox as prompted before use.
// Note that # is replaced with #, Which is copyrighted by the owner (www.im286.com) and can be used at will, but retains the copyright information.

// Example
Echo send_mail ('fuweng # im286.com ', 'mail title', 'mail content ');

Function send_mail ($ to, $ subject = 'no subobject', $ body ){
$ Loc_host = "im286"; // The Name Of The email sending computer.
$ Smtp_acc = "fuweng # im286.com"; // SMTP-authenticated user name, like a fuweng@im286.com or fuweng
$ Smtp_pass = "password"; // password for SMTP authentication, which is generally equivalent to a POP3 PASSWORD
$ Smtp_host = "smtp.im286.com"; // SMTP server address, similar to smtp.tom.com
$ From = "fuweng # im286.com"; // the sender's email address.
$ Headers = "Content-Type: text/plain; charset = \" gb2312 \ "\ r \ ncontent-transfer-encoding: base64 ";
$ LB = "\ r \ n"; // linebreak

$ HDR = explode ($ LB, $ headers); // resolved HDR
If ($ body) {$ bdy = preg_replace ("/^ \. /",".. ", explode ($ LB, $ body);} // The parsed body

$ SMTP = array (
// 1. EHLO, expecting to return 220 or 250
Array ("ehlo". $ loc_host. $ LB, "220,250", "HELO error :"),
// 2. send auth login and expect to return 334
Array ("auth login". $ LB, "334", "auth error :"),
// 3. Send the base64-encoded user name, and a 334 error is returned.
Array (base64_encode ($ smtp_acc). $ lbs, "334", "authentification error :"),
// 4. Send the base64 encoded password and expect to return 235
Array (base64_encode ($ smtp_pass). $ lbs, "235", "authentification error :"));
// 5. Send mail from and expect to return 250
$ SMTP [] = array ("mail from:". $ LB, "250", "mail from error :");
// 6. Send rcpt. Expected 250
$ SMTP [] = array ("rcpt to:". $ lb," 250 "," rcpt to error :");
// 7. Send data and expect to return 354
$ SMTP [] = array ("data". $ LB, "354", "data error :");
// 8.0. Send from
$ SMTP [] = array ("from:". $ from. $ LB ,"","");
// 8.2. Send.
$ SMTP [] = array ("to:". $ to. $ LB ,"","");
// 8.1. Sending title
$ SMTP [] = array ("Subject:". $ subject. $ LB ,"","");
// 8.3. Send other header content
Foreach ($ HDR as $ h) {$ SMTP [] = array ($ H. $ LB ,"","");}
// 8.4. Send an empty line and end header sending
$ SMTP [] = array ($ LB ,"","");
// 8.5 send email subject
If ($ bdy) {foreach ($ bdy as $ B) {$ SMTP [] = array (base64_encode ($ B. $ lb ). $ LB ,"","");}}
// 9. Sending "." indicates that the mail is over and 250 is expected to be returned.
$ SMTP [] = array (".". $ LB, "250", "data (end) error :");
// 10. Send quit and exit. expected to return 221
$ SMTP [] = array ("quit". $ LB, "221", "Quit error :");

// Open the SMTP server port
$ Fp = @ fsockopen ($ smtp_host, 25 );
If (! $ FP) echo"Error:Cannot conect to ". $ smtp_host ."
";
While ($ result = @ fgets ($ FP, 1024) {If (substr ($ result, 3, 1) = "") {break ;}}

$ Result_str = "";
// Send commands/data in the SMTP Array
Foreach ($ SMTP as $ req ){
// Send information
@ Fputs ($ FP, $ req [0]);
// If you want to receive the information returned by the server
If ($ req [1]) {
// Receive information
While ($ result = @ fgets ($ FP, 1024 )){
If (substr ($ result, 3, 1) = "") {break ;}
};
If (! Strstr ($ req [1], substr ($ result, 0, 3 ))){
$ Result_str. = $ req [2]. $ result ."
";
}
}
}
// Close the connection
@ Fclose ($ FP );
Return $ result_str;
}
?>

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.