Three php mail sending instance programs-PHP source code

Source: Internet
Author: User
We have used three php mail sending example programs. Below we will use various mail sending examples one by one. We have used three php mail sending example programs. Below we will use various mail sending examples one by one.

Script ec (2); script

If (send_mail ('test @ qq.com ', 'email topic', 'email content') = ""){
Echo "sent successfully!
";
} Else {
Echo "failed to send!
";
}

$ To = "test@test.com ";
$ Subject = "Test Subject ";
$ Message = "This is the test email .";
$ From = "youremail@yourdomain.com ";
$ Headers = "From: $ from ";
Mail ($ to, $ subject, $ message, $ headers );
?>

Function send_mail ($ to, $ subject = 'no subobject', $ body ){
$ Loc_host = "im286"; // The Name Of The email sending computer.
$ Smtp_acc = "test@126.com"; // Smtp authenticated user name
$ Smtp_pass = "******"; // password for Smtp authentication, which is generally equivalent to a pop3 PASSWORD
$ Smtp_host = "smtp.126.com"; // SMTP server address, similar to smtp.tom.com
$ From = "test@126.com"; // Email address of the sender, your mail address
$ Headers = "Content-Type: text/plain; charset =" UTF-8 "Content-Transfer-Encoding: base64 ";
$ Lb = ""; // 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;
}
?>

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.