PHP mail() function $to not working properly - php

my HTML code:
<form role="form" method="post" >
<fieldset>
<div class="form-group">
<input class="form-control" placeholder="Enter Your Email" name="email" type="text" autofocus>
</div>
<!-- Change this to a button or input when using this as a form -->
<input type="submit" href="dashboard.php" class="btn btn-lg btn-success btn-block btn-warning" value="Reset" name="sub">
</fieldset>
</form>
my PHP mail() code:
$email = $_POST['email'];
$to = $email;
$subject = "Password Recovery";
$full="http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$reffurl=str_replace("forget_pass_nw_pass.php","",$full);
$message = "You are receiving this e-mail because you have requested to recover your password.";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <noreply#caveo.sg>' . "\r\n";
$mail_sent = mail($to,$subject,$message,$headers);
i tried changing
$to = $email
to
$to = "xxx#gmail.com"
and it actually works. but when it's $to to $email, no email is being sent.

Looks like a extra spacing problem.
change
$email = $_POST['email'];
to
$email = trim($_POST['email']);

$email=$_POST['email'];
// Sanitize E-mail Address
$email =filter_var($email, FILTER_SANITIZE_EMAIL);
// Validate E-mail Address
$email= filter_var($email, FILTER_VALIDATE_EMAIL);
if (!$email){
echo "Invalid Sender's Email";
}
else{
$to = $_POST['email'];
$to = $email;
$subject = "Password Recovery";
$full="http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$reffurl=str_replace("forget_pass_nw_pass.php","",$full);
$message = "You are receiving this e-mail because you have requested to recover your password.";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <noreply#caveo.sg>' . "\r\n";
$mail_sent = mail($to,$subject,$message,$headers);

Related

How to send a confirmation email to sender and to me after form submission

I would like to send a confirmation email to the user who submitted the form and another email to me with the details which the user inputted, here is my form:
<form class="" action="." id="dateForm" method="POST">
<input type="text" class="form-control" id="dfName" placeholder="Name" required>
<input type="email" class="form-control" id="dfEmail" placeholder="Email" required>
<input type="tel" class="form-control" id="dfPhone" placeholder="Phone Number" required>
<input type="text" class="form-control" id="dfDate" placeholder="Schedule a call" required>
<textarea id="dfMessage" rows="5" class="form-control" placeholder="Your Message" required></textarea>
<button type="submit" class="">SUBMIT</button>
</form>
And here is my php which works perfect for sending the emails to me.
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$phone = trim($_POST['phone']);
$date = trim($_POST['date']);
$message = trim($_POST['message']);
function is_email_valid($email) {
return filter_var($email, FILTER_VALIDATE_EMAIL);
}
if( isset($name) && isset($email) && isset($phone) && isset($date) && isset($message) && is_email_valid($email) ) {
$to = "myemail#gmail.com";
$subject = "New inquiry request from Olho";
$body = <<<EOD
<strong>Name:</strong> $name <br>
<strong>Email:</strong> $email <br> <br>
<strong>Phone:</strong> $phone <br>
<strong>Booking Date:</strong> $date <br>
<strong>Message:</strong> $message <br>
EOD;
$headers = "From: $name <$email>\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($to, $subject, $body, $headers);
So what I would like is to receive another email after the one send to me which will contain a confirmation message like "Thank you for contacting us, we will get back to you as soon as possible."
I've tried to use this but I am not receiving the confirmation email:
$conf_subject = 'Your recent enquiry';
$conf_sender = 'Olho';
$msg = $name . ",\n\nThank you for your recent enquiry. A member of our
team will respond to your message as soon as possible.";
$headers2 = "From: $conf_sender <myemail#gmail.com>\r\n";
$headers2 .= 'MIME-Version: 1.0' . "\r\n";
$headers2 .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail( $email, $conf_subject, $msg, $headers2 );
You should wait for first mail to be completed and then should send second email as below code:
if (mail($to, $subject, $body, $headers)) {
$conf_subject = 'Your recent enquiry';
$conf_sender = 'Olho';
$msg = $name . ",\n\nThank you for your recent enquiry. A member of our
team will respond to your message as soon as possible.";
$headers2 = "From: $conf_sender <myemail#gmail.com>\r\n";
$headers2 .= 'MIME-Version: 1.0' . "\r\n";
$headers2 .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail( $email, $conf_subject, $msg, $headers2 );
}
Hope it helps you.

how to send email message with php and html

I am trying to send a message from HTML form. But for some reason I am not getting anything. Could someone please help me ?
Here is my HTML form:
<form method="post" action="subb.php">
<div class="field half first">
<label for="Name">Name</label>
<input type="text" name="Name" id="name" />
</div>
<div class="field half">
<label for="Email">Email</label>
<input type="text" name="Email" id="email" />
</div>
<div class="field">
<label for="Message">Message</label>
<textarea name="Message" id="message" rows="5"></textarea>
</div>
<ul class="actions">
<button type "submit" name="submit" id="submit" class="button submit">Send message</button>
and the PHP:
<?php
$Name = $_POST['Name'];
$Email = $_POST['Email'];
$Message = $_POST['Message'];
$to = "combatstriker111#gmail.com";
$subject="new message";
mail($to , $subject , $Message, "From :" . $Name . $Email);
echo "Your message has been Sent";
?>
I have named the PHP file subb.php and listed them both in the same directories but its still not working for some reason. Any help is very much appreciated.
Something in your code was wrong mail($to , $subject , $Message, "From :" . $Name . $Email);
Mail function SYNTAX :
mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
So,
<?php
if(isset($_POST['submit'])) {
$Name = $_POST['Name'];
$Email = $_POST['Email'];
$Message = "Name : ".$Name."<br />"
$Message .= $_POST['Message'];
$to = "combatstriker111#gmail.com";
$subject="new message";
// 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";
// Additional headers
$headers .= 'To: Name <$to>' . "\r\n";
$headers .= 'From: $Name <$Email>' . "\r\n";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
if(mail($to, $subject, $Message, $headers)) {
echo "Your message has been Sent";
} else {
echo "Mesage Error";
}
}
?>
Note : Use any mail library for prevent vulnerable to header injection like PHPMailer
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];;
$subject = $_POST['subject'];
$message = $_POST['message'];
$to = 'info#fullertoncomputerepairwebdesign.com';
$subject = 'Message From Website';
$headers = 'From: info#fullertoncomputerepairwebdesign.com' . "\r\n" .
'Reply-To: info#fullertoncomputerepairwebdesign.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$themessage = "Name: ".$name."</br>Email: ".$email."</br>Phone: ".
$phone."</br>Subject: ".$subject.
"</br>Message: ".$message;
//echo $themessage;
if(mail($to, $subject, $themessage, $headers)){
echo "message sent";
header( 'Location: http://www.fullertoncomputerepairwebdesign.com/contactus.php?smresponse=Message Sent' ) ;
}else{
echo "we have a error charlie!";
}
;
test this out and if it doesn't work it means your hosting blocks mail function.

How can i add a default message with php mail function?

I have created an html form that after the user clicks "send" redirects to mail.php which contains php mail function that works and sends the desired message.
Is there any way to also add a default message for example "This was sent from the website" to the email?
Form from index.html
<form method="post" action="mail.php">
<div class="form-style">
<h1 class="formh1">Full Name</h1>
<input type="text" id="name" name="name" placeholder="Full name" required>
<h1 class="formh1">Email address</h1>
<input type="email" id="email" name="email" placeholder="Email address" required>
<h1 class="formh1">Message</h1>
<textarea rows="4" cols="50" id="message" name="message" placeholder="Give us your thought" required></textarea>
<div class="button-form">
<input type="submit" name="send" value="Send" />
</div>
</div>
</form>
Mail.php
<?php
if (isset($_POST['send'])) {
$from = 'myemail'; // Use your own email address
$subject = 'The following message was sent from the website';
$message = 'Fullname: ' . $_POST['name'] . "\r\n\r\n";
$message .= 'Email address: ' . $_POST['email'] . "\r\n\r\n";
$message .= 'Message: ' . $_POST['message'];
$name = trim(filter_input(INPUT_POST,"name",FILTER_SANITIZE_STRING));
$email = trim(filter_input(INPUT_POST,"email",FILTER_SANITIZE_EMAIL));
$details = trim(filter_input(INPUT_POST,"message",FILTER_SANITIZE_SPECIAL_CHARS));
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
header('Location: index.html');
if ($email) {
$headers .= "\r\nReply-To: $email";
}
$headers = "From: ".$_POST['email']."\r\n";
$headers .= 'Content-Type: text/plain; charset=utf-8';
$success = mail($from, $subject, $message, $headers);
}
?>
Just add the default email your would like to send to the $message variable:
$message = 'This is my default message lalalalalal'
You just amend before, or after... wherever you want to inject that content.
When you use $var .= "something added!"; - You're appending/concatenating additional text in this case.
When you use $var = "something added!"; - w/o The .= you've reset the variable to the new string.
Think of .= being the same as $message = $message . "String being added to message"
$from = 'myemail'; // Use your own email address
$subject = 'The following message was sent from the website';
#EXAMPLE HERE - Also note I added htmlspecialchars
#These will convert any misc. characters to html readable content.
#Test with it, see if it fits. Just don't add it to $email = trim...
$message = "Your friend " . htmlspecialchars($_POST['name']) . " has sent you a message from www.mywebsite.com"\r\n\r\n;
$message .= 'Fullname: ' . htmlspecialchars($_POST['name']) . "\r\n\r\n";
$message .= 'Email address: ' . htmlspecialchars($_POST['email']) . "\r\n\r\n";
$message .= 'Message: ' . $_POST['message'];
$name = trim(filter_input(INPUT_POST,"name",FILTER_SANITIZE_STRING));
$email = trim(filter_input(INPUT_POST,"email",FILTER_SANITIZE_EMAIL));
$details = trim(filter_input(INPUT_POST,"message",FILTER_SANITIZE_SPECIAL_CHARS));
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);

How to handle HTML forms with PHP?

I am trying to create a simple contact form that takes user-inputted data and e-mails it to me. however, when I use the following code and the user presses submit, the page is redirected to a blank page (/form.php) - not even the "Thank you!" shows up on it, nor is the e-mail even sent. Can someone point out any errors I'm making? thanks!
PHP:
<?php
if (isset($_POST['email'])) {
$myEmail = "myemail#gmail.com";
$name = $_POST['name'];
$message = $_POST['message'];
$email = $_POST['email'];
$subject = "Message from $email";
$message = "
Name: $name
E-mail: $email
Message:
$message
";
/* Sends to e-mail. */
mail($myEmail, $subject, $message, "hello");
?>
Thank you!
<?php
}
?>
HTML:
<form id="form" method="post" name="contact-form" action="form.php">
Name: <br>
<input type="text" name="name" /><br><br>
Email:<br>
<input type="text" name="email" /><br><br>
Message:<br>
<textarea name="message" placeholder="Tell me anything!"></textarea><br><br>
<input type="submit" value="Send">
</form>
You're declaring $message with your message (as part of the mail() header) then you're including $message inside it, in turn hashing and bashing your header.
Try this method instead.
<?php
if (isset($_POST['email'])) {
$myEmail = "myemail#gmail.com";
$name = $_POST['name'];
$message = $_POST['message'];
$email = $_POST['email'];
$subject = "Message from $email";
$message2 = "
Name: $name
E-mail: $email
Message:
$message
";
/* Sends to e-mail. */
if(mail($myEmail, $subject, $message2, "hello")){
echo "<b>Thank you</b>"; // HTML bold text.
}
else{
echo "<h2>Sorry, there was an error.</h2>";
}
}
?>
If you want to send email as HTML you need to use the following:
$headers = "From: $email" . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
then change what is above:
if(mail($myEmail, $subject, $message2, "hello"))
to read as:
if(mail($myEmail, $subject, $message2, $headers))
Fixed 2 problems.
<?php
if (isset($_POST['email'])) {
$myEmail = "myemail#gmail.com";
$name = $_POST['name'];
$message = $_POST['message'];
$email = $_POST['email'];
$subject = "Message from $email";
$message = "
Name: ".$name."
E-mail: ".$email."
Message:
".$_POST['message']."
";
/* Sends to e-mail. */
mail($myEmail, $subject, $message);
?>
Thank you!
<?php
}
?>

PHP Contact form won't work

I've tried every php contact form tutorial but I can't get any of them to send the email properly. They all just show the code but don't send the email. This is the HTML code I have right now:
<form method="POST" action="scripts/contact.php">
<input id="name" name="name" placeholder="Name*" type="text" /> <br>
<input id="subject" name="subject" placeholder="Subject" type="text" /> <br>
<input id="email_address" name="email" placeholder="Email address*" type="text" /> <br>
<textarea id="message" name="message" placeholder="Your message*"></textarea>
<input id="submit" type="submit" name="submit" value="Send" />
</form>
and this is the php I have:
<?php
$name = $_POST['name'];
$subject = $_POST['subject'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = "benmuschol#gmail.com";
$subject = "Hello World";
$message = "Name: $name \n Subject: $subject \n Email: $email \n Message: $message \n";
$headers = "From: $email" . $email;
mail($name,$email,$subject,$message,$headers);
echo "Mail Sent.";
?>
I have decent knowledge of HTMl but next to none of PHP. Any help would be greatly appreciated.
first you be sure that php file is exist in path scripts/contact.php
second look at phpinfo or php.ini for find is smptp server was setting or not
use following php code:
<?php
$name = $_POST['name'];
$subject = $_POST['subject'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = "benmuschol#gmail.com";
$subject = "Hello World";
$message = "Name: $name \n Subject: $subject \n Email: $email \n Message: $message \n";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'From: '.$email. "\r\n";
if(mail($to, $subject, $message, $headers))
{
echo "Mail Sent Successfully.";
}
else
{
echo "Error in Mail Sent.";
}
?>

Categories