This php file sends the completed Form fields info to $to, but does it also send an email to the $email of the person who completed the Form?
<?php
//check if form was sent
if($_POST){
$to = 's@hmail.com';
$subject = 'Form1';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$headers = $name;
$message .= "\r\n\r\n" . $name;
if( empty($_POST["some_place"]) or $_POST['some_place'] != "glory" )
{
header("HTTP/1.0 403 Forbidden");
}else{
mail( $to, $subject, $message, $email, $headers );
}
header('Location: https://.......com');
exit;
}
?>
SamA74
March 8, 2019, 8:55pm
2
No, you would need to add the $email
to the $to
parameter.
$to = 's@hmail.com, '.$_POST['email'];
Note the 4th parameter should be the headers.
http://php.net/manual/en/function.mail.php
This isn’t the best way to test:-
ChrisjChrisj:
if($_POST){
Better to use:-
if($_SERVER["REQUEST_METHOD"] == "POST") {
1 Like
Thanks for your reply/help. Much appreciated.
My latest issue, where I need help, is that when the email is received it shows the (from) email address to be my domain account user name @ the server name, like this:
domain1@host3servername.com , where I’d prefer something more like noReply@actualdomain.com
Any help or suggested remedy will be appreciated
You can specify the from-address inside the headers. As you don’t specify it in you code, something is adding a default value in. See here for an example on how to specify the from-address: http://php.net/manual/en/function.mail.php
You might also want to look at using something other than the mail()
function to send your messages, as I read on here that it is less than reliable. Two that are often recommended are PHPMailer and Swiftmailer, though I have not used either for more than a quick test message.
Thanks for your reply.
I have PHPMailer installed. Maybe you can help direct me to tie-in this example code to my Form and to the PHPMailer:
<?php
/**
* PHPMailer simple file upload and send example.
*/
//Import the PHPMailer class into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
$msg = '';
if (array_key_exists('userfile', $_FILES)) {
// First handle the upload
// Don't trust provided filename - same goes for MIME types
// See http://php.net/manual/en/features.file-upload.php#114004 for more thorough upload validation
$uploadfile = tempnam(sys_get_temp_dir(), hash('sha256', $_FILES['userfile']['name']));
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
// Upload handled successfully
// Now create a message
require '../vendor/autoload.php';
$mail = new PHPMailer;
$mail->setFrom('from@example.com', 'First Last');
$mail->addAddress('whoto@example.com', 'John Doe');
$mail->Subject = 'PHPMailer file sender';
$mail->Body = 'My message body';
// Attach the uploaded file
$mail->addAttachment($uploadfile, 'My uploaded file');
if (!$mail->send()) {
$msg .= "Mailer Error: " . $mail->ErrorInfo;
} else {
$msg .= "Message sent!";
}
} else {
$msg .= 'Failed to move file to ' . $uploadfile;
}
}
?>
any guidance will be appreciated.
My Form looks like this:
<form action='/submit/submit.php' method='post' name='myform'>
<input type="hidden" id="some-place" name="some_place" value="classified">
<div class="row">
<div class="col-sm-14">
<textarea name='message' placeholder="Message..." class="form-control" rows="9" ></textarea>
</div>
</div>
<div class="row">
<input class="form-control" type="text" name="name" placeholder="Name ">
<input class="form-control" type="email" name="email" placeholder="Email" required>
</div>
<div class="row">
<input class="btnbtn-action" type='submit' value="Send" onclick="return UpdateTheHiddenField()" >
<br/><br/>
</div>
</div>
</div>
</form>
system
Closed
June 9, 2019, 6:54am
6
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.