S
S
Stanislas2016-05-30 23:33:19
PHP
Stanislas, 2016-05-30 23:33:19

How to teach the feedback form to take into account different e-mail domains?

The feedback form has stopped working. More precisely, if the domain after @ from 3 letters of the message comes. And if 2 letters, the message is not sent. So if you specify the sender's email, say [email protected] - messages are not sent, and if you specify [email protected] - messages arrive as expected.
Here is the handler code, help me edit it so that it works regardless of the number of domain letters.
handler file:

<?php

class Mail
 {
  const MAIL_HEADER_SEPARATOR = "\r\n"; // "\r\n" complies with RFC 2822 but might cause problems in some cases (see http://php.net/manual/en/function.mail.php)
  const MAIL_HEADER_TRANSFER_ENCODING = 'B'; // 'B' for Base64 or 'Q' for Quoted-Printable
  private $charset = 'utf-8';

  public function __construct()
   {
    mb_internal_encoding($this->charset);
   }

  public function set_charset($charset)
   {
    $this->charset = $charset;
    mb_internal_encoding($this->charset);
   }

  public function is_valid_email($email)
   {
    if(preg_match("/^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/", $email))
     {
      return true;
     }
    return false;
   }

  public function escape_display_name($display_name)
   {
    $display_name = str_replace('"', '\\"', $display_name);
    if(preg_match("/(\.|\;|\")/", $display_name))
     {
      return '"'.mb_encode_mimeheader($display_name, $this->charset, self::MAIL_HEADER_TRANSFER_ENCODING, self::MAIL_HEADER_SEPARATOR).'"';
     }
    else
     {
      return mb_encode_mimeheader($display_name, $this->charset, self::MAIL_HEADER_TRANSFER_ENCODING, self::MAIL_HEADER_SEPARATOR);
     }
   }

  public function make_address($display_name, $email)
   {
    return $this->escape_display_name($display_name).' <'.$email.'>';
   }

  private function mail_header_filter($string)
   {
    #return preg_replace("/(\015\012|\015|\012|content-transfer-encoding:|mime-version:|content-type:|subject:|to:|cc:|bcc:|from:|reply-to:)/ims", '', $string);
    #return preg_replace("/(\015\012|\015|\012|to:|cc:|bcc:|from:|reply-to:)/ims", '', $string);
    return preg_replace("/(\015\012|\015|\012)/", '', $string);
   }

  private function my_quoted_printable_encode($input, $line_max=76, $space_conv = false )
   {
    $hex = array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
    $lines = preg_split('/(?:\r\n|\r|\n)/', $input);
    $eol = "\n";
    $escape = '=';
    $output = '';
    while(list(, $line) = each($lines))
     {
      $linlen = strlen($line);
      $newline = '';
      for($i = 0; $i < $linlen; $i++)
       {
        $c = substr($line, $i, 1);
        $dec = ord( $c );
        if(($i == 0) && ($dec == 46)) // convert first point in the line into =2E
         { 
          $c = '=2E';
         }
        if($dec == 32)
         {
          if($i==($linlen-1)) // convert space at eol only
           {
            $c = '=20';
           }
          elseif($space_conv)
           {
            $c = '=20';
           }
          }
         elseif(($dec == 61) || ($dec < 32) || ($dec > 126)) // always encode "\t", which is *not* required
          { 
           $h2 = floor($dec/16);
           $h1 = floor($dec%16);
           $c = $escape.$hex[$h2].$hex[$h1];
          }
         if((strlen($newline) + strlen($c)) >= $line_max) // CRLF is not counted
          { 
           $output .= $newline.$escape.$eol; //  soft line break; " =\r\n" is okay
           $newline = '';
           if($dec == 46) // check if newline first character will be point or not
            {
             $c = '=2E';
            }
          }
         $newline .= $c;
       } // end of for
      $output .= $newline.$eol;
     } // end of while
    return $output;
   }

  public function send($to, $from, $subject, $message, $additional_parameters='')
   {
    $to = $this->mail_header_filter($to);
    $subject = mb_encode_mimeheader($this->mail_header_filter($subject), $this->charset, self::MAIL_HEADER_TRANSFER_ENCODING, self::MAIL_HEADER_SEPARATOR);
    $message = $this->my_quoted_printable_encode($message);

    $headers  = "From: " . $this->mail_header_filter($from) . self::MAIL_HEADER_SEPARATOR;
    $headers .= "MIME-Version: 1.0" . self::MAIL_HEADER_SEPARATOR;
    $headers .= "X-Sender-IP: ".$_SERVER["REMOTE_ADDR"] . self::MAIL_HEADER_SEPARATOR;
    #$headers .= "X-Mailer: " . BASE_URL . self::MAIL_HEADER_SEPARATOR;
    $headers .= "Content-Type: text/plain; charset=" . $this->charset . self::MAIL_HEADER_SEPARATOR;
    $headers .= "Content-Transfer-Encoding: quoted-printable";

    if($additional_parameters)
     {
      if(@mail($to, $subject, $message, $headers, $additional_parameters))
       {
        return true;
       }
      else
       {
        return false;
       }
     }
    else
     {
      if(@mail($to, $subject, $message, $headers))
       {
        return true;
       }
      else
       {
        return false;
       }
     }
   }
 }
?>

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
V
Vladimir Dubrovin, 2016-05-31
@z3apa3a

Mail stopped reaching because DMARC is enabled for the mail.ru domain, which protects letters from fakes:
https://habrahabr.ru/company/mailru/blog/282602/

A
Alexey, 2016-05-31
@alsopub

Maybe not "stopped sending", but "stopped reaching"?
Did you search in the spam folder?
Did you look at the mail logs?

O
Optimus, 2016-05-31
Pyan @marrk2

Line:
Change to:

if(preg_match("/[0-9a-zA-Z\.\-_]{1,}@[0-9a-zA-Z\-_]{1,}\.[0-9a-zA-Z\-_]{1,}[\.0-9a-zA-Z]{0,}/", $email))

S
Stanislas Werter, 2016-05-31
@furniture

It looks like the problem is on the side of mail.ru to see something there they set up so that the mail stopped coming. I sent a support ticket to the mail, waiting for an answer.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question