PHP functions for sending mail using sock technology _php tutorial

Source: Internet
Author: User
Tags server port
Use sock technology to send email without server support. Fast speed!!

Copy CodeThe code is as follows:
function Send_mail ($to, $subject, $body)
{
SMTP information can be modified to your,//////////
$loc _host = "Longbill"; The name of the sending computer, can be arbitrarily
$SMTP _ACC = "Longbill.mail"; The user name of the SMTP authentication,
$SMTP _pass = "Longbill"; The password for SMTP authentication,
$SMTP _host = "smtp.163.com"; SMTP server address,
$from = "longbill.mail@163.com"; to correspond to SMTP information, otherwise it will fail
SMTP Information finished//////////////////////////////////
$headers = "Content-type:text/plain; Charset=\ "gb2312\" \r\ncontent-transfer-encoding:base64 ";
$lb = "\ r \ n"; LineBreak
$HDR = Explode ($lb, $headers); Parsed HDR
if ($body)
{
$bdy = Preg_replace ("/^\./", "..", Explode ($lb, $body));
}//the body after parsing
$SMTP = Array (
1, EHLO, looking forward to return 220 or 250
Array ("EHLO". $loc _host. $lb, "220,250", "HELO Error:"),
2, send Auth Login, look forward to return 334
Array ("AUTH LOGIN". $lb, "334", "AUTH Error:"),
3, send the BASE64 encoded user name, expect to return 334
Array (Base64_encode ($smtp _acc). $lb, "334", "Authentification Error:"),
4, send the password encoded by BASE64, expect to return 235
Array (Base64_encode ($smtp _pass). $lb, "235", "Authentification Error:"));
5. Send mail from, expecting to return 250
$SMTP [] = Array ("MAIL from: <" $from. " > ". $lb," "," "MAIL from Error:");
6, send rcpt to. Expect to return 250
$SMTP [] = Array ("RCPT to: <" $to. " > ". $lb," "," RCPT to Error: ");
7, send data, look forward to return 354
$SMTP [] = Array ("Data". $LB, "354", "Data error:");
8.0. Send from
$SMTP [] = Array ("From:". $from. $lb, "", "");
8.2. Send To
$SMTP [] = Array ("To:". $to. $lb, "", "");
8.1, send the title
$SMTP [] = Array ("Subject:". $subject. $lb, "", "");
8.3. Send Other header content
foreach ($hdr as $h) {$SMTP [] = Array ($h. $lb, "", "");}
8.4, send a blank line, end header send
$SMTP [] = Array ($lb, "", "");
8.5. Sending the subject of the letter
if ($bdy)
{
foreach ($bdy as $b)
{
$SMTP [] = Array (Base64_encode ($b. $lb). $lb, "", "" ");
}
}
9, send "." Indicates the end of the letter, expecting to return 250
$SMTP [] = Array (".". $LB, "+", "DATA (end) Error:");
10, send quit, quit, look forward 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= "";
Sending commands/Data in an SMTP array
foreach ($smtp as $req)
{
Send Message
@fputs ($fp, $req [0]);
If you need to receive server return information,
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 connection
@fclose ($FP);
if ($result _str== "") {return "sent successfully!!";} else {return $result _str;}
}

http://www.bkjia.com/PHPjc/318466.html www.bkjia.com true http://www.bkjia.com/PHPjc/318466.html techarticle use sock technology to send email without server support. Fast speed!! Copy the code code as follows: Functionsend_mail ($to, $subject, $body) {/////////////SMTP information, can be modified to your, ...

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