mail function returns false - php

It has been asked multiple times here, but I can't find the answer.
I've tried to add headers etc but nothing seems to work.
My code returns "false" everytime I try to send the mails.
<?php
if(isset($_GET['action'])=='submitfunc') {
$EmailTo = prep($sObj->email);
$Subject = "The reason why your submission got rejected.";
$Message = Trim(stripslashes($_POST['Message']));
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <webmaster#website.com>' . "\r\n";
// prepare email body text
$Body = "";
$Body.= "Message: ";
$Body.= $Message;
// send email
$success = mail($EmailTo,$Subject,$Body,$headers);
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=succes.php\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
}else { ?>
<div class="emailrejection">
<form method="post" action="?action=submitfunc">
Dear <?php prep($sObj->username); ?>, <br>
<textarea placeholder="Describe why the image got rejected...." name="Message" rows="3" cols="20" id="Message"></textarea> <br>
All the best,,<br>
Moderator <?php prep($uObj->username); ?><br>
<input type="submit" name="submit" value="No + send message" class="btn btn-large btn-danger" />
</form>
</div>
I think the error is in line 3.
$EmailTo = prep($sObj->email);
If I replace this with:
$EmailTo = "myemail.gmail.com"; everthing works fine.
If I echo $EmailTo it also works fine. Why can't this be handeled in the mail function?

Related

php contact form was not working and not redirecting to success page

whats wrong in my code?
In HTML, I have taken form and i have created heading3 and i taken two inputs for name and email and i written textarea for message at last i taken another input is for submit and i closed the form tag.
<form method="post" action="contactengine.php">
<h3>Message Me</h3>
<input type="text" name="Name" id="Name" placeholder="Enter Name" required>
<input type="email" name="Email" id="Email" placeholder="Enter Email" required>
<textarea name="Message" id="Message" placeholder="Your Message"> </textarea>
<input type="submit" name="submit" value="Send Now" class="submit-button">
</form>
<?php
$EmailFrom = "xyz#gmail.com"; //your first email from where you want to send email
$EmailTo = "abc#gmail.com"; //your second email where you want to receive contact form content
$Subject = $_POST['Name']." Contacted you from your portfolio site"; //mail subject
$Name = Trim(stripslashes($_POST['Name'])); //getting name from html page
$Email = Trim(stripslashes($_POST['Email'])); //getting email from html page
$Message = Trim(stripslashes($_POST['Message'])); //getting message from html page
// validation
$validationOK=true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
exit;
}
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=index.html\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>
I would try this:
// send email
$success = mail($EmailTo, $Subject, $Body, "From:" . $EmailFrom);

How can we pass FORM data from included file to another page?

I had included footer.php in index file and the footer contains FORM for Subscriber Register.
when i click to submit a new subscriber email from https://mywebsite.com/index.php the value is not being passed from index.php page the same code is perfectly apply when i use url https://mywebsite.com/footer.php
footer.php
<form id="newsletterForm" action="newsletter-subscribe.php" method="POST">
<div class="input-group input-group-rounded">
<input name="mailid" type="email" placeholder="Email Address">
<span>
<input type="submit">
</span>
</div>
</form>
newsletter-subscribe.php
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$email = $_POST["mailid"];
$recipient = "info#myemail.com";
$email_content = "
<html>
<body>
<h2>Hello You had a new Subscriber!</h2>
<h3>Email Id : ".$email." </h3>
</body>
</html>
";
$email_headers = "MIME-Version: 1.0" . "\r\n";
$email_headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$email_headers .= 'From: info#myemail.com' . "\r\n";
if (mail($recipient, $subject, $email_content, $email_headers)) {
$msg= "Thanks You! For Subscribing our service.";
echo "<script type='text/JavaScript'>;window.location.href='../index.php';</script>";
} else {
$error= "Faild To Send Your Order. Please Try Again.";
echo "<script type='text/JavaScript'>alert ('$error');window.location.href='index.php';</script>";
}
} else {
$error1= "There was a problem with your submission, please try again.";
echo "<script type='text/JavaScript'>alert ('$error1');window.location.href='index.php';</script>";
}
?>

Upon submitting form, scroll to vertical position on page

I have created a simple html/php form where visitors on my site can write their name, email and message and then send the message to my email. Problem is that when they submit the email, my site then performs a full refresh (it looks like) and therefore just reloads to the top of my site. I would like for the user to remain at the same scroll position after submit, so that they can instantly see whether the submit was succesful or not. So either a solution that prevents the refresh or some other solution that automatically scrolls down vertical to the form.
Can you tell me if this is possible using php? Or do I have to use some jquery/ajax solution?
Below is the code I am using. I am a complete novice, so please be gentle.
<form action="" method="post" id="form">
<div class="contact-info-group">
<label for="name"><span>Your name</span>
<input type="text" id="name" name="name" autocomplete="off" value="<?php echo $name; ?>"></label>
<label for="email"><span>Your email</span>
<input type="email" id="email" name="email" autocomplete="off"></label>
</div>
<label for="message"><span>Your message</span>
<textarea id="message" name="message"></textarea></label>
<input id="button1" type="submit" class="button next" name="contact_submit" value="Send message">
<?php
// Check for header injections
function has_header_injection($str) {
return preg_match("/[\r\n]/", $str);
}
if (isset ($_POST['contact_submit'])) {
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$msg = $_POST['message'];
// Check to see if $name or $email have header injections
if (has_header_injection($name) || has_header_injection($email)) {
die();
}
if (!$name || !$email || !$msg) {
echo '<div class="contact-warning"><h2>! Error - Please note that all of the above fields are required !</h2></div>';
exit;
}
// Add the recipient email to a variable
$to = "email#email.com";
// Create a subject
$subject = "Message via website.com - $name";
// Construct the message
$message = "Name: $name\r\n";
$message .= "Email: $email\r\n";
$message .= "Message: \r\n\r\n$msg";
// Clean up the message
$message = wordwrap($message, 72);
// Set the mail headers into a variable
$headers = "MIME-Version 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "From: $name <$email> \r\n";
$headers .= "X-Priority: 1\r\n";
$headers .= "X-MSMail-Priority: High\r\n\r\n";
// Send the email
mail($to, $subject, $message, $headers);
echo '<div class="contact-warning"><h2>Thank you for your message. We will get back to you shortly.</h2></div>';
}
?>
</form>

Contact form submit error

I've got a simple contact form set up, but am having a little trouble with the finishing touches. I'm getting an error message whenever the form is submitted. Was hoping someone can take a look at my code and point out my mistake. Thanks!
(edited) Forgot to mention the error is that once form is submitted, I am being redirected to the error page (error.html) instead of the success (contactthanks.php) page.
HTML:
<form method="post" action="contactengine.php">
<label for="Name">Name:</label>
<input type="text" name="Name" id="Name" />
<label for="City">City:</label>
<input type="text" name="City" id="City" />
<label for="Email">Email:</label>
<input type="text" name="Email" id="Email" />
<label for="Message">Message:</label><br />
<textarea name="Message" rows="20" cols="20" id="Message"></textarea>
<input type="submit" name="submit" value="Submit" class="submit-button" />
</form>
PHP:
<?php
$EmailFrom = "";
$EmailTo = "MYEMAIL#gmail.com";
$Subject = "";
$Name = Trim(stripslashes($_POST['Name']));
$Tel = Trim(stripslashes($_POST['Tel']));
$Email = Trim(stripslashes($_POST['Email']));
$Message = Trim(stripslashes($_POST['Message']));
$validationOK=true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";
exit;
}
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Tel: ";
$Body .= $Tel;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=contactthanks.php\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";
}
?>
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=contactthanks.php\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";
}
It is redirecting to your error page because the call to mail is returning false (indicating the email was not accepted for delivery) and that is exactly what you are telling it to do in that situation.
You need to check the SMTP set up on your server.
Typically the settings to check for are sendmail_from and sendmail_path in your PHP.ini file. Seeing as you are already setting a From header, sendmail_path is likely to not be set up correctly.
See what's causing the problem with
ini_set("display_errors", "1");
error_reporting(E_ALL);`.
My guess is you have wrong smtp settings and mail function fails.

php to form script? syntax error unexpected

Hi Can anyone suggest a quick form script for a php form?
I generated the one below from telepro and modified the html and emails a bit, but I get this error for the checkbox
Parse error: syntax error, unexpected '=' in /home/inn.co.uk/public/mailer.php on line 14
thanks for your help
Regards
Judi
<form method="POST" action="contact.php">
<div class="form-field">
<input type="text" name="Name" onFocus="if(this.value=='Name')this.value='';" value="Name">
</div>
<div class="form-field">
<input type="text" name="Telephone" onFocus="if(this.value=='Telephone')this.value='';" value="Telephone">
</div>
<div class="form-field">
<input type="text" name="Timetocall" onFocus="if(this.value=='Time to call')this.value='';" value="Time to call">
</div>
<div class="form-field">
<ul class="tickboxes">
<li>Do you agree to our<br/>Terms of Business <input type="checkbox" name="DoyouagreetoourTermsofBusiness?" value="Yes" /></li></ul></div>
<div class="button">
<input type="image" src="images/submit.jpg" value="" name="submit">
</div>
</form>
<?php
// Website Contact Form Generator
// http://www.tele-pro.co.uk/scripts/contact_form/
// This script is free to use as long as you
// retain the credit link
// get posted data into local variables
$EmailFrom = "enquiry#inn.co.uk";
$EmailTo = "judith#yahoo.co.uk";
$Subject = "New enquiry";
$Name = Trim(stripslashes($_POST['Name']));
$Telephone = Trim(stripslashes($_POST['Telephone']));
$Timetocall = Trim(stripslashes($_POST['Timetocall']));
$DoyouagreetoourTermsofBusiness? = Trim(stripslashes($_POST['DoyouagreetoourTermsofBusiness?']));
// validation
$validationOK=true;
if (Trim($Name)=="") $validationOK=false;
if (Trim($Telephone)=="") $validationOK=false;
if (Trim($Timetocall)=="") $validationOK=false;
if (Trim($DoyouagreetoourTermsofBusiness?)=="") $validationOK=false;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
exit;
}
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Telephone: ";
$Body .= $Telephone;
$Body .= "\n";
$Body .= "Timetocall: ";
$Body .= $Timetocall;
$Body .= "\n";
$Body .= "DoyouagreetoourTermsofBusiness?: ";
$Body .= $DoyouagreetoourTermsofBusiness?;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=ok.htm\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>
You are using a question mark in your variable name. That doesn't work.
See the PHP Manual on variables for naming conventions.
replace
$DoyouagreetoourTermsofBusiness?
by
$DoyouagreetoourTermsofBusiness
and it will work fine.

Categories