Add custom variable to PHP Mail script - php

<?php
session_start();
$_SESSION['dnevne'] = $dnevne;
$email_to = "marioznik#gmail.com";
$name = $_POST["name"];
$email_from = $_POST["email"];
$message = $_POST["message"];
$email_subject = "Price is: $dnevne ";
$headers = "From: " . $email_from . "\n";
$headers .= "Reply-To: " . $email_from . "\n";
ini_set("sendmail_from", $email_from);
$sent = mail($email_to, $email_subject, $message, $headers, "-f" .$email_from);
if ($sent)
{
header("Location: http://www.yourdomain.com/thankyou.html");
} else {
echo "There has been an error sending your comments. Please try later.";
}
?>
I add few reccomended things, but still i can't get $dnevne variable to my e-mail. Where is my mistake?

As you define a variable : $dnevna = $nocna + $ukupno on your 1st page, then, I recommand putting this value in a hidden input of the form (so user can't mess with it), then access it through $dnevna = $_POST['dnevna'];
The same thing your are doing with $name = $_POST["name"]; $email_from = $_POST["email"]; $message = $_POST["message"];
Then, you use PHP doc -> mail to make your email formatted as you need, include all needed data, and send it to you...
No need to worry about sessionif you're not using them before...

You need to learn to use sessions. Put session_start(); at the top of each page where you want to use a session variable.
Then add your $xyz variable to it as follows: $_SESSION['xyz'] = $xzy;
Then that variable is accessible anywhere that you're using sessions.

Related

Hi, I am making a contact form thingy, and my php isn't working any reason why?

Error:
the code:
<?php
$name = $_POST['name'];
$mail = $_POST['mail'];
$message = $_POST['message'];
$to ="bejidev27#gmail.com";
$subject = "New contact from " .$name;
$body = "";
$body .= "Name : " .$name. "\r\n";
$body .= "Email : " .$mail. "\r\n";
$body .= $message. "\r\n";
if($mail !=NULL){
mail($to, $subject, $body);
header("Location: index.html") ;
header("Location: done.html") ;
}
?>
It's not sending the mail nor the page is working i need help ,thanks in advance :D
First, check if your servers are running properly. Then, for the following
$name = $_POST['name'];
$mail = $_POST['mail'];
$message = $_POST['message'];
There need to be values for each you. If you are getting the values from a form, then you have to handle the form first using the 'isset' function, to be able to pass the values to each variables.

Having an issue using mailer.php

I'm currently using a mailer.php file for a minimal contact form
However I have noticed recently then when submitting the form, I am getting the following error;
HTTP Error 405.0 - Method Not Allowed
The page you are looking for cannot be displayed because an invalid method (HTTP verb) is being used.
My mailer.php file is the following
<?php
$email_to = "admin#mydomain.co.uk";
$name = $_POST["name"];
$email_from = $_POST["admin#mydomain.co.uk"];
$message = $_POST["message"];
$email_subject = "Feedback from website";
$headers = "From: " . $email_from . "\n";
$headers .= "Reply-To: " . $email_from . "\n";
$message = "Name: ". $name . "\r\nMessage: " . $message;
ini_set("sendmail_from", $email_from);
$sent = mail($email_to, $email_subject, $message, $headers, "-f" .$email_from);
if ($sent)
{
header("Location: https://www.mydomain.co.uk/thankyou.html/");
} else {
echo "There has been an error sending your message. Please try later.";
}
?>
The form using this file has the following form tag
<form method="post" action="mailer.php">
PHP is not my strong point so I'm not sure why this maybe happening. The form itself does not have a captcha field. Could spam be the root of the issue?
I'd appreciate anyone who can shed this light on this for me

PHP E-mail form - Define sender

I'm trying to create a HTML form, which will in the end send an e-mail using the folling PHP Code:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$destinationemail = "myemail#domain.com";
$emailcontent = "Name: {$name}\n\nE-Mail: {$email}\n\nMessage: {$subject}\n{$message}";
$subject = "Contact from Domain.com";
$from = $email;
mail($destinationemail, $subject, $emailcontent) or die("Error!");
echo "Thank you $name!";
?>
The problem is, everytime i receive an e-mail, i get is as if being sent from a what i guess is the Webhost general e-mail.
htgkaylg#server776.web-hosting.net
<htgkaylg#server776.web-hosting.net>
dom 08/10/2017, 18:40
Você;
I would like it to be received something like this:
myemail#domain.com
<myemail#domain.com>
dom 08/10/2017, 18:40
Você;
Is it possible?
Thank you,
Vítor
You have to use the Header as 4th parameter in mail() function.
Add this line and change mail as follow:
$headers = "From:" . $from . "\r\n";
mail($destinationemail, $subject, $emailcontent, $headers) or die("Error!");
Ref. https://www.w3schools.com/php/func_mail_mail.asp

PHP error in form spam prevention

I have a problem with this code returning nothing but a blank page... I have followed several different tutorials to try and get this working, I am not a proficient PHP coder at all - But do have a little understanding. My server however, doesn't show error messages. So pinpointing this is rather hard for me to do!
I added this top section here to prevent spam using a hidden field on the html page that posts to this email page.
<?php
$if(isset($_POST['subject'],$_POST['Customer'],$_POST['Email'],$_POST['Phone'],$_POST['Comment'],$_POST['Product'],$_POST['Amount'],$_POST['Valid'])) {
$if(isset($_POST['Name']) && !empty($_POST['Name'])) {
echo "Spam Detected!";
Die();
}
Here is the email part that is supposed to send email once the above section determines that field "name" is empty.
$subject = $_POST['subject'];
$name = $_POST['Customer'];
$from = $_POST['Email'];
$phone = $_POST['Phone'];
$message = $_POST['Comment'];
$prod = $_POST['Product'];
$cash = $_POST['Amount'];
$valid = $_POST['Valid'];
$to = "email#email.com";
$body = 'HTML EMAIL CONTENT HERE...';
$headers .= "From: $from " . "Subject";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
mail($to,$subject,$body,$headers);
}
?>
Any and all help will be greatly appreciated!
Thanks
use this i see your code you use $if try if without $
if(isset($_POST['subject'],$_POST['Customer'],$_POST['Email'],$_POST['Phone'],$_POST['Comment'],$_POST['Product'],$_POST['Amount'],$_POST['Valid'])) {
if(isset($_POST['Name']) && !empty($_POST['Name'])) {
echo "Spam Detected!";
Die();
}
}

Why is the From email field not getting set?

Problem solved! Here is the final code:
<?php
$full_name;$email;$subject;$message;$captcha;
if(isset($_POST['full_name'])){
$full_name=$_POST['full_name'];
}if(isset($_POST['email'])){
$email=$_POST['email'];
}if(isset($_POST['subject'])){
$subject=$_POST['subject'];
}if(isset($_POST['message'])){
$message=$_POST['message'];
}if(isset($_POST['g-recaptcha-response'])){
$captcha=$_POST['g-recaptcha-response'];
}
if(!$captcha){
echo 'Check the reCAPTCHA box.';
exit;
}
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=SECRET-KEY-HERE&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
if($response.success==false)
{
echo 'You are a robot!';
}else
{
$to = "me#domain.com";
$from = $full_name . ' <'.$email.'>';
$headers = 'From: ' . $from . "\r\n";
mail ($to, $subject, $message, $headers);
echo 'Your message has been sent!';
}
?>
Now I get the From field with the name of the sender + email address.
Thank you everyone for the help.
Make sure you're properly validating that you're receiving everything from $_POST. If it fails, you should trigger an error each step of the way.
In the code
if(isset($_POST['full_name']) && isset($_POST['full_name'])){
$full_name = $_POST['full_name'];
$email = $_POST['email'];
$from = $full_name . '<'.$email.'>';
}
You're validating against $_POST['full_name'] twice; there's no isset() check on $_POST['email']. If $_POST['email'] isn't coming through properly, the from email address will be nulled out.
First of all you don't have initializing $headers variable at all, but you have
$headers .= 'From: ' . $from . "\r\n";
Initalize it on the beggining or just change operator ".=" into "=".
The next thing is that above concatenation will be execute only if field "g-recaptcha-response" will be exists in send $_POST array. As I can see there's no field with that name in that HTML code below. Try to add that field or remove this "if" statement
if(isset($_POST['g-recaptcha-response']))
I believe the issue may be the way that you're building the from variable:
$from = $full_name . '<'.$email.'>';
Notice that you have no space between the full name and the < character. Add a space in there and you should be good, i.e.:
$from = $full_name . ' <'.$email.'>';
As an aside, your code is not very well structured and has some logic flaws as commenters have mentioned. Consider restructuring it for your own sanity.

Categories