Changing From field when sending an email through php form

958 Views Asked by At

I made a a php form where users can fill it up and when they press the submit button, it mails the feedback to my chosen email address. The only problem is when I put the 'from' email as [email protected], it doesn't work. The email will be sent but it will be sent from '[email protected]'.

After that I tested my gmail address under the 'from' header. This time it worked. The email's from header is '[email protected]'. I was wondering why does it work on gmail but not for my domain?

2

There are 2 best solutions below

0
On

It is not your problem. gmail smtp server does not allow the difference from email.

If you use your own smtp server, then u can see that's ok..

Remark:

if smtp account email and from email is not same, then email may be spammed.

You can use Reply-To email address for get reply mail or feedback mail.

0
On

sample mail code with all the functions(dummy content are there. Edit it with your variables and datas)

<?php

    $to = '[email protected]';
    $subject = 'Subject with name '.$c_name;

Method 01

Single line message

    $message = 'My message here';

Method 02

HTML(Tbale format) message

    $message = '<html><body>';
    $message .= '<table>';
    $message .= "<tr><td><strong>Name</strong> </td><td width='75%'>".strip_tags($_POST['name'])."</td></tr>";
    $message .= "<tr><td><strong>Email</strong> </td><td>" .strip_tags($_POST['email']) . "</td></tr>";
    $message .= "<tr><td><strong>Mobile</strong> </td><td>" .strip_tags($_POST['mob']) . "</td></tr>";
    $message .= "<tr><td><strong>Message</strong> </td><td>" . strip_tags($_POST['message']). "</td></tr>";
    $message .= "</table>";
    $message .= "</body></html>";
    $headers = "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

end of method 02

    $headers .= "Cc: [email protected]";//Cc email
    $headers .= "From: [email protected]";//From email

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

    if(!$result)
    {
        ?>
        <script type="text/javascript">
            alert("Mail sending Not Successful. Try again");
        </script>
    <?php
        }
        else
        {
    ?>
        <script type="text/javascript">
            alert("Thank You!");
        </script>
    <?php
        }
    }