Code for using SMTP to send mail under PHP _php tutorial

Source: Internet
Author: User
Tags pear
A recent project requires SMTP to send mail, the previous library class does not exist, and does not like to install pear or use the Pear net/smtp class, feeling too complex. Directly from the Discuz to extract the core slightly modified under.
From the Protocol analysis network, find the command and answer to the SMTP protocol, the SMTP protocol between sending SMTP and receiving SMTP is done by sending SMTP commands and receiving SMTP feedback. The common commands are as follows:
Hello <domain> <CRLF> A Hello command to identify the sender to receive SMTP
MAIL from: <reverse-path> <CRLF> <reverse-path> is the sender address. This command tells the receiver the start of a new message send and initializes all states and buffers. This command starts a message transfer process and eventually completes the delivery of the message data to one or more mailboxes.
RCPT to: <forward-path> <CRLF> <forward-path> Identifies the address of each recipient of the message
DATA <CRLF>
Receiving SMTP will treat the subsequent behavior as the message data, with <CRLF>. <CRLF> identifying the end of the data.
REST <CRLF> to exit/reset the current message transfer
NOOP <CRLF> requires receiving SMTP only for OK answers. (For testing)
The QUIT <CRLF> requires receiving SMTP to return an OK answer and turn off the transport.
VRFY <string> <CRLF> verifies that the specified mailbox exists and that the server prohibits this command because of security factors.
EXPN <string> <CRLF> verifies that a given list of mailboxes exists, expands the list of mailboxes, and is often disabled.
Help <CRLF> what commands the query server supports

Note: <CRLF> is carriage return, line wrapping, ASCII codes are 13, 10 (decimal), respectively.

Alternatively, you can use Telnet for a simple manual SMTP operation under command.
Like what:


Telnet smtp.263.net 25
Trying 211.150.96.25 ...
Connected to Smtp.263.net.
Escape character is ' ^] '.
Welcome to Coremail System (with anti-spam) 2.1 for 263 (040326)
HELO weiqiong@cctk.net
Smtp.263.net
Mail from:weiqiong@cctk.net
Ok
RCPT To:g2_t1@263.net
Ok
Data
354 END Data with .
haha
.
Ok:queued as b9e452ff3e
Quit
221 Bye
Connection closed by foreign host.



On this basis, you can write a simple SMTP class.

Class stmp{

Private $mailcfg =array ();
Private $error _msg= ';

function __construct ($mailcfg) {

$this->mailcfg= $mailcfg;

}

Public function Send ($mail) {
$mailcfg = $this->mailcfg;
if (! $fp = Fsockopen ($mailcfg [' Server '], $mailcfg [' Port '], $errno, $ERRSTR, 30)} {
return $this->error ("($mailcfg [Server]: $mailcfg [port]) connect-unable to CONNECT to the SMTP server, please check your R \ "Mail_config.php\". ");
}
Stream_set_blocking ($fp, true);
$lastmessage = fgets ($FP, 512);
if (substr ($lastmessage, 0, 3)! = ' 220 ') {
return $this->error ("$mailcfg [Server]: $mailcfg [port] connect-$lastmessage");
}
Fputs ($FP, ($mailcfg [' auth ']? ' EHLO ': ' HELO '). " ". $mailcfg [' Auth_username ']." \ r \ n ");
$lastmessage = fgets ($FP, 512);
if (substr ($lastmessage, 0, 3)! = && substr ($lastmessage, 0, 3)! = 250) {
return $this->error ("($mailcfg [Server]: $mailcfg [port]) helo/ehlo-$lastmessage");
}
while (1) {
if (substr ($lastmessage, 3, 1)! = '-' | | empty ($lastmessage)) {
Break
}
$lastmessage = fgets ($FP, 512);
}
if ($mailcfg [' auth ']) {
Fputs ($fp, "AUTH login\r\n");
$lastmessage = fgets ($FP, 512);
if (substr ($lastmessage, 0, 3)! = 334) {
return $this->error ("($mailcfg [Server]: $mailcfg [port]) AUTH login-$lastmessage");
}
Fputs ($fp, Base64_encode ($mailcfg [' auth_username ']). " \ r \ n ");
$lastmessage = fgets ($FP, 512);
if (substr ($lastmessage, 0, 3)! = 334) {
return $this->error ("($mailcfg [Server]: $mailcfg [port]) username-$lastmessage");
}

Fputs ($fp, Base64_encode ($mailcfg [' Auth_password ']). " \ r \ n ");
$lastmessage = fgets ($FP, 512);
if (substr ($lastmessage, 0, 3)! = 235) {
return $this->error ("($mailcfg [Server]: $mailcfg [port]) password-$lastmessage");
}

$email _from = $mailcfg [' from '];
}
Fputs ($fp, "MAIL from: <". Preg_replace ("/.*\< (. +?) \>.*/"," \\1 ", $email _from)." >\r\n ");
$lastmessage = fgets ($FP, 512);
if (substr ($lastmessage, 0, 3)! = 250) {
Fputs ($fp, "MAIL from: <". Preg_replace ("/.*\< (. +?) \>.*/"," \\1 ", $email _from)." >\r\n ");
$lastmessage = fgets ($FP, 512);
if (substr ($lastmessage, 0, 3)! = 250) {
return $this->error ("($mailcfg [Server]: $mailcfg [port]) MAIL from-$lastmessage");
}
}

$email _to= $mail [' to '];
foreach (Explode (', ', $email _to) as $touser) {
$touser = Trim ($touser);
if ($touser) {
Fputs ($fp, "RCPT to: < $touser >\r\n");
$lastmessage = fgets ($FP, 512);
if (substr ($lastmessage, 0, 3)! = 250) {
Fputs ($fp, "RCPT to: < $touser >\r\n");
$lastmessage = fgets ($FP, 512);
return $this->error ("($mailcfg [Server]: $mailcfg [port]) RCPT to-$lastmessage");
}
}
}
Fputs ($fp, "data\r\n");
$lastmessage = fgets ($FP, 512);
if (substr ($lastmessage, 0, 3)! = 354) {
return $this->error ("($mailcfg [Server]: $mailcfg [port]) data-$lastmessage");
}
$str = "To: $email _to\r\nfrom: $email _from\r\nsubject:". $mail [' Subject ']. " \r\n\r\n ". $mail [' content ']." \r\n.\r\n ";
Fputs ($fp, $STR);
Fputs ($fp, "quit\r\n");
return true;
}

Public Function Get_error () {
return $this->error_msg;
}

Private Function Error ($MSG) {
$this->error_msg.= $msg;
return false;
}

}
?>

A simple invocation example:

$mailcfg [' server '] = ' smtp.163.com ';

$mailcfg [' port '] = ' 25 ';

$mailcfg [' auth '] = 1;
$mailcfg [' from '] = ' test ';

$mailcfg [' auth_username '] = ' test ';

$mailcfg [' auth_password '] = ' password ';
$stmp =new stmp ($mailcfg);
$mail =array (' to ' + ' test@gmail.com ', ' subject ' = ' Test title ', ' content ' = ' content ' = ' e-mail contents ' PHP Object-oriented ');
if (! $stmp->send ($mail)) {
echo $stmp->get_error ();
}else{
Echo ' Mail succ! ';
}
?>

If sent successfully, you can go to the mailbox to view the mail. ^_^

http://www.bkjia.com/phpjc/318510.html www.bkjia.com true http://www.bkjia.com/phpjc/318510.html techarticle

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