F
F
Frostik122021-06-09 11:34:56
PHP
Frostik12, 2021-06-09 11:34:56

How to send an email to multiple recipients from a form?

<?php

var_dump($_POST);
header('Location: success');

    if ($_POST){

        function mime_header_encode($str, $data_charset, $send_charset) {
            if($data_charset != $send_charset)
            $str=iconv($data_charset,$send_charset.'//IGNORE',$str);
            return ('=?'.$send_charset.'?B?'.base64_encode($str).'?=');
        }

        class TEmail {
            public $from_email;
            public $from_name;
            public $to_email;
            public $to_name;
            public $subject;
            public $data_charset='UTF-8';
            public $send_charset='windows-1251';
            public $body='';
            public $type='text/html';
            function send(){
                $dc=$this->data_charset;
                $sc=$this->send_charset;
                $enc_to=mime_header_encode($this->to_name,$dc,$sc).' <'.$this->to_email.'>';
                $enc_subject=mime_header_encode($this->subject,$dc,$sc);
                $enc_from=mime_header_encode($this->from_name,$dc,$sc).' <'.$this->from_email.'>';
                $enc_body=$dc==$sc?$this->body:iconv($dc,$sc.'//IGNORE',$this->body);
                $headers='';
                $headers.="Mime-Version: 1.0\r\n";
                $headers.="Content-type: ".$this->type."; charset=".$sc."\r\n";
                $headers.="From: ".$enc_from."\r\n";
                return mail($enc_to,$enc_subject,$enc_body,$headers);
            }
        }



        $content = $_POST;


        $message = '<h1>Заявка с сайта Test:</h1>';

      
        $name = $_POST['name'];
        $phone = $_POST['phone'];
        $quant = $_POST['quant'];
        $form_theme = $_POST['theme'];
        if (isset($_POST['theme'])) {
            $message .= "<p>Тема письма - ".$form_theme.'</p>';
        }
        if (isset($_POST['name'])) {
        $message .= "<p>Имя - ".$name.'</p>';
        }
        if (isset($_POST['phone'])) {
            $message .= "<p>Телефон - ".$phone.'</p>';
        }
       
        if (isset($_POST['quant'])) {
            $message .= "<p>Количество - ".$quant.'</p>';
        }


        $emailgo= new TEmail;
        $emailgo->from_email= 'Test';
        $emailgo->from_name= 'Test';
        $emailgo->to_email= '[email protected]'; 
        $emailgo->to_name= 'Test';
        $emailgo->subject= 'Заявка с сайта Test:';
        $emailgo->body= $message; 
        $emailgo->send();
        
        echo $message; //для debug

        Header('Location: success');
    }
    else{
        echo 'access denial';

    }
?>


Now everything is working fine, sending letters. Help, how to send a letter to several recipients, I just tried to specify it with a comma, it didn’t work

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
nokimaro, 2021-06-09
@nokimaro

$emailgo= new TEmail;
$emailgo->from_email= 'Test';
$emailgo->from_name= 'Test';
$emailgo->to_name= 'Test';
$emailgo->subject= 'Заявка с сайта Test:';
$emailgo->body= $message; 

foreach([
  '[email protected]', 
  '[email protected]', 
  '[email protected]',
  '[email protected]',
] as $to_email)    
{
  $emailgo->to_email= $to_email 
  $emailgo->send();
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question