Use PHP through the socket to send the source code, support letter authentication _php Foundation

Source: Internet
Author: User
Tags auth base64 explode server port

A small program that uses PHP to open the socket port directly for a letter
The feature is the use of SMTP authentication, can better use to meet the needs of general users to build stations.

The entire source code attached to this article, the same content can be downloaded to the following address.
Reprint please indicate the source.

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

/******************************************/
/* PHP Webmail Sender * *
* www.ebugs.org * *
* Copyright millionaire in im286.com 2005.03.07 * *
/* Reprint Please indicate the source * *
/******************************************/

Before use, please follow the prompts to configure the authenticated username, password and SMTP server address, sender's mailbox, and so on.
Note the # replaced by #, outdated (www.im286.com) negative Weng Copyright, free to use, but retain copyright information

Example
echo send_mail (' fuweng#im286.com ', ' letter title ', ' letter content ');


function Send_mail ($to, $subject = ' No subject ', $body) {
$loc _host = "im286"; The computer name of the letter, can be arbitrary
$SMTP _ACC = "fuweng#im286.com"; SMTP authenticated username, similar to fuweng@im286.com, or Fuweng
$SMTP _pass= "Password"; SMTP authenticated password, generally equivalent to POP3 password
$SMTP _host= "smtp.im286.com"; SMTP server address, similar to smtp.tom.com
$from = "fuweng#im286.com"; Email address of sender, your letter-mail address
$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)); After parsing the body

$SMTP = Array (
1, EHLO, look 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 after BASE64 encoded username, look forward to return 334
Array (Base64_encode ($smtp _acc). $lb, "334", "Authentification Error:"),
4, send the password after BASE64 encoded, look forward to return 235
Array (Base64_encode ($smtp _pass). $lb, "235", "Authentification Error:");
5, send mail from, look forward to return 250
$SMTP [] = Array ("MAIL from: <". $from. " > ". $lb," A "," 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, send the letter subject
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, "The", "DATA (end) Error:");
10, send quit, exit, 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= "";
Send commands/Data in an SMTP array
foreach ($smtp as $req) {
Send a message
@fputs ($fp, $req [0]);
If a receive server returns information is required, the
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);
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.