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.
Related
<?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.
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();
}
}
Amateur question, I know but could someone please let me know the correct syntax for inputting variables in the "$to = " section when using php's mail function. When I used the below format, I got an error. I know its possible to input the email addresses as constants like "$to = abc#gmail.com, def#gmail.com", but I want to use variables.
Thanks in advance!
$to = '$user1', '$user2';
$subject = 'Congratulations! You have a match!';
$message = 'Someone who you liked likes you too! Log in to find out who';
$headers = 'From: info#xyy.com' . "\r\n" .
'Reply-To: info#xyy.com' . "\r\n" .
mail($to, $subject, $message, $headers);
change this
$to = '$user1', '$user2';
to this
$to = "$user1,$user2";
Easy as
$to = "$user1, $user2";
Please ensure you have validated e-mail addresses, e.g. by using
$to = filter_var($user1, FILTER_SANITIZE_EMAIL) . ', ' . filter_var($user2, FILTER_SANITIZE_EMAIL);
which implements a little security.
It accepts a single comma separed string.
$to="$user1,$user";
Phpmail docs
my site is hosted on name.com, and to test this I uploaded a simple file called contact.php with this at the top.
<?php
$to = '~~~~';
$subject = 'enquiry from ';
$name = $_POST['name'];
$email - $_POST['email'];
$message = $_POST['message'];
if ($_POST){
mail($to, $subject, $message, $header);
$feedback = "Sent";
}
?>
So, when I click the submit button it sends the mail. Inside a p tag I have echo $feedback, which shows up after I click submit.
The mail does not send? Anything I'm doing wrong here, or do I need to configure my cPanel in some way?
Since header is optional and is left undefined, removing it should resolve your issue.
Be aware that using the $_POST content directly into a email is a security risk for you!
THere are good email libraries that have tools to avoid abuses. (For example: ZendMail, PHPmailer)
Having said that, on your code you are missing the header and have a small mistake on $email = (not -) $_POST['email'];, you can use this:
$header = 'From: from#name.com' . "\r\n" .
'Reply-To: from#name.com' . "\r\n" .
'Content-type: text/html; charset=iso-8859-1' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
You have not set header variable which is optional, there is a typo while setting $email variable, you've use - instead of =
your updated code,
<?php
$to = 'a#a.com';
$subject = 'enquiry from ';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
if ($_POST){
mail($to, $subject, $message);
$feedback = "Sent";
}
?>
I want to send the user an activation link after they registered an account. when I put this http://www.homeloan.com.sg in the $message I didn't receive the email, but when I remove the .sg and put http://www.homeloan.com it works. There's no error message, so I really don't know what's my mistake. Please help
here are my codes:
$id = mysql_insert_id();
$to = 'myemail#gmail.com';
$subject = "E-mail Verification";
$message = 'Click on the link to verify your account-> http://www.homeloan.com.sg/rates/activate?id='.$id.'';
$headers = "From: Homeloan Singapore" . "\r\n" . "Reply-To: enquiry#homeloan.com.sg";
mail($to,$subject,$message,$headers, '-f enquiry#homeloan.com.sg');
Make sure if your site have a form to fill, then fill the form correctly by assembling the input tag in the corresponded variable.
Try concatenating 'the email' to the variables ($...) with (.) or "...".
I really don't know what's my mistake
There is no mistake. I tried this code:
<?php
$id = 1;
$to = 'my_email#gmail.com';
$subject = "E-mail Verification";
$message = 'Click on the link to verify your account-> http://www.homeloan.com.sg/rates/activate?id='.$id;
$headers = "From: Homeloan Singapore" . "\r\n" . "Reply-To: enquiry#homeloan.com.sg";
mail($to,$subject,$message,$headers, '-f enquiry#homeloan.com.sg');
It arrived in the mailbox. Maybe, for your account it was put into spam folder?