i am working on one static website, in which there is a contact us page. Here what i want to do is when the contact form is submitted it should show the message that - Email has been sent successfully. But the problem is i am calling the html page and we cannot pass php message in html view. So is there any way to get it done.
conatctus.php
<?php
$error = '';
$mailTo = $_POST['email'];
$mailFrom = 'info#sample.com';
//$headers = 'MIME-Version: 1.0' . "\r\n";
//$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$fullname = $_POST['username'];
$phoneno = $_POST['mobile'];
$emailaddress = $_POST['email'];
$msgsubject =$_POST['message'];
$new = "\n";
$msg = $fullname.$new.$emailaddress.$new.$phoneno.$new.$msgsubject;
$to = $email;
$subject = 'Inquiry';
$messageclient = '<div>
<p>Thank you For Inquiry.</p>
<p> We will reach back to you shortly. Have a Nice Day!</p>
<p>Company © 2013</p>
</p></div>
';
$headers = 'From: info#email.com' . "\r\n" .
'Reply-To: info#email.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=utf-8\r\n";
$headers .= "From: Company<info#email.com>\r\n";
//$res = mail($to, $subject, $message, $headers);
//$message ='Thank you For Inquiry. We will reach back to you shortly. Have a Nice Day';
mail( $mailTo , $subject, $messageclient, $headers);
$message .= "<p>Name: $fullname</p><br /><p>Contact Number : $phoneno</p><br /> <p>Email: $emailaddress</p><br /><p>Message: $msgsubject</p>";
mail( $mailFrom, $subject,$message, $headers);
header("location:home.html");
?>
Your help is much appreciated, thanks in advance.
There are a lot of problems here, but to answer your question, you can't redirect after content has been sent.
If you add ob_start() to the top of the page it will buffer the contents and allow the redirect.
Upon, further re-reading of your post, maybe I misinterpreted. It doesn't look like you are sending content which means that your redirect IS working, but what you want is to add a message after it's been redirected.
You have options.
Redirect to a static HTML page that reflects the message you want to convey.
Redirect to a PHP page that has the logic to give the user a message.
Use Ajax to send the email and don't redirect at all.
You might want to do either an ajax request on the submit to prevent changing the page or make a landing page to which you will redirect after processing the mailing function
Have this email sent function performed as an ajax call and on it's success show user the success message.
If you can configure the web server, you can change it in a way to treat html pages like php pages. For example in APACHE's httpd.conf:
AddType application/x-httpd-php .php .htm .html
Hope that works for you.
Related
I want to write a PHP page to run on a cron job to email out the contents of a PHP page.
When trying this, I get the scripts included in the body of the email, not just the echoed out elements.
Here is the script I'm using below currently to try and include kpi.php in the email.
Any thoughts on how I can achieve this?
alternatively I'd be happy to grab a screenshot of the page.
Thanks
<?php
$to = "email#domai.com";
$subject = "Daily Update";
$message = include('kpi.php');
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: <no-reply#email.com>' . "\r\n";
mail($to,$subject,$message,$headers);
?>
Just include it beforehand and echo the variables needed into the body of the email
I am new here and this is my first post. Unfortunately, I am not familiar with php coding and so I need help for the following script. I would like to use this code on my website containing download files. I want to add a link or button next to the download link. When clicking the link the script should be executed and send an email to me with a given text.
Now, I read that this code could be a victim to header injection. As I am not familiar with php I do not know what to change to be protected. Is there anyone who might help me out with a solution? This is the code:
<?php
$to = 'name#example.com';
$subject = 'Broken Download-Link';
$from = 'Subject-Title <name#example.com>';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Create email headers
$headers .= 'From: '.$from."\r\n".
'Reply-To: '.$from."\r\n" .
'X-Mailer: PHP/' . phpversion();
// Compose a simple HTML email message
$message = '<html><body>';
$message .= '<h2 style="color:#080;font-weight:normal;">Hello!</h1>';
$message .= '<p style="color:#000;font-size:18px;font-weight:normal;">Text here:</p>';
$message .= '<p style="color:#f40;font-size:22px;font-weight:bold;">Another text here</p>';
$message .= '</body></html>';
// Sending email
if(mail($to, $subject, $message, $headers)){
echo 'Your mail has been sent successfully.';
} else{
echo 'Unable to send email. Please try again.';
}
?>
Thank you in advance for any help.
Best regards,
Feechen
I have a simple message that I send using php mail().
The code used:
//recipient info
$to = "$bookernavn <$mail>";
$from = "Visens Venner Hillerød <booking#eksample.dk>";
$subject = "Kvittering - $a_titel - ". date("j/n - Y",$a_dato);
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=utf-8" . "\r\n";
$headers .= "From: $from" . "\r\n";
$headers .= "Reply-To: $from" . "\r\n";
$headers .= "Return-Path: $from" . "\r\n";
$headers .= "Bcc: $from" . "\r\n";
// now lets send the email.
mail($to, $subject, $mailmsg, $headers); }
For some strange reason two mails are sent each time...
Sometimes with several minutes in between...
Any ideas?
You don't check to see if the form has been submitted so a browser refresh will send the form data again and cause the mail to be sent again. This also happens when a user presses the back button.
After the email is sent you need to do a 303 redirect to prevent the re-submission. You can redirect to the same page if you'd like.
This is called the Post/Redirect/Get pattern.
mail(...);
header('Location: /some-page-php', true, 303);
exit;
an easy way to prevent this from happening is to use POST method instead of GET for the form.
<form method="post">
if (isset($_POST['submitted']))
and at the end of the mail code use a redirect that will send the browser to load using a GET method.
Not only you can then redirect your user to a OK page "mail was sent" or a error page "sorry there was a mistake, please try again", a refresh of that page open by the browser will only send a GET, not triggering the send mail function
if (empty($errors)) {
header('Location: http://www.example.com/mail_OK.html');
exit;
} else {
// passing data to the "error/retry" page
$info = array(
'msg' => $msg,
'email' => $_POST['email'],
'name' => $_POST['name']
// etc...
)
header('Location: http://www.example.com/mailform.php?'.http_build_query($info));
exit;
}
in your form you can retrieve those info
<input name="name" type="text" placeholder="Naam" class="form-control" value="<?php echo htmlspecialchars($_GET['name']); ?>">
I have a simple message that I send using php mail().
The code used:
//recipient info
$to = "$bookernavn <$mail>";
$from = "Visens Venner Hillerød <booking#eksample.dk>";
$subject = "Kvittering - $a_titel - ". date("j/n - Y",$a_dato);
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=utf-8" . "\r\n";
$headers .= "From: $from" . "\r\n";
$headers .= "Reply-To: $from" . "\r\n";
$headers .= "Return-Path: $from" . "\r\n";
$headers .= "Bcc: $from" . "\r\n";
// now lets send the email.
mail($to, $subject, $mailmsg, $headers); }
For some strange reason two mails are sent each time...
Sometimes with several minutes in between...
Any ideas?
You don't check to see if the form has been submitted so a browser refresh will send the form data again and cause the mail to be sent again. This also happens when a user presses the back button.
After the email is sent you need to do a 303 redirect to prevent the re-submission. You can redirect to the same page if you'd like.
This is called the Post/Redirect/Get pattern.
mail(...);
header('Location: /some-page-php', true, 303);
exit;
an easy way to prevent this from happening is to use POST method instead of GET for the form.
<form method="post">
if (isset($_POST['submitted']))
and at the end of the mail code use a redirect that will send the browser to load using a GET method.
Not only you can then redirect your user to a OK page "mail was sent" or a error page "sorry there was a mistake, please try again", a refresh of that page open by the browser will only send a GET, not triggering the send mail function
if (empty($errors)) {
header('Location: http://www.example.com/mail_OK.html');
exit;
} else {
// passing data to the "error/retry" page
$info = array(
'msg' => $msg,
'email' => $_POST['email'],
'name' => $_POST['name']
// etc...
)
header('Location: http://www.example.com/mailform.php?'.http_build_query($info));
exit;
}
in your form you can retrieve those info
<input name="name" type="text" placeholder="Naam" class="form-control" value="<?php echo htmlspecialchars($_GET['name']); ?>">
I'm trying to get a JQM page to send an email via a form and then redirect to a new page. I have the mail part working fine, but the redirect not so much! After the form has been submitted and mail sent, the page basically just refreshes instead of loading the new page. I've tried many different things using the code header('Location:myredirectpage'); but nothing works.
The following php code is located below my closing HTML tag at the bottom of my page.
<?php
if(isset($_POST['mr'])) {
$to = "myemailaddress";
$subject = "Subject";
$content = ""
."Survey Details"."\n\n"
."How did you hear about us: ".$_POST['mr']."\n";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: <myemailaddress>\r\n";
mail($to, $subject, $content, $headers);
$sendit= #mail($to, $subject, $content, $headers);
if($sendit){
header('Location:myredirectpage');
}else{ echo "Email failed to send";}
}
?>
To redirect your page after it has been sent, you need something in the following format:
header('Location: index.php');
Change index.php to the FILENAME of what you want to get to.
This can be a full URL such as http://google.com
You are sending the mail twice : mail(...) then #mail(...).
Also using #mail(...) with # is considered bad practice.
Instead of header('Location:myredirectpage'); use
echo '<script>window.location = 'yourpage'</script>'
Try this