0

I receive emails from clients normally without issues, but I can't reply to the client email, the reply goes only to my emails. I did put reply to header and not working.

This is the code I am using:

<?php

$from = ' <mail@.com>';
$sendTo = ' <info@.com>';
$subject = 'New Mail From';
$fields = array('name' => 'Name', 'surname' => 'Surname', 'need' => 'Need', 'email' => 'Email', 'message' => 'Message'); 

$okMessage = 'Contact form successfully submitted. Thank you, I will get back to you soon!';
$errorMessage = 'There was an error while submitting the form. Please try again later';

error_reporting(E_ALL & ~E_NOTICE);

try
{
    if(count($_POST) == 0) throw new \Exception('Form is empty');

    foreach ($_POST as $key => $value) {
        if (isset($fields[$key])) {
            $emailText .= "$fields[$key]: $value\n";
        }
    }

    $headers = array('Content-Type: text/plain; charset="UTF-8";',
        'From: ' . $from,
        'Reply-To: ' . $headers,
        'Return-Path: ' . $from,
    );
    
    mail($sendTo, $subject, $emailText, implode("\n", $headers));

    $responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
    $responseArray = array('type' => 'danger', 'message' => $errorMessage);
}

if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    $encoded = json_encode($responseArray);

    header('Content-Type: application/json');

    echo $encoded;
}
else {
    echo $responseArray['message'];
}
El_Vanja
  • 2,622
  • 4
  • 13
  • 20
  • 3
    You have `'Reply-To: ' . $headers` inside `$headers = ...`. Seems like a mistake. Also, are `$from`, `$sendTo` and `$subject` really defined after they're used? If not, please indicate it somehow to make it more understandable. – El_Vanja Jan 04 '21 at 12:00
  • This code is running in your local computer or a server? Is SMTP configured? Here is some good examples of sending emails with PHP: https://stackoverflow.com/questions/14456673/sending-email-with-php-from-an-smtp-server – bpanatta Jan 04 '21 at 12:01
  • @bpanatta how is the location of the server relevant? OP says the emails are being received. The issue is with the reply to field specifically, and el_vanja already pointed out a problem with that. I do agree though that OP could study existing examples to see how to do this more easily. – ADyson Jan 04 '21 at 12:04
  • @ADyson receiving !== sending. He have problems with his code, which el_vanja already stated, but he could still have problems setting up the smtp for sending. One thing complements the other. – bpanatta Jan 04 '21 at 12:06
  • sorry it may look weird but i cant past all the code here so i did post it online on this link thanks a lot https://codeshare.io/2jzD3L – wissam baltaji Jan 04 '21 at 12:08
  • 1
    @bpanatta correct, receiving is not the same as sending. But logically it's trivial to deduce that if the email is successfully received then it must have been successfully sent – ADyson Jan 04 '21 at 12:10
  • @bpanatta please check this is the code https://codeshare.io/2jzD3L – wissam baltaji Jan 04 '21 at 12:15
  • @Wissam all relevant code must be shown here on the question itself, as per the site rules. There's no reason why what you've shared there could not be pasted on stackoverflow. El_vanja has done it for you – ADyson Jan 04 '21 at 13:16
  • Anyway the point still stands that `'Reply-To: ' . $headers,` makes no sense at all, because $headers doesn't exist at that point - you're only just defining it in the same statement. Replace that with an actual email address. – ADyson Jan 04 '21 at 13:28
  • replacing it by an actual email wont make any difference cause my problem is when i receive the email from the client i do receive the client email and its visible but if i press reply on the email it will show the contact form email which is mine and forwarded email whine is mine as well !! – wissam baltaji Jan 04 '21 at 17:18
  • Sorry I'm not following you now? You said the problem was when you hit reply it doesn't put the client's email address as the address to reply to? That would be fixed by setting a proper reply-to option in your code. The clients email may be visible in the body of the message, but that's irrelevant to whether it's set as the reply-to field. You need to set the client's email as the reply-to field instead of the non-existent $headers variable. I assume you can get it from one of the $_POST variables. – ADyson Jan 04 '21 at 23:24

1 Answers1

0

When you reply to an email, normally your email program will populate the "to" field of the reply email.

Normally it populates this with the value in the "from" field of the original email. However, this can be overridden if the headers of the original email contain a valid "reply-to" value. If that is set correctly, the email program will use the email address in the "reply-to" field as the "to" field of the reply.

However in your case this isn't happening because you have a logical error in the code:

'Reply-To: ' . $headers

makes no sense, because $headers doesn't exist at that time. You've written this inside the statement $headers = ...so headers is only just being defined. And even if it was defined at this moment, it wouldn't contain what's needed for a reply-to field.

You need to include an email address in that header. You could simply hard-code it, but it sounds like you want it to be set to the email address that the user submits in the form. You didn't say what that field is called, but presumably it comes from the POST values, therefore something like this would make sense:

'Reply-To: ' . $_POST["email"]

Just change the bit inside the [ ] depending on the exact name of the email field in your form.

(You might also want to check out this answer which contains equivalent code to send the mail via PHPMailer, which is easier to use and more robust than mail() - I highly recommend it.)

ADyson
  • 44,946
  • 12
  • 41
  • 55