Code _php tips for using SMTP to send mail under PHP

Source: Internet
Author: User
Tags auth pear
A recent project needs to use SMTP to send mail, the previous library class does not exist, do not like to install pear or use Pear's net/smtp class, it is too complicated. The core is extracted directly from the Discuz and slightly modified.
From the Protocol analysis Web, find the commands and answers to the SMTP protocol, and the SMTP protocol's session between sending SMTP and receiving SMTP is done by sending SMTP commands and receiving SMTP feedback responses. The commonly used commands are as follows:
Hello <domain> <CRLF> Identifies the sender to a Hello command to receive SMTP
MAIL from: <reverse-path> <CRLF> <reverse-path> is the sender address. This command tells the receiver the beginning of a new mail delivery and initializes all States and buffers. This command begins a message transfer process that ultimately completes the delivery of the message data to one or more mailboxes.
RCPT to: <forward-path> <CRLF> <forward-path> Identify the addresses of individual message recipients
DATA <CRLF>
Receiving SMTP will treat the behavior that follows as mail data to <CRLF>. <CRLF> to identify the end of the data.
REST <CRLF> exit/Reset the current message transfer
NOOP <CRLF> requires the receive SMTP to do only the OK answer. (For testing)
QUIT <CRLF> requires that receive SMTP return an OK answer and turn off the transmission.
VRFY <string> <CRLF> verifies that the specified mailbox exists, and because of security factors, the server suppresses this command more than once.
EXPN <string> <CRLF> verifies that a given mailbox list exists, and that expanding the list of mailboxes is often prohibited.
Help <CRLF> What command does the query server support?

Note: The <CRLF> returns, wraps, and ASCII codes are 13, 10 (decimal), respectively.

Alternatively, you can use Telnet for simple manual use of SMTP 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 <CR><LF>.<CR><LF>
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 ($lastmessage, 0, 3)!= substr && 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;
}

}
?>

Simple Invocation Example:

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

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

$mailcfg [' auth '] = 1;
$mailcfg [' from '] = ' test <test@163.com> ';

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

$mailcfg [' auth_password '] = ' password ';
$stmp =new stmp ($mailcfg);
$mail =array (' to ' => ' test@gmail.com ', ' Subject ' => ' Test title ', ' content ' => ' message contents <a href= ') http:// Www.phpobject.net ">php Object Oriented </a>");
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. ^_^

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.