0
votes

I have searched for this answer on Google and Youtube but have not found an answer.

I have created a very simple contact form on my website.

(http://www.torontoimc.com/contact)

I have attached a seperate PHP file to process the contact form elements.

When someone sends me an inquiry through the form on my site, I'm receiving all of the information except the person's email address input section info.

It's very important that I receive that email address otherwise I won't be able to reply to whom ever sent it to me.

I have tried setting the form to be sent to my gmail and outlook email but it just sends it as:

It just shows that the sender's email address as some random name "@hemi.websitewelcome.com"

I'm not sure if this is a server side issue or an issue with PHP file.

I have attached the code for both my Contact form and PHP file.

Here is the contact form code:

    <form action="formprocess.php" method="post" name="contact">
    <fieldset>

    <label>First Name</label>
    <input name="first_name" type="text"/>

    <label>Last Name</label>
    <input name="last_name" type="text"/>


    <label>Email</label>
    <input name="email_add" type="text"/>


    <label>Please write your inquiry below.</label>

    <textarea name="message" rows="15" cols="40">
    </textarea>

    <input type="submit" name="submit" id="submit" value="Submit"/>


    <input type="reset" name="reset" id="reset" value="Reset"/>


    </fieldset>

    </form>

Here is the php code:

    <?php

    $first_name = $_POST['first_name'];

    $last_name = $_POST['last_name'];

    $email_add = $_POST['email_add'];

    $message = $_POST['message'];

    $to = "[email protected]";

    $subject = "Inquiry for TorontoIMC";

    mail ($to, $subject, $message, "From: " . $first_name . $last_name . $email_add);

   header('Location: thanks.html');
   exit();

   ?>

I apologize if this is a repeat question but I'm a newbie to php and have not really been able to find an answer, if someone can please help me out, I would really appreciate it.

Thanks,

Latish Vohra

2
I'd suggest using a default email address as the From address, and including the submitted email address as part of the message. It means that you're not including user-submitted data as part of the header, for one thing. Also, some mail servers silently discard mails with a badly formed From line, which means if a user has a typo in their email address, the email won't get sent (and you'll never know)andrewsi

2 Answers

2
votes

Try this:

$headers = "From: $first_name $last_name <$email_add>\r\n";
mail ($to, $subject, $message, $headers );

I added "\r\n" to From

0
votes

I understand what you are trying to do on the FROM line, but you should use the "real" FROM sender. Many servers will not allow you to use a FROM line from a domain that is not the host's (think about it, the truth is you are lying, the person that filled the form did not actually send that email).

Secondly, follow user4035's way of styling the email in the FROM, if you are going to use a name last name <email>, it may not only be the way to do it for normal practice, also in the way that you wrote it, joining the fields without spaces, you might end up with a FROM line such as this FrançoisMARTIN [email protected]which may contain illegal characters and/or spaces that may cause some servers to choke on or discard. Instead 'FROM: '. $first_name . ' '. $last_name. ' <'. $email_add. '>' will produce FROM: François Martin Mellier <[email protected]>. But again, you should use a real account from the same domain you are sending (maybe something like 'FROM: New enquiry <[email protected]>' ?). As it's been pointed out, include the email_add field in the body of the message or the subject line in order to receive it; those are the proper places.

Using the -f parameter like mti2935 pointed out is always a good idea, but only if that email coount belongs to the same domain as the server's.

As a side note, try to take errors into account, a minimum:

if (mail ($to, $subject, $message, $headers )) {
 header('Location: thanks.html');
} else {
 header('Location: error.html');
}

might go a long way.