Use php to send mail source code through Socket, support for mail authentication _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Use php to send the source code through Socket and support sending authentication. Using php to directly open the socket port for sending small programs is characterized by the use of smtp Authentication, better use to meet the general needs of Web site construction. The entire source code is included in this article, and the same content uses php to directly open the socket port for sending small programs
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: <". $ from. ">". $ lb, "250", "mail from error :");
// 6. send Rcpt. Expected 250
$ Smtp [] = array ("rcpt to: <". $ 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;
}
?>

Failover is characterized by the use of smtp authentication, which can better meet the general needs of Web site construction. The entire source code is included in this article, the same content...

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.