Contact form send vars right but not confirmed any way - php

thanks for your help. I hope you can help me.
I have this contact form:
<form action="assets/php/sendmail.php" method="post" id="contactForm">
<p class="text_cont"><input id="name" input type="text" name="name" placeholder="<?php echo FORM_PLACEHOLDER_1 ?>" required></p>
<p class="text_cont"><input id="email" input type="text" name="email" placeholder="<?php echo FORM_PLACEHOLDER_2 ?>" required></p>
<p class="text_cont"><input id="subject" input type="text" name="subject" placeholder="<?php echo FORM_PLACEHOLDER_3 ?>"></p>
<p class="text_cont"><textarea id="message" textarea name="message" placeholder="<?php echo FORM_PLACEHOLDER_4 ?>"></textarea></p>
<div class="col-lg-4 col-md-3 col-sm-3"><p><input type="submit" id="send" class="btn btn-default" value="<?php echo SEND_PLACEHOLDER ?>" /></p></div>
sendmail.php has this code because the host company recommends it (windows servers), I have modified a little:
error_reporting( E_ALL & ~( E_NOTICE | E_STRICT | E_DEPRECATED ) );
require_once "Mail.php";
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$subject = htmlspecialchars($_POST['subject']);
$message = htmlspecialchars($_POST['message']);
$to = 'my#mail.com';
$from = $email;
$host = 'smtp.myhost.com';
$username = 'usernameformail';
$password = 'passwordformail';
$body =
utf8_decode("This mail has been written from your web: \r\n") .
utf8_decode("Name: " . $name . "\r\n") .
utf8_decode("Email: " . $email . "\r\n") .
utf8_decode("Subject: " . $subject . "\r\n") .
utf8_decode("Message: " . $message);
$headers = array (
'From' => $from,
'To' => $to,
'Subject' => $subject,
);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
};
$name = '';
$email = '';
$subject = '';
$message = '';
?>
So, all works fine. However, when I write code in "if (PEAR::isError($mail))" to redirect to other page to know if is an error or success, it doesn't works. For example:
if (PEAR::isError($mail)) {
header('Location: http://www.myweb.com/error.php');
} else {
header('Location: http://www.myweb.com/thanks.php');
};
If I write an script, it's the same:
if (PEAR::isError($mail)) {
echo "<script type='text/javascript'> redirect in javascript to error </script>";
} else {
echo "<script type='text/javascript'> redirect in javascript to thanks</script>";
};
Mail is always sent correctly to me, but someone who send the mail never know if it has been sent or not. It's like "if/else" doesn't work. I click on submit button and nothing happen (mail send correctly but people who write it don't know that).
Do you see where is my mistake?
Thanks a lot.

Related

PHP form with image upload

I'm trying to make a simple contact form page with a combination of HTML and PHP with an upload image file section to it. I am 99% sure I got the HTML part correct but I am just lost with the PHP part.
So here is my HTML
<form class="" action="contactform.php" method="post">
<input type="text" name="firstname" placeholder="First Name" required>
<input type="text" name="lastname" placeholder="Last Name" required>
<input type="text" name="mail" placeholder="Your e-mail" required>
<input type="text" name="phone" placeholder="Phone">
<input type="file" id="myFile" name="filename">
<textarea name="message" placeholder="Message" required></textarea>
<button type="submit" name="submit">Send</button>
</form>
And here is my PHP
if (isset($_POST['submit'])) {
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$phone = $_POST['phone'];
$mailFrom = $_POST['mail'];
$message = $_POST['message'];
$mailTo = "email#email.com";
$headers = "From: ".$mailFrom;
$subject = "You have received an e-mail from your web site";
$message = "First Name: " . $firstname . " Last Name: " . $lastname . "\r\nPhone: " . $phone . "\r\nemail: " . $mailFrom . "\r\nMessage: " . $message;
$retval = mail( $mailTo, $subject, $message, $headers );
if ( $retval == true ){
echo "<h1>Message Sent</h1>
<p>Thank you for contacting us your message has been sent successfully and we will get back to you a soon as possible.</p>";
}
else {
echo "<h1>Message Sent</h1><p>Message could not be sent Mail server did not accept mail.</p>";
}
}
Does anyone know how I can make the upload file part work?
try changing if($retval==true) to if($retval!=false) because I believe it only returns false if it fails and if it is successful it will return a hash value, so I believe making sure it's not true will work.
Also just you might want to take mail( $mailTo, $subject, $message, $headers ); and copy it into its own function, but I'm not sure. Kinda like this:
$retval = mail( $mailTo, $subject, $message, $headers );
mail( $mailTo, $subject, $message, $headers );
I'm not sure though, I hope this works, sorry if it doesn't!

PHP HTML Contact us form issue

I am using html ( bootstrap4) contact us page and trying to make it dynamic . server test ok with the php test script using pear mail.
But issue when trying to make dynamic and calling filed out put in email its not working
Can any one review below php script and tell me where is issue
HOW can I call here this part : $email_subject ? email body ? and email message ?
<form id="contact-form" method="post" action="test.php">
<div class="row">
<!-- Name -->
<div class="col-md-6 mb-2">
<input type="text" name="Full_Name" class="form-control" placeholder="Firstname *" required="required" data-error="Firstname is required.">
</div>
<!-- Email -->
<div class="col-md-6 mb-2">
<input type="email" name="email" class="form-control" placeholder="email *" required="required" data-error="Valid email is required.">
</div>
<!-- subject -->
<div class="col-md-12 mb-2">
<input type="text" name="subject" class="form-control" placeholder="subject *" required="required" data-error="Valid subject is required.">
</div>
<!-- Message -->
<div class="col-md-12 mb-2">
<textarea name="message" class="form-control" placeholder="Message *" rows="4" required="required" data-error="Please,leave us a message."></textarea>
</div>
<!-- Submit Button -->
<div class="col-12 text-right">
<input type="submit" class=" form-control btn btn-success btn-send" value="Send message">
</div>
</div>
</form>
<!-- Email -->
<div class="col-md-6 mb-2"> <input type="email" name="email" class="form-control" placeholder="email *" required="required" data-error="Valid email is required."> </div>
<?php
error_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED ^ E_STRICT);
require_once "Mail/Mail-1.4.1/Mail.php";
$name = $_POST["Full_Name"];
$email = $_POST["email"];
$subject = $_POST["subject"];
$message = $_POST["message"];
$host = "*******"; <!--i will use my hosting server url-->
$username = "*******"; <!--i will use my email-->
$password = "*****"; <!--i will use here my email pss-->
$port = "2525";
$to = "******"; <!--i will use the email here-->
$email_from = "*****"; <!--how to call email from here by input that i defined above in php file->
$email_subject = "******" ; <!--how to call sujectl from here by input that i defined above in php file->
$email_body = "*****"; <!--how to call message from here by input that i defined above in php file->
$email_address = "*****";
$headers = array ('From' => $email, 'To' => $to, 'Subject' => $subject, 'Message' => $message, Reply-To' => $email_address);
$smtp = Mail::factory('smtp', array ('host' => $host, 'port' => $port, 'auth' => true, 'username' => $username, 'password' => $password));
$mail = $smtp->send($to, $headers, $email_body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>
I've done a slight revamp of the code which might help, also if you're using gmail to send the email please see the last bit of this article: https://help.dreamhost.com/hc/en-us/articles/216140597-How-do-I-send-PHP-mail-via-SMTP-
<?php
error_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED ^ E_STRICT);
require_once "Mail/Mail-1.4.1/Mail.php";
$name = $_POST["Full_Name"];
$email = $_POST["email"];
$subject = $_POST["subject"];
$message = $_POST["message"];
$host = "*******"; <!--i will use my hosting server url-->
$username = "*******"; <!--i will use my email-->
$password = "*****"; <!--i will use here my email pss-->
$port = "2525";
$to = "******"; <!--i will use the email here-->
$reply_to = "*****";
// Please note the change to the headers
$headers = array ('From' => $email, 'To' => $to, 'Subject' => $subject, Reply-To' => $reply_to);
$smtp = Mail::factory('smtp', array ('host' => $host, 'port' => $port, 'auth' => true, 'username' => $username, 'password' => $password));
$mail = $smtp->send($to, $headers, $message);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>
After carefully reading the code and optimized slightly its working pefrectly and here i am pasting it.. might it be helpful for some one facing the same issue with html/php/pear
<?php
error_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED ^ E_STRICT);
require_once "Mail/Mail-1.4.1/Mail.php";
$name = $_POST["Full_Name"];
$email = $_POST["email"];
$subject = $_POST["subject"];
$message = $_POST["message"];
$host = "mail.example.com";
$username = "xyz#example.com";
$password = "abc";
$port = "25";
$to = "xyz#example.com";
$reply_to = "xyz#example.com";
$headers = array ('From' => $email, 'To' => $to, 'Subject' => $subject, 'Reply-To' => $reply_to);
$smtp = Mail::factory('smtp', array ('host' => $host, 'port' => $port, 'auth' => true, 'username' => $username, 'password' => $password));
$mail = $smtp->send($to, $headers, $message);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>

PHP PEAR Mail sends 2 emails [duplicate]

This question already has answers here:
Blank PHP Emails
(2 answers)
Closed 5 years ago.
This script works, but it sends a blank email when it is visited. Then a real one when submitted. I want to get rid of the first blank one it sends. This is mostly edited code I found here and another place I pieced together to work for my needs.
<?php
ini_set("include_path", '/home/user/php:' . ini_get("include_path") );
require_once "Mail.php";
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$address = $_POST['address'];
$phone = $_POST['phone'];
$contact = $_POST['contact'];
$comments = $_POST['comments'];
$from = "Info <info#domain.net>";
$to = "Info <info#domain.net>";
$subject = "Customer Contact Form";
$body = "If this works it will be edited a bit \n" . "First Name: " . $first_name . "\n" . "Last Name: " . $last_name . "\n" . "Email: " . $email . "\n" . "Address: " . $address . "\n" . "Phone: " . $phone . "\n" . "Please contact me by: " . $contact . "\n" . "Other Comments: " . $comments;
$host = "mail.domain.net";
$username = "info#domain.net";
$password = "emailpassword";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
//if (PEAR::isError($mail)) {
// echo("<p>" . $mail->getMessage() . "</p>");
//} else {
// echo("<p>Message successfully sent!</p>");
//}
?>
<!DOCTYPE html>
<head>
<title>Form submission</title>
</head>
<body>
<form action="" method="post">
First Name: <input type="text" name="first_name"><br>
Last Name: <input type="text" name="last_name"><br>
Email: <input type="text" name="email"><br>
Address: <input type="text" name="address"><br>
Phone: <input type="text" name="phone"><br>
Perferred Form of Contact: <input type="text" name="contact"><br>
Message:<br><textarea rows="5" name="comments" cols="30"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
Because you don't check the form data!
Just use a simple if(isset($_POST['first_name'])) around your code for generating and sending the mail and it will just send an email if you've sent the form.

Why contact form gives blank white screen when online?

I'm trying to make php contact form work on webhost we are currently using. I found with google this trick to get it work when smtp is off:
http://www.top-answers.net/webhost/web-hosting.html
What I found it only gives me white screen on browser when I upload it to server but as offline it shows the form correctly.
Is there way to make this work or should I seek for another solution?
Thank you
<?php
function has_header_injection($str) {
return preg_match( "/[\r\n]/", $str );
}
if (isset ($_POST['contact_submit'])) {
$name = trim($_POST['name']);
$company = trim($_POST['company']);
$email = trim($_POST['email']);
$phone = trim($_POST['phone']);
$msg = $_POST['message'];
if (has_header_injection($name) || has_header_injection($company) ||has_header_injection($email) ||has_header_injection($phone)) {
die();
}
if ( !$name || !$company || !$email || !$phone || !$msg ) {
echo '<h4 class="error">Kaikki kentät ovat pakollisia.</h4>Palaa takaisin ja yritä uudelleen';
exit;
}
$to = "info#mydomain.com";
$subject = "$name lähetti viestin lomakkeen kautta";
$message = "Nimi: $name\r\n";
$message .= "Yritys: $company\r\n";
$message .= "Sähköposti: $email\r\n";
$message .= "Puhelin: $phone\r\n";
$message .= "Viesti:\r\n$msg";
if (isset($_POST['subscribe']) && $_POST['subscribe'] == 'Subscribe') {
$message .= "\r\n\r\n$name tilasi uutiskirjeen. Muista lisätä sähköpostilistalle!\r\n";
}
$message = wordwrap($message, 72);
require_once "Mail.php";
$from = "info#mydomain.com"; \\the mail-iD under your domain
$to = "myemail#myemail.com"; \\the TO gmail ID
$subject = "PHP Enquiry form";
$body = $email_message;
$host = "mail.mydomain.com"; \\ your domain mail server
$username = "info#mydomain.com"; \\the mail-id under your domain
$password = "secretsecurepassword"; \\your password for yourid#mydomain.com
$headers = array ('From' => $from, 'To' => $to, 'Reply-To'=> $email_from, 'Subject' => $subject);
$smtp = Mail::factory('smtp', array ('host' => $host, 'auth' => true, 'username' => $username, 'password' => $password));
$mail = $smtp->send($to, $headers, $body);
?>
<h5>Kiitos yhteydenotosta!</h5>
<p>Vastaamme kahden arkipäivän kuluessa.</p>
<?php } else { ?>
<form method="post" action="" id="contact">
<label for="name">Nimi</label>
<input type="text" id="name" name="name">
<label for="company">Yritys</label>
<input type="text" id="company" name="company">
<label for="email">Sähköposti</label>
<input type="email" id="email" name="email">
<label for="phone">Puhelin</label>
<input type="text" id="phone" name="phone">
<label for="message">Viesti</label>
<textarea id="message" name="message"></textarea>
<input type="checkbox" id="subscribe" name="subscribe" value="Subscribe">
<label for="subscribe">Tilaa uutiskirje</label>
<button type="submit" class="button next" name="contact_submit">Lähetä</button>
</form>
<?php } ?>

Unsuccessful attempt to send email from gmail.com

I have file with name contact.php, inside I have form:
<form method="post" name="kontakt" action="send_mail.php">
<fieldset class="formularz_kontaktowy">
<legend>Formularz kontaktowy</legend>
<div><label id="lblStatus"></label></div>
<div><input type="text" name="txtName" title="Imię i nazwisko" id="txtName" class="text"></div>
<div><input type="text" name="txtEmail" title="Email" id="txtEmail" class="text"></div>
<div><input type="text" name="txtTitle" title="Tytuł" id="txtTitle" class="text"></div>
<div><textarea cols="30" rows="10" name="txtMessage" id="txtMessage" class="text" title="Treść wiadomości"></textarea></div>
<input type="submit" name="btnSendmail" value=Send">
</fieldset>
</form>
send_mail.php
<?php
require_once "Mail.php";
$from = "<mailaddress#gmail.com>";
$to = "<mail2#gmail.com>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "ssl://smtp.gmail.com";
$port = "465";
$username = "<mailaddress#gmail.com>";
$password = "password";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>
after click btnSendmail in browser I have URL, for example: localhost/contact.php?txtName=Mary+Smith&txtEmail=mailMail%40gmail.com&txtTitle=dfd&txtMessage=fgdf&btnSendmail=Send, but mail doesn'y sent and I have not message. Why?
Try changing
$username = "<mailaddress#gmail.com>";
to
$username = "mailaddress#gmail.com";
I found problem. I had two tags , one in template and second, nested in contact.php. Thanks a lot for help.

Categories