A
A
Alexey Vesnin2017-10-09 15:18:52
Email
Alexey Vesnin, 2017-10-09 15:18:52

Sending PHP emails, what am I doing wrong?

Hello! Tell me what am I doing wrong?
I want to write one universal class for sending letters to mail, to telegrams, and then to CRM, but for now the task should be sent only to mail. This class should be able to accept all forms at once. By default, everything should work something like this:
1. Not all fields are required
2. Check if something has come in $_POST or not
3. If it has come, we write it to properties, by default properties = null, since not all of them can be filled.
4. The data that came is written to the properties of the class.
5. Then, in turn, we will call the methods for sending to mail, telegrams, CRM.
---- On the site with forms, I connect like this: or send via AJAX:
<?php require_once('mail.php'); ?>

$(document).ready(function () {
    $("form").submit(function () {
        // Получение ID формы
        var formID = $(this).attr('id');
        // Добавление решётки к имени ID
        var formNm = $('#' + formID);
        var message = $(formNm).find(".msgs"); // Ищет класс .msgs в текущей форме  и записываем в переменную
        var formTitle = $(formNm).find(".formTitle"); // Ищет класс .formtitle в текущей форме и записываем в переменную
        $.ajax({
            type: "POST",
            //url: 'modalform/mail.php',
            url: 'mail.php',
            data: formNm.serialize(),
            success: function (data) {
                // Вывод сообщения об успешной отправке
                message.html(data);
                formTitle.css("display","none");
                setTimeout(function(){
                    //$(formNm).css("display","block");
                    $('.formTitle').css("display","block");
                    $('.msgs').html('');
                    $('input').not(':input[type=submit], :input[type=hidden]').val('');
                }, 3000);
            },
            error: function (jqXHR, text, error) {
                // Вывод сообщения об ошибке отправки
                message.html(error);
                formTitle.css("display","none");
                // $(formNm).css("display","none");
                setTimeout(function(){
                    //$(formNm).css("display","block");
                    $('.formTitle').css("display","block");
                    $('.msgs').html('');
                    $('input').not(':input[type=submit], :input[type=hidden]').val('');
                }, 3000);
            }
        });
        return false;
    });
    //для стилей формы
    var $input = $('.form-fieldset > input');
    $input.blur(function (e) {
        $(this).toggleClass('filled', !!$(this).val());
    });
});

Class code:
<?php

class Mail {

    public $name = strip_tags($_POST['name']);
    public $phone = null;
    public $email = null;
    public $phone_email = null;
    public $website = null;
    public $message = null;
    public $formInfo = null;

    public function __construct(){

            if(!isset($_POST)) {

            $this->name =  strip_tags($_POST['name']);
            $this->phone =  strip_tags($_POST['phone']);
            $this->email =  strip_tags($_POST['email']);
            $this->phone_email =   strip_tags($_POST['phone_email']);
            $this->website =   strip_tags($_POST['website']);
            $this->message =   strip_tags($_POST['message']);
            $this->formInfo =   strip_tags($_POST['$formInfo']);
            $startmail = new Mail($_POST);
            $startmail->sendmail();
        }


    }


    public function sendmail () {

        $to = "[email protected]"; /*Укажите адрес, на который должно приходить письмо*/
        $sendfrom = "[email protected]"; /*Укажите адрес, с которого будет приходить письмо */
        $headers  = "From: " . strip_tags($sendfrom) . "\r\n";
        $headers .= "Reply-To: ". strip_tags($sendfrom) . "\r\n";
        $headers .= "MIME-Version: 1.0\r\n";
        $headers .= "Content-Type: text/html;charset=utf-8 \r\n";
        $headers .= "Content-Transfer-Encoding: 8bit \r\n";
        $subject = "$this->formInfo";
        $message = "$this->name
                    $this->phone
                    $this->email
                    $this->phone_email
                    $this->formInfo
                    $this->message
                    $this->website
                    ";

        $send = mail ($to, $subject, $message, $headers);

        if ($send){
            echo '<p class="fail">Сообщение успешно отправлено!</p>';
        } else {
            echo '<p class="fail">Ошибка. Вы заполнили не все обязательные поля!</p>';


    }







}

but something is wrong, it still doesn't work, what's wrong? why is the method not working?
$startmail->sendmail();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly, 2017-10-19
@Wohlstand

I recommend not using the mail() function at all, but using a direct SMTP socket connection instead. When I'm at work, I'll adjust the script example.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question