PHP Mail Send source code

Source: Internet
Author: User
Tags explode

Long time to write something ..... Recently, life is depressing .... For the life and tired, tidy up the example of e-mail sent, there are many online, I am also a good extract, test OK, home Mail class smtp_email_class.php as follows:
<?php
Class Email
{
function Send_mail ($to, $subject, $message, $from, $from _name, $mailformat =1)
{
if (Function_exists (' Mail '))
{

$headers = ' from: '. $from _name. ' < '. $from. ' > '. ' \ r \ n ";
$headers. = ' to: '. $to. " \ r \ n ";
if ($mailformat)
{$headers. = "content-type:text/html;\r\n";}
Else
{$headers. = "content-type:text/plain;\r\n";}
$headers. = "charset=gb2312\r\n\r\n";


$message = Str_replace ("\ R", "', $message);

[Email protected] ($to, Str_replace ("\ n", "", $subject), $message, $headers);

if (! $mail _return)
{
return $to. ' Send unsuccessful ';
}

return 1;
}
}

function Send_win32_mail ($to, $subject, $message, $from, $from _name, $host, $port, $mailformat =1)
{
Ini_set (' SMTP ', $host);
Ini_set (' Smtp_port ', $port);
Ini_set (' Sendmail_from ', $from);

$headers = ' from: '. $from _name. ' < '. $from. ' > '. ' \ r \ n ";
$headers. = ' to: '. $to. " \ r \ n ";
if ($mailformat)
{$headers. = "content-type:text/html;\r\n";}
Else
{$headers. = "content-type:text/plain;\r\n";}
$headers. = "charset=gb2312\r\n\r\n";

foreach (Explode (', ', $to) as $touser)
{
$touser = Trim ($touser);
if ($touser)
{
[Email protected] ($touser, $subject, $message, $headers);
if (! $mail _return)
{
return $touser. ' Send unsuccessful ';
}
}
}
return 1;
}

Send E_Mail via sock, no attachments supported,
//-------------------------------------------------------------------------------------------------------
function Email_sock ($host, $port, $errno, $errstr, $timeout, $auth, $user, $pass, $from)//constructors
{
$this->host = $host;
$this->port = $port;
$this->errno = $errno;
$this->errstr = $errstr;
$this->timeout = $timeout;
$this->auth = $auth;
$this->user = $user;
$this->pass = $pass;
$this->from = $from;
}

function Send_mail_sock ($subject, $message, $to, $from _name, $mailformat =0)//message header, message content, Inbox address, message format 1=text|0=html, default to 0
{
$host = $this->host;
$port = $this->port;
$errno = $this->errno;
$errstr = $this->errstr;
$timeout = $this->timeout;
$auth = $this->auth;
$user = $this->user;
$pass = $this->pass;
$from = $this->from;

/*
1. Create the sock and open the connection
2. Set to blocking mode
3. Test whether the SMTP answer code is 220,220 for mail Service readiness
4. Send user authentication, set by user
1=ehlo Host Domain \ r \ n
0=helo Host Domain \ r \ n
?. Read the server-side send back data to the client
The data sent by smtp.163.com is:
250-pipelining//pipelining command, which tells the client to send multiple commands at once to increase speed, here PHP
is not used because the PHP single file runs or is single-threaded
250-auth LOGIN PLAIN
250-auth=login PLAIN
8bitmime//get this line that is the SMTP server sends the end, waiting for the client to send commands
5. Send Auth Login command
6. Send User Name
7. Send Password
?. After the authentication has been successful,
8. Add from to the server
9. Add to the server
10. Send the data command and start typing the email to "." Number over
11. Write the contents of the message
12. Sending message content to an SMTP server
13. Send the QUIT command to end the session
*/
$fp = Fsockopen ($host, $port, $errno, $errstr, $timeout);//Open Sock network connection
if (! $fp) {return ' 1. The SMTP service is not set up ';}

Stream_set_blocking ($fp, true);//set to blocking mode, this mode will stop when the data is not read

$mail _return=fgets ($FP, 512);//read 512 bytes of content
if (substr ($mail _return, 0, 3)! = ' 220 ')
{return $host. ' -2. Return the answer code to '. substr ($mail _return, 0, 3);} Refer to the "SMTP protocol. txt" for the meaning of the return answer code.


Fputs ($fp, ($auth? ' EHLO ': ' HELO '). " ". $host." \ r \ n ");//server identity user Identity 1 = identity of authentication, 0 = Identity that does not require authentication
$mail _return = fgets ($FP, 512);
if (substr ($mail _return, 0, 3)! = && substr ($mail _return, 0, 3)! = 250)
{return $host. ' -3. Return the answer code to '. substr ($mail _return, 0, 3);}

while (true)
{
$mail _return = fgets ($FP, 512);
if (substr ($mail _return, 3, 1)! = '-' | | empty ($mail _return))
{break;}
}


if ($auth)
{
Fputs ($fp, "AUTH login\r\n");
$mail _return = fgets ($FP, 512);
if (substr ($mail _return, 0, 3)! = 334)
{return $host. ' -5. Return the answer code to '. substr ($mail _return, 0, 3);}

Fputs ($fp, Base64_encode ($user). " \ r \ n ");
$mail _return = fgets ($FP, 512);
if (substr ($mail _return, 0, 3)! = 334)
{return $host. ' -6. Return the answer code to '. substr ($mail _return, 0, 3). ' User= '. $user;}

Fputs ($fp, Base64_encode ($pass). " \ r \ n ");
$mail _return=fgets ($FP, 512);
if (substr ($mail _return, 0, 3)! = 235)
{return $host. ' -7. User authentication failed with answer code '. substr ($mail _return, 0, 3);}
}

Adding from and to the server
//------------------------------------------------------------------------------------------------------------- -----------
Fputs ($fp, "MAIL from:". $from. " \ r \ n ");//There are two formats, mail From:[email protected] and mail from: <[email protected]>
$mail _return = fgets ($FP, 512);
if (substr ($mail _return, 0, 3)! = 250)
{
Fputs ($fp, "MAIL from: <" $from. " >\r\n ");
$mail _return = fgets ($FP, 512);
if (substr ($mail _return, 0, 3)! = 250)
{return $host. ' -8. Return the answer code to '. substr ($mail _return, 0, 3);}
}

foreach (Explode (', ', $to) as $mailto)
{
$mailto = Trim ($mailto);
if ($mailto)
{
Fputs ($fp, "RCPT to:" $mailto. " \ r \ n ");
$mail _return = fgets ($FP, 512);
if (substr ($mail _return, 0, 3)! = 250)
{
Fputs ($fp, "RCPT to: <". $mailto. " >\r\n ");
$mail _return = fgets ($FP, 512);
if (substr ($mail _return, 0, 3)! = 250)
{return $host. ' -9. Return the answer code to '. substr ($mail _return, 0, 3);}
}
}

}
//------------------------------------------------------------------------------------------------------------- -----------
Fputs ($fp, "data\r\n");//start typing the email data to "." Number over
$mail _return = fgets ($FP, 512);
if (substr ($mail _return, 0, 3)! = 354)
{return $host. ' -10. Return the answer code to '. substr ($mail _return, 0, 3);}

Message content
//-----------------------------------------------------------
$mail _message = "From:". $from _name. ' < '. $from. " >\r\n ";
$mail _message. = "To:" $to. " \ r \ n ";
$mail _message. = "Subject:". Str_replace ("\ n", "', $subject)." \ r \ n ";
if ($mailformat ==1)
{$mail _message. = "content-type:text/html;\r\n";}
Else
{$mail _message. = "content-type:text/plain;\r\n";}
$mail _message. = "charset=gb2312\r\n\r\n";
$mail _message. = $message;
$mail _message. = "\r\n.\r\n";
//-----------------------------------------------------------

Fputs ($fp, $mail _message);
Fputs ($fp, "quit\r\n");

return 1;
}
}

?>


Use method, new send.php, the source code is as follows
<?php
//*
Include (' smtp_email_class.php ');
Define the parameters------------------------------------------------------
$host = ' smtp.163.com ';//smtp server address, I can be 163 here, depending on the situation
$from = ' [email protected] ';//own e-mail address
$port = 25;//Port
$errno = 0;//Error return number
$ERRSTR = ";//Error return content
$timeout = 10;//System Run timeout
$auth = 1;//requires auth LOGIN authentication, 1 = yes, 0 = no
$user = ' your mail user name ';//smtp Server user name
$pass = ' Your email password ';//SMTP server password
$from _name= ' your name ';//Contact
//---------------------------------------------------------------
$send _mode=2;
$em =new Email ();//Use Class
if ($send _mode==1) {
Send mail using PHP's own supported mail functions, which require SMTP service support
if ($em->send_mail (' Send to someone (Zhang San) email address ', ' title ', ' content ', ' [email protected] ', ' own name/contact name ', 0)} {
Echo ("Mail sent successfully ....");
}
}elseif ($send _mode==2) {
$em->email_sock ($host, $port, $errno, $errstr, $timeout, $auth, $user, $pass, $from);
if ($em->send_mail_sock (' title ', ' Welcome to Harmony ', ' Send to someone (Zhang San) ' E-mail address ', ' Your name ', 0)) {
$em->send_mail_sock (' title ', ' content ', ' [email protected] ', ' Contact name ', 0)
Echo ("Message sent successfully ....");
}
}

?>

PHP Mail Send source code

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.