Why contact form gives blank white screen when online? - php

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 } ?>

Related

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>");
}
?>

creating a contact page in HTML and PHP can getting a return error when testing (HTML 500)

So I am rather new to coding in these languages, I am getting a good understanding of how to write in them but not how to troubleshoot well. I went on and found some videos and read some sites to understand how to get this to work, and got this
HTML
<form id="Contact-form" method="post" action="contactform.php">
<input type="text" name="name" placeholder="Your Name" class="form-control" required>
<input type="text" name="Mail" placeholder="Your E-mail" class="form-control" required>
<input type="text" name="name" placeholder="Subject" class="form-control" required>
<textarea name="message" placeholder="Message" rows="7" class="form-control" required></textarea>
<button class="form-control submit" type="submit" name="sumbit"> Send message</button>
The PHP
<?php
if(isset($_POST['submit'])){{
$name = $_POST['name'];
$subject = $_POST['subject'];
$mailFrom = $_POST['mail'];
$message = $_POST['message'];
$mailTo = "myhostprovided#email"
$headers = "From: ".$mailFrom;
$txt = "You have received an e-mail from " .$name. ".\n\n".$message;
mail($name, $subject, $txt, $headers);
header("Location: Contact.php?mailsend")
}
I have it live and attempted to test send myself an email and I get a basic error (HTML Error 500) and no email, it changes the URL to "contactform.php" as expected. I followed the tutorials to the T, what am I doing wrong!
Thank you for the help in advance
Please change the codes from
<?php
if(isset($_POST['submit'])){{
$name = $_POST['name'];
$subject = $_POST['subject'];
$mailFrom = $_POST['mail'];
$message = $_POST['message'];
$mailTo = "myhostprovided#email"
$headers = "From: ".$mailFrom;
$txt = "You have received an e-mail from " .$name. ".\n\n".$message;
mail($name, $subject, $txt, $headers);
header("Location: Contact.php?mailsend")
}
to
<?php
if(isset($_POST['submit'])){
$name = $_POST['name'];
$subject = $_POST['subject'];
$mailFrom = $_POST['mail'];
$message = $_POST['message'];
$mailTo = "myhostprovided#email";
$headers = "From: ".$mailFrom;
$txt = "You have received an e-mail from " .$name. ".\n\n".$message;
mail($name, $subject, $txt, $headers);
header("Location: Contact.php?mailsend");
}

Contact form send vars right but not confirmed any way

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.

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.

PHP MailScript - Unable to receive email

For some reason i cant receive any email from my submit form - is there a problem with my formmail script?
<form action="contact_process.php" method="post" enctype="application/x-www-form-urlencoded" class="three">
<legend><strong>Form</strong></legend>
<fieldset>
<p>
<label for="name">Your Name</label>
<input type="text" name="name"></p>
<p>
<label for="email">Your Email</label>
<input type="text" name="email"></p>
<p>
<label for="subject">Subject</label>
<input type="text" name="subject"></p>
<p>
<label for="EnquiryType">Enquiry Type</label>
<select type="text" name="EnquiryType">
<option value="general">General</option>
<option value="other">Other</option>
</select></p>
<p>
<label for="message">Message</label>
<textarea type="text" name="message" class="msg"></textarea></p>
</fieldset>
<input type="submit" name="submit" value="Submit">
<input type="reset" name="Submit2" value="Reset">
</form>
contact_process.php
<?php
# bool has_injection (String $var, [String $var, ...])
function has_injection () {
$values = func_get_args();
for ($i=0, $count=func_num_args(); $i<$count; $i++) {
if ( stristr($values[$i], "%0A") || stristr($values[$i], "%0D") || stristr($values[$i], "\\r") || stristr($values[$i], "\\n")
|| stristr($values[$i], "Bcc") || stristr($values[$i], "Content-Type") ) {
return true;
}
}
return false;
}
$error = '';
if (isset($_POST) && count($_POST)>0) {
# The form has been submitted
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$EnquiryType = $_POST['EnquiryType'];
$message = $_POST['message'];
if ($name && $email && $subject && $EnquiryType && $message) {
if (has_injection($name, $email, $subject, $EnquiryType, $message)) {
# You've got another spammer at work here
$error = 'No spamming';
exit(0);
}
else {
# It's safe to send the message
mail('my#email.com', $subject, $message, $EnquiryType, $message,"From: $name <$email>");
}
}
else {
$error = 'Please fill in all the forms';
}
}
?>
u send 6 parameters..
i checked that and i get elow error
mail() expects at most 5 parameters, 6 given in C:\xampp\htdocs\parixan\contact_process.php on line 31
see here
update
$message = $_POST['EnquiryType']."\r\n".$_POST['message'];
$headers = 'From: $name <$email>' . "\r\n" .
'Reply-To: $name <$email>' . "\r\n" ;
then use
mail('my#email.com', $subject, $message, $headers);
I made a pretty good PHP email script that sends. I've been programming a website of my own (PHP, HTML) and I'm trying to find a way to receive emails. I've heard about POP3, and others but I can't ever find much on them.
<?php
$to = $_POST['to'];
$from = $_POST['from'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$body = "This email was sent from a no-reply email service. \n\n $message";
$headers = "From: $from";
mail($to, $subject, $body, $headers);
?>
Hope that helps ^^

Categories