简体   繁体   中英

PHP Mailer script Issue

I am a real noob when it comes to PHP but here is my problem -

the form "from address" reads as rittehqkxm@dedi96.cpt1.host-h.net, and This causes a) a delivery failure notice and b) one cannot click "reply" as the from address is not the person's address who completed the form.

Here is my code can anyone point out why this is happening and how to fix it?

thank you so much in advance

<?php
/* Set e-mail recipient */
$myemail = "info@ritter-accountants.co.za";

/* Check all form inputs using check_input function */
$name = check_input($_POST['name'], "Enter your name");
$subject = check_input($_POST['subject'], "Enter a subject");
$email = check_input($_POST['email']);
$message = check_input($_POST['message'], "Write your message");

/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}
/* Let's prepare the message for the e-mail */
$message = "

Name: $name
E-mail: $email
Subject: $subject

Message:
$message

";

/* Send the message using mail() function */
mail($myemail, $subject, $message);

/* Redirect visitor to the thank you page */
header('Location: thanks.html');
exit();

/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}

function show_error($myError)
{
?>
<html>
<body>

<p>Please correct the following error:</p>
<strong><?php echo $myError; ?></strong>
<p>Hit the back button and try again</p>

</body>
</html>
<?php
exit();
}
?>

And HTML

<form class="email" action="mailer.php" method="post">
                        <p align="center"><span class="style27"><strong>Name:</strong></span></p>
                        <div align="right"><span class="style21 link_title"><strong>
                        <input style="width:50%;" type="text" name="name" />
                        </strong></span>
                        </div>
                        <p align="center"><span class="style27"><strong>E-mail:</strong></span></p>
                        <div align="right"><span class="style21 link_title"><strong>
                        <input style="width:50%;" type="text" name="email" />
                        </strong></span>
                        </div>
                        <p align="center"><span class="style27"><strong>Subject:</strong></span></p>
                        <div align="right"><span class="style21 link_title"><strong>
                        <input style="width:50%;" type="text" name="subject" />
                        </strong></span>
                        </div>
                        <p align="center"><span class="style27"><strong>Message:         </strong></span></p>
                        <div align="right"><span class="style21 link_title"><strong>
                          <textarea style="width:50%;"= name="message"></textarea>
                        </p>
                        <input class="send" type="submit" value="Send">
                        </strong></span>
                        </div>
                      </form>

Your code does not set the sender email, so the default sendmail (or whatever is sending mails from your server) user is used for the sender header.

You can fix this by appending headers to your call to PHP's mail function, with the fourth parameter.

PHP mail documentation 's comments provides a straightforward example on how to accomplish this :

$headers   = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From: Sender Name <sender@domain.com>";
$headers[] = "Bcc: JJ Chong <bcc@domain2.com>";
$headers[] = "Reply-To: Recipient Name <receiver@domain3.com>";
$headers[] = "Subject: {$subject}";
$headers[] = "X-Mailer: PHP/".phpversion();

mail($to, $subject, $email, implode("\r\n", $headers));

I must add that, even if in your case you hard coded the recipient mail, it is not a good habit to set the address someone typed in your form as the sender address.

A good practice is to use a system address as sender and to fill the Reply-to header with the email provided via the form (The link above also describes this). This way you can see where the message really come from and reply easily to the person that actually talk to you.

In case where recipient is inputed via the form (eg. "Tell a friend" feature), it is mandatory to use a system address as the sender, since in some countries you may have legal issues if some X people sends an email to some Y one using Z's address as sender (indentity theft).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM