PHP Mail sending email on page refresh - php

Ahoy!
Any help on this would be really appreciated! I have a contact form which is sending emails on a page refresh. I have tried a number of things and am not getting anywhere.. Heres what Ive got so far:
if(isset($_POST['email']) && $_POST['email'] != '') {
if( filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ){
$userName = $_POST['name'];
$userEmail = $_POST['email'];
$messageSubject = $_POST['subject'];
$message = $_POST['message'];
$to = "email#gmail.com";
$body = "";
$body .= "From: " .$userName. "\r\n";
$body .= "Email: " .$userEmail. "\r\n";
$body .= "Message: " .$message. "\r\n";
header('Location: http://www.website.net/contact-thank-you.html');
exit();
mail($to, $messageSubject, $body);
}
}
?>

The easiest way to prevent this is to redirect to a new URL after the email has been sent. Often websites will redirect to a "thank you" page.
You might be able to get away with redirecting to the same URL (and this won't send a second email because $_POST will be empty).
Another way would be to use the user session ($_SESSION) to keep track of whether the email was sent, and don't send an email again if you detect that $_SESSION indicates it was already sent. You could also use cookies for this same purpose. Last but not least you could store that information in a database and check the DB before sending an email so you don't send multiple emails.

Related

Using PHP Session variables to send email form

I am currently trying to use the same php session variables saved in the DB to fill in information for an email form. Unfortunately as far as i can see the code is correct but the email won't return errors or refuses to send.
var_dump ($_POST);
if
(isset($_POST['submit']));
$to = "brendonexus#gmail.com"; // this is your Email address
$from = $_SESSION['email']; // this is the sender's Email address
$fullname = $_SESSION['fullname'];
$subject = "ICE Live Streaming Event - Online questions";
$subject2 = "Copy of the questions you submitted.";
$message = $fullname . " asked the following:" . "\n\n" . $_POST['message'];
$message2 = "Thank you, your question has been received and will be put forward to the panel for consideration." . $first_name . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers); // this is the message that is sent
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
echo "Mail Sent. Thank you " . $fullname . ", We have recieved your email.";
// You can also use header('Location: thank_you.php'); to redirect to another page.
Edit: I have managed to get it to send an email now but it seems that when i var_dump the session variables the only one that returns is the username. Whats confusing is that if the username works and all the other code is built the same then i do not understand why there is a compatibility issue.

How to get cookie value from user's browser and email it to webmaster after form submit

I have a form on my site which users are filling with name and email, and then it generates an email to the webmaster notifying of user submitting their info. There's also a cookie placed which is for referral tracking, how can I also include that data in the email out to the webmaster?
<?php
///subscribe form
$email = $_POST['email'] ;
$firstname = $_POST['firstname'] ;
$recipient = "myemail#email.com"; /// Webmaster address
if (isset($_POST['email']))
{
//Send Mail To Webmaster
$email = $_POST['email'] ;
$subject = ' Enviralizer: Someone Just Requested More Info';
$message = 'Your new lead info is below: ';
$message .= "\r\n\nName: " .$firstname;
$message .= "\r\n\nEmail: " .$email;
mail("$recipient", $subject,
$message, "From:" . $recipient);
header("Location: http://thanks.com");
}
?>
I know the script is not the best secured thing in the world, I will take care of that later, I just need a quick fix for a bigger script tracking issue I'm trying to resolve as well. Thanks.
I guess you're looking for $_COOKIE['name_of_cookie'] ?

send an email using data the user has given in a form?

I have a contact form for my website and am hoping to modify it so that a confirmation email is sent to the user when they click submit. Can anybody advise me on the best way to do this?
My php is pretty simple:
// validation complete
if(isset($_POST['submit'])){
if($msg_name=="" && $msg2_name=="" && $msg_email=="" && $msg2_email=="" &&
$msg2_Message=="")
$msg_success = "Thankyou for your enquiry";
//send mail
$EmailFrom = "someone#somewhere.co.uk";
$EmailTo = "someone#somewhere.co.uk";
$Subject = "Online contact form";
$full_name = Trim(stripslashes($_POST['full_name']));
$Phone_Num = Trim(stripslashes($_POST['Phone_Num']));
$email_addr = Trim(stripslashes($_POST['email_addr']));
}
// prepare email body text
$Body = "";
$Body .= "full_name: ";
$Body .= $full_name;
$Body .= "\n";
$Body .= "Phone_Num: ";
$Body .= $Phone_Num;
$Body .= "\n";
$Body .= "email_addr: ";
$Body .= $email_addr;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: $EmailFrom");
?>
First we get the mailto function to work with localhost and email client:
Check this link on stackoverflow:
URL link: send email to owner by collecting information provided by user through form?
Then I recommend using Swiftmailer. http://swiftmailer.org/docs/sending.html They got the best manual.
As others have already suggested the "if" statement is useless since it is achieving nothing. I think your idea was to verify if the fields that you have in your contact form are filled or not. If the fields are unavailable you should throw some error which you are not doing.
Also, if you are to send a confirmation mail to the user who clicks the submit button then the $EmailTo variable should take the email from $msg_email or $msg2_email which according to the code is not done here.
Check this simple snippet this might help you:
if ($_POST['submit']) {
if ($_POST['name1] == '')
echo "Please provide the first name";
if ($_POST['name2] == '')
echo "Please provide the last name";
if ($_POST['email1] == '')
echo "Please provide the email";
if ($_POST['email2] == '')
echo "Please provide the alternate email";
if ($_POST['message] == '')
echo "Please provide the Message";
//Send email to your inquiry mail with above details
// Confirmation mail
$message =<<<EOM
Dear $_POST['name1'],
Thank you for your inquiry.
Our executives will get back to you ASAP.
Thanks,
Sales
EOM;
$to = $_POST['email1'];
$subject = "Acknowledgement of Inquiry";
$headers = "Content-Type: text-plain";
$headers .= "From: sales#yourcompany.co.uk";
mail($to, $subject, $message, $headers);
}

How do I send an email to the user on submit of php form

How do I send an email to the user with the data they submitted in the form that includes a little message using there name and thanking them on submit of php form.
Here is my current php code. It currently just shows them a message that says there name and that the message has been sent and then sends me an email to my email address.
<?php
if(isset($_POST['submit'])){
$to = "benlevygraphics#gmail.com";
$headers = "From: " . $_POST['email'];
$subject = "Ben, you have been contacted...";
$body = "Name: " . $_POST['name'] . "\nEmail: " . $_POST['email'] . "\nWebsite: " . $_POST['web'] . "\nMessage: " . $_POST['message'];
if(mail($to, $subject, $body, $headers)){
echo("<p class=contactformsent>".$_POST['name'].", your message has been sent!</p>");
}
else{
echo("<p class=contactformnotsent>".$_POST['name'].", Message delivery failed...</p>");
}
}
?>
I am new to php and I have read stuff online and I still don't understand so if you could be clear in your examples or help I would greatly appreciate it very much. Thanks!
Assuming your current code is already working fine, you can do this to send yourself an email together with the recipient:
Set $to to $_POST['email']
Set $headers to "From: {$_POST['email']}\r\nBcc: benlevygraphics#gmail.com"
Adjust $body and $subject to your needs.
Btw, I can't say this often enough; make sure that your page has some form of CSRF protection.
How to properly add CSRF token using PHP
The above is just one way, there are others, just search for it :)
Look into your php.ini beacuse you have to enter a SMTP Server.
At my file it begins in line 1087 with "[mail function]"

Need a PHP email send script for HTML an form

I have a single page portfolio website that I'm building and I have a contact form that I need to process using php send script. I'm a novice when it comes to PHP so I'm having trouble getting this to work. I've done some searching but I can't find what I'm looking for.
Here's what I have done, I copied this from a PHP contact page that I had built but the PHP and form are on the same page and I need an external send.php to process my form.
<?php
$error = ''; // error message
$name = ''; // sender's name
$email = ''; // sender's email address
$company = ''; // company name
$subject = ''; // subject
$comment = ''; // the message itself
if(isset($_POST['send']))
{
$name = $_POST['name'];
$email = $_POST['email'];
$company = $_POST['company'];
$subject = $_POST['subject'];
$comment = $_POST['comment'];
if($error == '')
{
if(get_magic_quotes_gpc())
{
$message = stripslashes($message);
}
// the email will be sent here
// make sure to change this to be your e-mail
$to = "example#email.com";
// the email subject
// '[Contact Form] :' will appear automatically in the subject.
// You can change it as you want
$subject = '[Contact Form] : ' . $subject;
// the mail message ( add any additional information if you want )
$msg = "From : $name \r\ne-Mail : $email \r\nCompany : $company \r\nSubject : $subject \r\n\n" . "Message : \r\n$message";
mail($to, $subject, $msg, "From: $email\r\nReply-To: $email\r\nReturn-Path: $email\r\n");
}
}
if(!isset($_POST['send']) || $error != '')
{
header("location: http://www.#.com/#contact");
}
?>
So for my form I want to have:
<form method="post" action="send.php" class="form">
I plan on using HTML5 and jQuery to validate the form, so I really only need the script to capture the info and send the email to a single address. After it sends I want the script to redirect back to the Contact page.
Edit:
I found a solution after spending a while on google.
http://www.website.com/#contact");
?>
For one thing, you haven't initialized the value for $message
$message = stripslashes($message);
You probably meant to use $comment instead of $message
Not sure what problem you're facing. Simply copy the PHP you have into a file called send.php and my first glance says it'll work if you change $message back to $comment and add error checking. If you still have issues, post back with more details.

Categories