Code for sending mails via SMTP in php

Source: Internet
Author: User
The code for sending mails using SMTP in php the latest project needs to use SMTP to send mails. the previous library class does not exist and does not like to install pear or use the net/smtp class of pear, it is too complicated. The core is extracted from discuz and slightly modified.
Find the SMTP protocol commands and responses from the Protocol Analysis Network, the session between sending SMTP and receiving SMTP protocol is completed by sending SMTP commands and receiving SMTP feedback responses. Common commands are as follows:
HELLO <domain> <CRLF> identifies a HELLO command sent by the sender to receive SMTP
Mail from: <reverse-path> <CRLF> <reverse-path> is the sender address. This command informs the recipient of the start of sending a new mail and initializes all statuses and buffers. This command starts a mail transmission process and finally delivers mail data to one or more mailboxes.
Rcpt to: <forward-path> <CRLF> <forward-path> identifies the addresses of each email recipient
DATA <CRLF>
After receiving SMTP, the behavior is treated as mail data, ending with <CRLF>. <CRLF> identifying data.
REST <CRLF> exit/reset the current email transmission
NOOP <CRLF> requires that only OK responses are received for SMTP. (For testing)
QUIT <CRLF> requires receiving SMTP to return an OK response and disable transmission.
VRFY <string> <CRLF> verifies whether the specified email address exists. This command is not allowed on the server due to security reasons.
EXPN <string> <CRLF> verifies whether the specified email address list exists and expands the email address list.
HELP <CRLF> query commands supported by the server

Note: <CRLF> the carriage return and line feed are supported, and the ASCII code is 13 and 10 (decimal), respectively ).

In addition, you can use telnet to manually use SMTP in command.
For example:


Telnet smtp.263.net 25
Trying 211.150.96.25...
Connected to smtp.263.net.
Escape character is '^]'.
220 Welcome to coremail System (With Anti-Spam) 2.1 for 263 (040326)
HELO weiqiong@cctk.net
250 smtp.263.net
Mail from: weiqiong@cctk.net
250 OK
Rcpt to: g2_t1@263.net
250 OK
Data
354 End data .
Haha
.
250 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 \" mail_config.php \". ");
}
Stream_set_blocking ($ fp, true );
$ Lastmessage = fgets ($ fp, 512 );
If (substr ($ lastmessage, 0, 3 )! = '000000 '){
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 )! = 220 & 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 ['subobject']. "\ 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 call 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 ', 'subobject' => 'test title', 'content' => 'Mail content PHP object ');
If (! $ Stmp-> send ($ mail )){
Echo $ stmp-> get_error ();
} Else {
Echo 'mail succ! ';
}
?>

If the email is successfully sent, you can go to your mailbox to view the email. Pai_^

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.