简体   繁体   中英

PHP mail: Email field won't print

This is the first time I'm using PHP's mail function and have it working nicely except that I can't seem to get the user's email address to print to the submission form data that is emailed back to the form recipient.

Currently, when I get the form data, it looks like so (as you can see, the email field is empty):

在此输入图像描述

Here's my form code:

<form id="contact" action="process.php" method="post" role="form">
<!---snip to save space and show relevant code--->
<label for="email">Email</label>
    <input type="email" name="email" id="email" class="form-control input-lg" tabindex="3">

My process.php code:

<?php

$recipient = "johndoe@aol.com";
$subject = "FORM SUBMISSION";

$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$replyTo = $_POST['email'];
$sender = "From: $replyTo\r\n";
$message = $_POST['message'];
$whyContact = $_POST['whyContact'];

$sender = "MIME-Version: 1.0\n";
$sender = "Content-type: text/html; charset=us-ascii\n";

$msgBody = "
<html>
<head>
<title>title here</title>
</head>

<style type='text/css'>

body{width:100% !important; -webkit-text-size-adjust:100%; -ms-text-size-adjust:100%; margin:0; padding:0;}
table td {border-collapse: collapse;}
table { border-collapse:collapse; mso-table-lspace:0pt; mso-table-rspace:0pt; }

</style>

<body>

<table cellpadding='0' cellspacing='0' border='0' style='width:100%;border-bottom:1px solid #eee;font-size:12px;line-height:135%;font-family:'Lucida Grande','Lucida Sans Unicode', Tahoma, sans-serif'>
<tr style='background-color:#F5F5F5'>
<th style='vertical-align:top;color:#222;text-align:left;padding:7px 9px 7px 9px;border-top:1px solid #eee;'>Name:</th>
<td style='vertical-align:top;color:#333;width:60%;padding:7px 9px 7px 0;border-top:1px solid #eee;'>$firstName $lastName</td>
</tr>
<tr style='background-color:#FFFFFF'>
<th style='vertical-align:top;color:#222;text-align:left;padding:7px 9px 7px 9px;border-top:1px solid #eee;'>Email:</th>
<td style='vertical-align:top;color:#333;width:60%;padding:7px 9px 7px 0;border-top:1px solid #eee;'>$replyTo</td>
</tr>
<tr style='background-color:#F5F5F5'>
<th style='vertical-align:top;color:#222;text-align:left;padding:7px 9px 7px 9px;border-top:1px solid #eee;'>What are you contacting us about?</th>
<td style='vertical-align:top;color:#333;width:60%;padding:7px 9px 7px 0;border-top:1px solid #eee;'>$whyContact</td>
</tr>
<tr style='background-color:#FFFFFF'>
<th style='vertical-align:top;color:#222;text-align:left;padding:7px 9px 7px 9px;border-top:1px solid #eee;'>Message:</th>
<td style='vertical-align:top;color:#333;width:60%;padding:7px 9px 7px 0;border-top:1px solid #eee;'>$message</td>
</tr>
</table>
</body>
</html>
";

mail($recipient, $subject, $msgBody, $sender);
?>

You are overriding $sender with each assignment which means your From and MIME-Version headers are being dropped. Try Something like:

$sender  = "From: $replyTo\r\n";
$sender .= "MIME-Version: 1.0\n";
$sender .= "Content-type: text/html; charset=us-ascii\n";

I figured it out. I forgot to add the "." to the two $sender variables in my example. Thanks!

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