Three kinds of php send mail instance program

Source: Internet
Author: User
Tags auth base64 explode php send mail server port

if (Send_mail (' test@qq.com ', ' mail subject ', ' message content ') = = "") {
echo "Send success!" <br> ";
} else{
echo "Send failed! <br> ";
}

<?php
$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);
?>

<?php
function Send_mail ($to, $subject = ' No subject ', $body) {
$loc _host = "im286"; The computer name of the letter, can be arbitrary
$SMTP _ACC = "test@126.com"; SMTP Authenticated user name
$smtp _pass= "Hu Jintao"; SMTP authenticated password, generally equivalent to POP3 password
$SMTP _host= "smtp.126.com"; SMTP server address, similar to smtp.tom.com
$from = "test@126.com"; Email address of sender, your letter-mail address
$headers = "Content-type:text/plain; charset= "Utf-8" content-transfer-encoding:base64 ";
$lb = ""; 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 "<b>Error:</b> cannot conect to". $smtp _host. " <br> ";
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. " <br> ";
}
}
}
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.