Code for automatically sending emails (php code for sending emails)

Source: Internet
Author: User
Tags auth ereg explode php code zip email account server port
The code is as follows: Copy code

Class smtp
{
/* Public variables */
Var $ smtp_port;
Var $ time_out;
Var $ host_name;
Var $ log_file;
Var $ relay_host;
Var $ debug;
Var $ auth;
Var $ user;
Var $ pass;

/* Private variables */
Var $ sock;

/* Constractor */
Function smtp ($ relay_host = "", $ smtp_port = 25, $ auth = false, $ user, $ pass)
{
$ This-> debug = false;
$ This-> smtp_port = $ smtp_port;
$ This-> relay_host = $ relay_host;
$ This-> time_out = 30; // is used in fsockopen ()
#
$ This-> auth = $ auth; // auth
$ This-> user = $ user;
$ This-> pass = $ pass;
#
$ This-> host_name = "localhost"; // is used in helo command
$ This-> log_file = "";

$ This-> sock = false;
}

/* Main function */
Function sendmail ($ to, $ from, $ subject = "", $ body = "", $ mailtype, $ cc = "", $ bcc = "", $ additional_headers = "")
{
$ Mail_from = $ this-> get_address ($ this-> strip_comment ($ from ));
$ Body = ereg_replace ("(^ | () (\.)", "\ 1. \ 3", $ body );
$ Header. = "maid: 1.0 ";
If ($ mailtype = "html "){
$ Header. = "content-type: text/html ";
}
$ Header. = "to:". $ ."";
If ($ cc! = ""){
$ Header. = "cc:". $ cc ."";
}
$ Header. = "from: $ from <". $ from. "> ";
$ Header. = "subject:". $ subject ."";
$ Header. = $ additional_headers;
$ Header. = "date:". date ("r ")."";
$ Header. = "x-mailer: by redhat (php/". phpversion ().")";
List ($ msec, $ sec) = explode ("", microtime ());
$ Header. = "message-id: <". date ("ymdhis", $ sec ). ". ". ($ msec * 1000000 ). ". ". $ mail_from. "> ";
$ To = explode (",", $ this-> strip_comment ($ ));

If ($ cc! = ""){
$ To = array_merge ($ to, explode (",", $ this-> strip_comment ($ cc )));
}

If ($ bcc! = ""){
$ To = array_merge ($ to, explode (",", $ this-> strip_comment ($ bcc )));
}

$ Sent = true;
Foreach ($ to as $ rcpt_to ){
$ Rcpt_to = $ this-> get_address ($ rcpt_to );
If (! $ This-> smtp_sockopen ($ rcpt_to )){
$ This-> log_write ("error: cannot send email to". $ rcpt_to ."");
$ Sent = false;
Continue;
}
If ($ this-> smtp_send ($ this-> host_name, $ mail_from, $ rcpt_to, $ header, $ body )){
$ This-> log_write ("e-mail has been sent to <". $ rcpt_to. "> ");
} Else {
$ This-> log_write ("error: cannot send email to <". $ rcpt_to. "> ");
$ Sent = false;
}
Fclose ($ this-> sock );
$ This-> log_write ("disconnected from remote host ");
}
Echo "<br> ";
// Echo $ header;
Return $ sent;
}

/* Private functions */

Function smtp_send ($ helo, $ from, $ to, $ header, $ body = "")
{
If (! $ This-> smtp_putcmd ("helo", $ helo )){
Return $ this-> smtp_error ("sending helo command ");
}
# Auth
If ($ this-> auth ){
If (! $ This-> smtp_putcmd ("auth login", base64_encode ($ this-> user ))){
Return $ this-> smtp_error ("sending helo command ");
}

If (! $ This-> smtp_putcmd ("", base64_encode ($ this-> pass ))){
Return $ this-> smtp_error ("sending helo command ");
}
}
#
If (! $ This-> smtp_putcmd ("mail", "from: <". $ from. "> ")){
Return $ this-> smtp_error ("sending mail from command ");
}

If (! $ This-> smtp_putcmd ("rcpt", "to: <". $ to. "> ")){
Return $ this-> smtp_error ("sending rcpt to command ");
}

If (! $ This-> smtp_putcmd ("data ")){
Return $ this-> smtp_error ("sending data command ");
}

If (! $ This-> smtp_message ($ header, $ body )){
Return $ this-> smtp_error ("sending message ");
}

If (! $ This-> smtp_eom ()){
Return $ this-> smtp_error ("sending <cr> <lf>. <cr> <lf> [eom]");
}

If (! $ This-> smtp_putcmd ("quit ")){
Return $ this-> smtp_error ("sending quit command ");
}

Return true;
}

Function smtp_sockopen ($ address)
{
If ($ this-> relay_host = ""){
Return $ this-> smtp_sockopen_mx ($ address );
} Else {
Return $ this-> smtp_sockopen_relay ();
}
}

Function smtp_sockopen_relay ()
{
$ This-> log_write ("trying to". $ this-> relay_host. ":". $ this-> smtp_port ."");
$ This-> sock = @ fsockopen ($ this-> relay_host, $ this-> smtp_port, $ errno, $ errstr, $ this-> time_out );
If (! ($ This-> sock & $ this-> smtp_ OK ())){
$ This-> log_write ("error: cannot connenct to relay host". $ this-> relay_host ."");
$ This-> log_write ("error:". $ errstr. "(". $ errno .")");
Return false;
}
$ This-> log_write ("connected to relay host". $ this-> relay_host ."");
Return true ;;
}

Function smtp_sockopen_mx ($ address)
{
$ Domain = ereg_replace ("^. + @ ([^ @] +) $", "\ 1", $ address );
If (! @ Getmxrr ($ domain, $ mxhosts )){
$ This-> log_write ("error: cannot resolve mx" ". $ domain .""");
Return false;
}
Foreach ($ mxhosts as $ host ){
$ This-> log_write ("trying to". $ host. ":". $ this-> smtp_port ."");
$ This-> sock = @ fsockopen ($ host, $ this-> smtp_port, $ errno, $ errstr, $ this-> time_out );
If (! ($ This-> sock & $ this-> smtp_ OK ())){
$ This-> log_write ("warning: cannot connect to mx host". $ host ."");
$ This-> log_write ("error:". $ errstr. "(". $ errno .")");
Continue;
}
$ This-> log_write ("connected to mx host". $ host ."");
Return true;
}
$ This-> log_write ("error: cannot connect to any mx hosts (". implode (",", $ mxhosts ).")");
Return false;
}

Function smtp_message ($ header, $ body)
{
Fputs ($ this-> sock, $ header. "". $ body );
$ This-> smtp_debug (">". str_replace ("", "". ">", $ header. ">". $ body. "> "));

Return true;
}

Function smtp_eom ()
{
Fputs ($ this-> sock ,".");
$ This-> smtp_debug (". [eom]");

Return $ this-> smtp_ OK ();
}

Function smtp_ OK ()
{
$ Response = str_replace ("", "", fgets ($ this-> sock, 512 ));
$ This-> smtp_debug ($ response ."");

If (! Ereg ("^ [23]", $ response )){
Fputs ($ this-> sock, "quit ");
Fgets ($ this-> sock, 512 );
$ This-> log_write ("error: remote host returned" ". $ response .""");
Return false;
}
Return true;
}

Function smtp_putcmd ($ cmd, $ arg = "")
{
If ($ arg! = ""){
If ($ cmd = "") $ cmd = $ arg;
Else $ cmd = $ cmd. "". $ arg;
}

Fputs ($ this-> sock, $ cmd ."");
$ This-> smtp_debug (">". $ cmd ."");

Return $ this-> smtp_ OK ();
}

Function smtp_error ($ string)
{
$ This-> log_write ("error: error occurred while". $ string .".");
Return false;
}

Function log_write ($ message)
{
$ This-> smtp_debug ($ message );

If ($ this-> log_file = ""){
Return true;
}

$ Message = date ("m d h: I: s"). get_current_user (). "[". getmypid (). "]:". $ message;
If (! @ File_exists ($ this-> log_file) |! ($ Fp = @ fopen ($ this-> log_file, ""))){
$ This-> smtp_debug ("warning: cannot open log file" ". $ this-> log_file .""");
Return false;
}
Flock ($ fp, lock_ex );
Fputs ($ fp, $ message );
Fclose ($ fp );

Return true;
}

Function strip_comment ($ address)
{
$ Comment = "\ ([^ ()] * \)";
While (ereg ($ comment, $ address )){
$ Address = ereg_replace ($ comment, "", $ address );
}

Return $ address;
}

Function get_address ($ address)
{
$ Address = ereg_replace ("([]) +", "", $ address );
$ Address = ereg_replace ("^. * <(. +)>. * $", "\ 1", $ address );

Return $ address;
}

Function smtp_debug ($ message)
{
If ($ this-> debug ){
Echo $ message. "<br> ";
}
}

Function get_attach_type ($ image_tag ){//

$ Filedata = array ();

$ Img_file_con = fopen ($ image_tag, "r ");
Unset ($ image_data );
While ($ tem_buffer = addslashes (fread ($ img_file_con, filesize ($ image_tag ))))
$ Image_data. = $ tem_buffer;
Fclose ($ img_file_con );

$ Filedata ['context'] = $ image_data;
$ Filedata ['filename'] = basename ($ image_tag );
$ Extension = substr ($ image_tag, strrpos ($ image_tag, "."), strlen ($ image_tag)-strrpos ($ image_tag ,"."));
Switch ($ extension ){
Case ". gif ":
$ Filedata ['type'] = "image/gif ";
Break;
Case ". gz ":
$ Filedata ['type'] = "application/x-gzip ";
Break;
Case ". htm ":
$ Filedata ['type'] = "text/html ";
Break;
Case ". html ":
$ Filedata ['type'] = "text/html ";
Break;
Case ". jpg ":
$ Filedata ['type'] = "image/jpeg ";
Break;
Case ". tar ":
$ Filedata ['type'] = "application/x-tar ";
Break;
Case ". txt ":
$ Filedata ['type'] = "text/plain ";
Break;
Case ". zip ":
$ Filedata ['type'] = "application/zip ";
Break;
Default:
$ Filedata ['type'] = "application/octet-stream ";
Break;
}


Return $ filedata;
}

}

// Usage

The code is as follows: Copy code
$ Smtps tutorial erver = "smtp.163.com"; // smtp server
$ Smtpserverport = 25; // smtp server port
$ Smtpusermail = "enter your NetEase email account"; // user email address of the smtp server
$ Smtpemailto = $ rs ['email ']; // to whom
$ Smtpuser = "enter your NetEase email account name"; // User account of the smtp server
$ Smtppass = "enter your NetEase email password"; // smtp server user password
$ Mailsubject = "set new password"; // email subject
$ Mailbody = "{$ rs ['username']} Hello! Here is powered by your temporary new password: $ emailpwd ".
"Click the URL below to go to the new password setting page and complete the new password setting. ".
"<A href = 'http: // www.111cn.net '> set a new password </a>"; // mail content
$ Mailtype = "html"; // The email format (html/txt). txt is a text email.
######################################## ##
$ Smtp = new smtp ($ smtpserver, $ smtpserverport, true, $ smtpuser, $ smtppass); // Here, true indicates that authentication is used; otherwise, authentication is not used.
$ Smtp-> debug = false; // whether to display the sent debugging information
$ Smtp-> sendmail ($ smtpemailto, $ smtpusermail, $ mailsubject, $ mailbody, $ mailtype );
$ Prompt_msg-> p ('The system has sent an email, please check it! ', 'Index. Php', 'Back to homepage ');

?>

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.