When using the mail () php function, it is not necessary to specify the From header, it is automatically substituted anyway, but if sent via phpmailer, the message is not sent without $ email -> From. Do not specify the From header is very convenient, because when transferring from a local server to a remote one, you do not need to change anything. How can I send a letter without From header to phpmailer so that it can be substituted? Mail sending code:

$email = new PHPMailer(); $email->CharSet = 'UTF-8'; $email->From = 'example@mail.ru'; if (!empty($name)) { $email->FromName = $name; } else { $email->FromName = 'Client'; } $email->Subject = 'Новая заявка'; $email->Body = $msg; $email->IsHTML(true); $email->AddAddress('example@mail.ru'); if ($_FILES['file_attach']) { $email->AddAttachment($_FILES['file_attach']['tmp_name'], $_FILES['file_attach']['name']); } if (!empty($_POST[$required_field])) { // отправка сообщения if ($email->Send()) { $result = 1; echo json_encode($result); } else { $result = 0; echo json_encode($result); } } else { $result = 0; echo json_encode($result); } 

I would be grateful for any help.

    1 answer 1

    For security purposes, the email domain in from must match the domain of the sender. And many hosters have this check. Otherwise, the letter does not go.

    Therefore, it would be more correct not to “not specify”, but to substitute the domain from which it is sent. Something like this:

     $email->From = 'noreply@'.$_SERVER['HTTP_HOST']; $email->Reply-to= 'mymail@gmailcom`; 

    In general, plug-ins should be used to submit forms. If only because they are thoroughly tested and in case of anything, they are promptly corrected (I mean, plug-ins from the office of normal manufacturers)

    see add. https://ru.wordpress.org/support/topic/%d1%83%d1%8f%d0%b7%d0%b2%d0%b8%d0%bc%d0%be%d1%81%d1%82 % d1% 8c-php-mailer / page / 2 /

    • But when sending via mail (), the header from is not necessary to specify, the domain itself is inserted into it when sending, can this be done in phpmailer? - Vladimir
    • I do not see a problem to get a domain ($ _SERVER ['HTTP_HOST'];) and substitute it in soap. 2 lines g. - SeVlad
    • if you substitute the domain, it does not send anything, it is necessary to specify the soap. But I have a completely different soap on my local server, and when I download it to a remote server, I need to change the soap. It seems not a significant job, but why complicate this life. I would like the header from itself to be substituted as in the mail () function. But phpmailer doesn’t send to without from - Vladimir
    • Once again: in order to GUARANTEED to send a letter from the script (it doesn’t matter how / what) you need to specify the From soap in the domain from which it is being poisoned. And for "your" soap there is a Reply-to header. You can write whatever you want. Shl, I added the answer code. - SeVlad
    • probably on my local server it does not work, because in the sendmail.ini file, smtp_server = smtp.mail.ru and u, auth_username and auth_password are set, my soap and password are specified and when I specify a value from the header from specified in auth_username, the letter is not sent. - Vladimir