Three ways to send mail code in PHP

Source: Internet
Author: User
Keywords Web Programming PHP Tutorials
Tags address array code content data echo error examples

We use three kinds of PHP tutorials to send mail instance program Oh, here are a variety of examples sent to send mail oh.

if (Send_mail (' test@qq.com ', ' mail subject ', ' message content ') = = "") {
Echo sent successfully! <br> ";
} else{
Echo sent 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 is optional
$SMTP _ACC = "test@126.com"; SMTP authenticated username
$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" rncontent-transfer-encoding:base64 ";
$LB = "RN"; LineBreak

$hdr =explode ($lb, $headers);//parsed HDR
if ($body) {$bdy = Preg_replace ("/^./", "...", Explode ($lb, $body));} Parsed body

$smtp = Array (
//1, EHLO, expecting to return 220 or the
Array ("EHLO"). $loc _host. $lb, "220,250", "HELO Error:"),
//2, send auth Login, expecting to return 334
Array ("AUTH LOGIN". $lb, "334", "AUTH Error:"),
//3, Send BASE64 encoded username and look forward to returning 334
Array (base64_ Encode ($smtp _ACC). $lb, "334", "Authentification Error:"),
//4, sending Base64 encoded password, expecting to return 235
Array ($ Smtp_pass). $lb, "235", "Authentification Error:");
//5, send mail from, expect to return to the $SMTP [] = Array ("MAIL from: <". $from. " > ". $lb," The "," MAIL from Error: ");
//6, send rcpt to. Look forward to returning the
$smtp [] = Array ("RCPT to: <". $to. " > $lb, "RCPT", "to Error:");
//7, sending DATA, expecting 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 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, knotThe bundle header sends the
$SMTP [] = Array ($lb, "", "");
//8.5, send letter Principal
if ($bdy) {foreach ($bdy as $b) {$SMTP [] = Array (Base64_encode ($b. $lb). $lb, "", "");}}
//9, send. Indicates the end of the letter, expecting to return the
$smtp [] = Array ("."). $LB, "The", "DATA (end) Error:");
//10, send QUIT, exit, expect 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> cant 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;
}
?>

Related Article

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.