emailing confirmation to user when they submit an html registration form - php

I am trying to email the data in an html when you click on the submit button, the html form uses the POST action. My php code is:
<?php
$email = $_POST['email'];
$to = '$email'; //email submitted in html form
$subject = 'Welcome ';
$message = 'hello';
$headers = 'From: myaddress#email.com' . "\r\n".
'Reply-To: From: myaddress#email.com' . "\r\n".
'X-Mailer: PHP/' . phpversion();
$result = mail($to, $subject, $message, $headers);
if(!$result) {
echo "Error";
} else {
echo "Success";
}
?>
I get a success message but no email comes through, any ideas would be highly appreciated. many thanks

Related

PHP Confirmation Email after filling out Contact Form

I've created a contact page for a project im working on and im having trouble getting it to send a confirmation email to the user. I have the function defined on my emailer page along with the code that sends the contact form to me
public function sendEmail(){ //this will format and send an email to the smtp server
$to = $this->getSenderEmail();
$subject = $this->getSubject();
$message = $this->getMessage();
$headers = "From: <contact#tristonnearmyer.com >";
$from = $this->getCustomerInfo();
//it will use the php mail()
return mail($to,$subject,$message,$from);
}
public function sendConfEmail(){
$userEmail = $this->getRecipientEmail();
$confSubject = "confirmation";
$confMessage = "thank you";
return mail($userEmail, $confSubject, $confMessage);
}
then the functions are used on the contact page
echo $emailTest->sendEmail(); //send email to SMTP server
echo $emailTest->sendConfEmail(); //send confirmation email
I can't seem to figure out why one is working while the other isn't
Please try this code, you need to call the function sendConfEmail after sendEmail success. Also from here you need to modify the header. But base on user3783243 commend better change to PHPMailer or use swift
public function sendEmail(){ //this will format and send an email to the smtp server
$to = $this->getSenderEmail();
$subject = $this->getSubject();
$message = $this->getMessage();
$from = $this->getCustomerInfo();
$headers = 'From: '. $from . "\r\n" .
'Reply-To: youremailhere#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$sendMail = mail($to,$subject,$message,$headers);
if($sendMail){
// Call the function send confirmation
$this->sendConfEmail();
}else{
echo 'failed sent the email';
}
}
public function sendConfEmail(){
$userEmail = $this->getRecipientEmail();
$confSubject = "confirmation";
$confMessage = "thank you";
$from = $this->getCustomerInfo();
$headers = 'From: '. $from . "\r\n" .
'Reply-To: youremailhere#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
return mail($userEmail, $confSubject, $confMessage,$headers);
}

Mail Sending But Not On Submit?

I am a PHP novice here. I have a send mail script that looks like this:
$to = 'example#email.com';
$from = 'noreply#email.com';
$subject = 'Test Submission';
$message = 'This is just another test.';
$headers = 'From: example#email.com' . "\r\n" .
// 'Reply-To: example#email.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
This script works fine and sends an e-mail when the page is loaded. The problem is that when I alter the script just enough so that the form is not submitted until the Submit button is clicked then all of a sudden nothing works anymore. Here is the altered code I have been trying to use (and which seems to go right along with what the PHP site suggests):
if(isset($_POST['submit'])){
$to = 'example#email.com';
$from = 'noreply#email.com';
$subject = 'Test Submission';
$message = 'This is just another test.';
$headers = 'From: example#email.com' . "\r\n" .
// 'Reply-To: example#email.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
Make sure your submit button on your form has a NAME attribute. The value of that NAME attribute is what gets sent to the server, so:
<input type="submit" name="btnSubmit" value="Go!">
...would result in this variable and value:
$_POST["btnSubmit"] = "Go!";
...and you would check it like this:
if(isset($_POST["btnSubmit"]))
{
...
}

php send mail to client and business

I am struggling with the following.When a user fills in a form looking like this, the inquiry gets sent to us to follow up the lead, but I need to find a way to modify my email script to send a confirmation email to the user aswell
My email script looks like this:
MY QUESTION HOW DO I MODIFY THIS TO SEND A THANK YOU CONFIRMATION TO THE USER
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = $_POST['name'];
$email = $_POST['email'];
$mobile = $_POST['mobile'];
$eventType = $_POST['eventType'];
$nrGuests = $_POST['nrGuests'];
$state = $_POST['state'];
$suburb = $_POST['suburb'];
$msg = $_POST['msg'];
$refferal_page = $_POST['page'];
$ip = $_SERVER['REMOTE_ADDR'];
if(!isset($refferal_page)){
$refferal_page="Source Unknown";
}
$to_email = "unknown#yahoo.co.nz"; //Recipient email
//TODO Sanitize input data using PHP filter_var().
$suburb = filter_var($_POST["suburb"], FILTER_SANITIZE_STRING);
$subject = "You Have a New Booking!";
//Add booking INFO here;
$message = "A new Enquiry has been received from http://www.xxxxx.com.au. Please find details below and follow up:";
//email body
$message_body = $message."\r\n\r\n".$name."\r\nEmail : ".$email."\r\nPhone Number : ". $mobile."\r\nEvent Type: ".$eventType."\r\nNumber Guests:".$nrGuests."\r\nState: ".$state."\r\nSuburb:".$suburb."\r\nIP ADDRESS:".$ip."\r\nMessage:".$msg."\r\nReferal Page Source:".$refferal_page;
//proceed with PHP email.
$headers = 'From: '.$name.'' . "\r\n" .
'Reply-To: '.$email.'' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$send_mail = mail($to_email, $subject, $message_body, $headers);
if($send_mail)
{
echo '<h1 class="page-header">Enquiry Successfully submitted</h1>';
}
else if(!$send_mail){
echo'<h1 class="page-header" style="color:RED">OOPS...SOMETHING WENT WRONG, PLEASE TRY AGAIN!</h1>';
}
}//server request method
else{
echo'<h1 style="color:red">SUBMIT A FORM PLEASE!</h1>';
}
?>
EMAIL THAT COMES TO US
A new Inqury has been received from www.xxxx.com.au. Please find details below and follow up:
FROM: Ryan XXX
Email : aass#testms.com
Phone Number : 334533
Event Type: Aniversary
Nr Guests: 34
State: Melbourne
Suburb: XYZ
IP ADDR: 162.111.255.111
MSG: this is a test. please disregard
Referal Page Source:aniversary
Obviously I dont want user to see this I just want to add something like "Thank you we will get back to you soon"
Any idea how I can modify the above script for us to get inquiry form but also for user to get an confirmation email?
Any help greatly appreciated
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = $_POST['name'];
$email = $_POST['email'];
$mobile = $_POST['mobile'];
$eventType = $_POST['eventType'];
$nrGuests = $_POST['nrGuests'];
$state = $_POST['state'];
$suburb = $_POST['suburb'];
$msg = $_POST['msg'];
$refferal_page = $_POST['page'];
$ip = $_SERVER['REMOTE_ADDR'];
if(!isset($refferal_page)){
$refferal_page="Source Unknown";
}
$to_email = "unknown#yahoo.co.nz"; //Recipient email
//TODO Sanitize input data using PHP filter_var().
$suburb = filter_var($_POST["suburb"], FILTER_SANITIZE_STRING);
$subject = "You Have a New Booking!";
//Add booking INFO here;
$message = "A new Enquiry has been received from http://www.xxxxx.com.au. Please find details below and follow up:";
//email body
$message_body = $message."\r\n\r\n".$name."\r\nEmail : ".$email."\r\nPhone Number : ". $mobile."\r\nEvent Type: ".$eventType."\r\nNumber Guests:".$nrGuests."\r\nState: ".$state."\r\nSuburb:".$suburb."\r\nIP ADDRESS:".$ip."\r\nMessage:".$msg."\r\nReferal Page Source:".$refferal_page;
//proceed with PHP email.
$headers = 'From: '.$name.'' . "\r\n" .
'Reply-To: '.$email.'' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$send_mail = mail($to_email, $subject, $message_body, $headers);
if($send_mail)
{
echo '<h1 class="page-header">Enquiry Successfully submitted</h1>';
$EmailTo = $email;
$subject = "recipient subject";
$message_body = "Hi ".$name."recipient message";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: yourname<$youremail>\n";
$send_cust_mail = mail($to_email, $subject, $message_body, $headers);
}
else if(!$send_mail){
echo'<h1 class="page-header" style="color:RED">OOPS...SOMETHING WENT WRONG, PLEASE TRY AGAIN!</h1>';
}
}//server request method
else{
echo'<h1 style="color:red">SUBMIT A FORM PLEASE!</h1>';
}
?>

How do I include sender's name and not just the email address while emailing message from user input form

I am trying to take inputs from a form on my website using a simple PHP script as given below:
<?php
$toemail = 'xyz#anyemail.com';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
if(mail($toemail, 'Subject', $message, 'From: ' . $email)) {
echo 'Your email was sent successfully.';
} else {
echo 'There was a problem sending your email.';
}
?>
This worked perfectly. Well, almost. The email I receive does not include the sender's name. I would want to see a normal email response (with name of the sender in the name column of inbox and I should be able to see the email address of the sender when I open the mail.)
So I made a few changes after looking up some threads like this:
<?php
$toemail = 'xyz#anyemail.com';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$email_from = $name.'<'.$email.'>';
$header = 'From: '.$email_from;
if(mail($toemail, 'Subject', $message, $header)) {
echo 'Your email was sent successfully.';
} else {
echo 'There was a problem sending your email.';
}
?>
Email response is still same. What do I need to change ?
Try this format, note the spaces and the \r\n's, change your variables accordingly:
$email_headers = 'MIME-Version: 1.0' . "\r\n";
$email_headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$email_headers .='From: YOURNAME <sender_email#yourdomain.com>' . "\r\n";
$email_headers .='Reply-To: reply_to_email#yourdomain.com' . "\r\n";
mail($recipient_address, $email_subject, $email_body, $email_headers);
<?php
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
Quoted from http://php.net/manual/en/function.mail.php

if statement breaking my generated email

I have the following email generated automatically. When it is triggered and I am sent an email, I only get "MESSAGE PART 1". The if statement, and everything after the if statement (including REST OF MESSAGE) never gets sent. How can this be done?
/*----SEND EMAIL TO STUDENT----*/
$to = $studentEmail;
$subject = 'Title';
$message = 'Dear '.$studentFirstName.',
MESSAGE PART 1';
if ($bubbleWrap == true) {
'Bubble Wrap ($5)';
}
'REST OF MESSAGE';
$headers = 'From: CustomerService#GuysAndDollies.com' . "\r\n" .
'Reply-To: customerservice#GuysAndDollies.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
You need to concatenate the remaining strings with the original:
$message = 'Dear '.$studentFirstName.', MESSAGE PART 1';
if ($bubbleWrap == true) {
$message .= 'Bubble Wrap ($5)';
}
$message .= 'REST OF MESSAGE';
Currently you just have two string literals which will do absolutely nothing on their own.

Categories