Why isnt this PHP Mailer for HTML Forms working? - php

This is the PHP code:
<?PHP
$to = "adschweinfurth#gmail.com";
$subject = "New Finance Request";
$headers = "Request:";
$forward = 0;
$location = "";
$date = date ("l, F jS, Y");
$time = date ("h:i A");
$msg = "Below is the request sent on $date at $time.\n\n";
if ($_SERVER['REQUEST_METHOD'] == "POST") {
foreach ($_POST as $key => $value) {
$msg .= ucfirst ($key) ." : ". $value . "\n";
}
}
else {
foreach ($_GET as $key => $value) {
$msg .= ucfirst ($key) ." : ". $value . "\n";
}
}
mail($to, $subject, $msg, $headers);
if ($forward == 1) {
header ("Location:$location");
}
else {
echo "Thanks! Your request has been sent for approval and someone will be in contact with you soon!";
}
?>
This is the HTML code:
<form action="mailer.php" method="post">
<strong>Name:</strong><br />
<input type="text" name="Name" />
<br />
<br />
<strong>Email:</strong><br />
<input type="text" name="Email" />
<br />
<br />
<strong>Organization:</strong><br />
<input type="text" name="Org:" />
<br />
<br />
<strong>Amount:</strong><br />
<input type="text" name="Amount" />
<br />
<br />
<strong>Explain Request:</strong><br />
<textarea rows="5" cols="30" name="Message"></textarea>
<br />
<hr />
<input type="submit" name="submit" value="Submit">
</form>
I can not for the life of me get it to actually send the email. It goes to the thanks page, but I never receive any email...Let me know, comment below.

Put it up like this if email is really sent or if there is an error:
if (mail($to, $subject, $msg, $headers)){
// email sent
}
else {
echo 'some error occurred !';
}

Related

How to send multiple emails to only selected user's emails in php?

I am trying to send emails to only the users which i am selecting using checkbox from same index.php page. i am trying something here but i don't know that how to transfer and hold checked emails to "Bcc" field. here, is my code please have a look !
Php code for email (index.php) :
<?php
if (isset($_POST['submit']))
{
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$comments = $_POST['comments'];
$to = "";
$headers = "From:$name<$email>";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "Bcc: $selectedemailsall\r\n";
$message = "Name: $name\n\n Email: $email \n\n Subject : $subject \n\n Message : $comments";
if(mail($to,$subject,$message,$headers))
{
echo "Email Send";
}
else
{
echo "Error : Please Try Again !";
}
}
?>
Code for form (index.php) :
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Mail Document</title>
</head>
<body>
<form action="" method="post" >
<p>Name :<br>
<input type="text" name="name" id=""></p>
<p>Email :<br>
<input type="text" name="email" id=""></p>
<p>Subject :<br>
<input type="text" name="subject" id=""></p>
<p>Comments :<br>
<textarea name="comments" id="" cols="30" rows="10"></textarea></p>
<p><input type="submit" value="Send Email" name="SubmitEmail"></p>
</form>
<form action="#" method="post">
<?php
error_reporting(E_ERROR | E_PARSE);
$connection = mysqli_connect("localhost","root", "");
$db = mysqli_select_db("testdb", $connection);
$query = mysqli_query("select * from users", $connection);
while ($row = mysqli_fetch_array($query))
{
echo "
<input type='checkbox' name='check_list[]' value='{$row['email']}'>
<label>{$row['username']}</label><br/>
";
}
?>
<?php
if(isset($_POST['submituserchk']))
{
//to run PHP script on submit
if(!empty($_POST['check_list']))
{
// Loop to store and display values of individual checked checkbox.
foreach($_POST['check_list'] as $selectedemails)
{
$selectedemailsall = $selectedemails.",";
//echo $selectedemailsall;
}
}
}
?>
</div> <!-- End of RightUsersDivWithCheckBox -->
<input type="submit" name="submituserchk" style="margin-left: 87%; margin-top: 20px;" value="Done"/>
</form>
</body>
</html>
Any solution please how to do this ? right now when i click "Done" and submit emails nothing happens and i don't want to click "Done" button after selecting emails. I just select emails and they goes to "Bcc" field in a variable.
Don't use Bcc header for many users. Yo can make is:
Your form:
...
<input type="checkbox" name="email[]" value="foo#host.tld"> - foo#host.tld
<input type="checkbox" name="email[]" value="bar#host.tld"> - bar#host.tld
<input type="checkbox" name="email[]" value="baz#host.tld"> - baz#host.tld
...
Your backend code:
...
if (array_key_exists('email', $_POST) && is_array($_POST['email'])) {
foreach ($_POST['email'] as $to) {
mail($to, $subject, $message, $headers);
}
}
...
All emails sent separatelly for all recipients. This is flexible case for your application -- you can check for each send status.
Finally i got an answer to my own question. below is my code,
Display username, emails with checkbox from database :
$getemail = mysqli_query("SELECT * FROM users",$connection);
if(!$getemail) die('MsSQL Error: ' . mysqli_error());
echo '<div class="AllUserDiv" style="overflow-y:scroll;height:400px;"><table class="table table-bordered">';
echo "<thead>
<tr>
<th><input type='checkbox' onchange='checkedbox(this)' name='chk' /> </th>
<th>Username</th>
<th>Email</th>
</tr>
</thead>";
if(mysqli_num_rows($getemail) == 0)
{
echo "<tbody><tr><td colspan='3'> No Data Available</td></tr></tbody>";
}
while($row = mysqli_fetch_assoc($getemail))
{
echo "<tbody><tr><td><input value='".$row['email']."' type='checkbox' name='check[]' checked /> </td>";
echo "<td>".$row['username']."</td>";
echo "<td>".$row['email']."</td></tr></tbody>";
}
Email Form :
<form method="post" action="">
<p style="margin-top:30px;">Email Subject: <input type="text" name="subject" value="" class="form-control" /></p>
<p>Email Content: <textarea name="message" cols="40" rows="6" style="width:100%;"></textarea></p>
<center><input type="submit" name="submit" value="Send Email Now" class="btn btn-primary btn-block" />
</form>
JavaScript for making checkbox selection :
<script type="text/javascript" language="javascript">
function checkedbox(element)
{
var checkboxes = document.getElementById('input');
if(element.checked)
{
for (var i = 0; i < checkboxes.length; i++ )
{
if(checkboxes[i].type == 'checkbox')
{
checkboxes[i].checked = true;
}
}
}
else
{
for (var i = 0; i < checkboxes.length; i++)
{
console.log(i)
if(checkboxes[i].type == 'checkbox')
{
checkboxes[i].checked = false;
}
}
}
}
</script>

simple php captcha contact form

i am trying to create simple contact form with captcha in php. However it turns out implementing captcha is out of my league.
I found a simple answer on stackoverflow opn similar problem which pushed me 1 step closer to the end, but again i got stuck.
So i need a contact form that only check if text is entered and if correct captcha is answered, email is not mandatory.
</br>
<?php
$a=rand(2,9);
$b=rand(2,9);
$c=$a+$b;
if (isset($_POST['contact_text']) && isset($_POST['contact_email']) ) {
$contact_text = $_POST['contact_text'];
$contact_email = $_POST['contact_email'];
$recaptcha = $_POST['recaptcha'];
$info = 'Pranešimas apie korupciją: ';
$sender = 'Atsiuntė: ';
if (!empty($contact_text) && ($recaptcha == $c )) {
echo $recaptcha;
$to = 'muksinovas#gmail.com';
$subject = 'Korupcija';
$body = $sender."\n".$contact_email."\n".$info."\n".$contact_text;
$headers = 'From: '.$contact_email;
if (#mail($to,$subject, $body, $headers)) {
echo 'Jūsų pranešimas sėkmingai išsiustas. ';
} else {
} echo 'Įvyko klaida, bandykite dar karta.';
} else {
echo 'Neteisingai užpildyta forma.';
}
}
?>
<form action="contact1.php" method="post">
Pranešimas apie korupciją:<br><textarea name="contact_text" rows="6" cols="30" maxlength="1000" ></textarea><br><br> <!-- -->
Email (nebūtinas):<br><input type="text" name="contact_email" maxlength="30">
<?php echo $a."+".$b."="?><input type="number" name="recaptcha" maxlength="2" style="width:40px" />
<input type="submit" value="Siusti">
<br>
</form>
Now the problem is that I always get the message that details are incorrect. I tried to echo recaptcha just to see if $c is correct and it works. But for some reason not able to compare $recaptcha with $c or some other issue I am not sure.
The value of $c will be a completely different value when the user submits the contact form vs when your validation checks it. The value will change on every request because the script is re-interpreted.
You will have to save the value of $c on the initial page load, so that you can compare it afterwards in the next request. You can do that by storing it in $_SESSION.
You can write this
<?php
$min_number = 2;
$max_number = 9;
$random_number1 = mt_rand($min_number, $max_number);
$random_number2 = mt_rand($min_number, $max_number);
if (isset($_POST['contact_text']) && isset($_POST['contact_email']) ) {
$contact_text = $_POST['contact_text'];
$contact_email = $_POST['contact_email'];
$recaptcha = $_POST['recaptcha'];
$firstNumber = $_POST["firstNumber"];
$secondNumber = $_POST["secondNumber"];
$checkTotal = $firstNumber + $secondNumber;
$info = 'Pranešimas apie korupciją: ';
$sender = 'Atsiuntė: ';
if (!empty($contact_text) && ($recaptcha != $checkTotal )) {
echo $recaptcha;
$to = 'muksinovas#gmail.com';
$subject = 'Korupcija';
$body = $sender."\n".$contact_email."\n".$info."\n".$contact_text;
$headers = 'From: '.$contact_email;
if (#mail($to,$subject, $body, $headers)) {
echo 'Jūsų pranešimas sėkmingai išsiustas. ';
} else {
} echo 'Įvyko klaida, bandykite dar karta.';
} else {
echo 'Neteisingai užpildyta forma.';
}
}
?>
<form action="contact1.php" method="post">
Pranešimas apie korupciją:<br><textarea name="contact_text" rows="6" cols="30" maxlength="1000" ></textarea><br><br> <!-- -->
Email (nebūtinas):<br><input type="text" name="contact_email" maxlength="30">
<?php
echo $random_number1 . ' + ' . $random_number2 . ' = ';
?>
<input type="number" name="recaptcha" maxlength="2" style="width:40px" />
<input name="firstNumber" type="hidden" value="<?php echo $random_number1; ?>" />
<input name="secondNumber" type="hidden" value="<?php echo $random_number2; ?>" />
<input type="submit" value="Siusti">
<br>
</form>
This might solve your problem
you should to use session to solve your problem, i did little changes in your code, it should to work perfectly.
<?php
#session_start();
if (isset($_POST['contact_text']) && isset($_POST['contact_email']) ) {
$contact_text = $_POST['contact_text'];
$contact_email = $_POST['contact_email'];
$recaptcha = $_POST['recaptcha'];
$info = 'Pranešimas apie korupciją: ';
$sender = 'Atsiuntė: ';
if (!empty($contact_text) && ($recaptcha == $_SESSION["captcha"])) {
echo $recaptcha;
$to = 'muksinovas#gmail.com';
$subject = 'Korupcija';
$body = $sender."\n".$contact_email."\n".$info."\n".$contact_text;
$headers = 'From: '.$contact_email;
if (#mail($to,$subject, $body, $headers)) {
echo 'Jūsų pranešimas sėkmingai išsiustas. ';
} else {
} echo 'Įvyko klaida, bandykite dar karta.';
}else{
echo 'Neteisingai užpildyta forma.';
}
}else{
$a=rand(2,9);
$b=rand(2,9);
$c=$a+$b;
//setting captcha code in session
$_SESSION["captcha"] = $c;
?>
<form action="contact1.php" method="post">
Pranešimas apie korupciją:<br><textarea name="contact_text" rows="6" cols="30" maxlength="1000" ></textarea><br><br> <!-- -->
Email (nebūtinas):<br><input type="text" name="contact_email" maxlength="30">
<?php echo $a."+".$b."="?><input type="number" name="recaptcha" maxlength="2" style="width:40px" />
<input type="submit" value="Siusti">
<br>
</form>
<?php
}
?>

contact form, message is not recived

I need to make a contact form and I did, but when Im sending test message Im not reciving that message, I have check my email addres and test message never get there i also looked into spam folder but still no luck how to fix this? here is my code
<?php
$formMessage = "";
$senderName = "";
$senderEmail = "";
$senderMessage = "";
if ($_POST['username']) { // If the form is trying to post value of username field
// Gather the posted form variables into local PHP variables
$senderName = $_POST['username'];
$senderEmail = $_POST['email'];
$senderMessage = $_POST['msg'];
// Make sure certain vars are present or else we do not send email yet
if (!$senderName || !$senderEmail || !$senderMessage) {
$formMessage = "The form is incomplete, please fill in all fields.";
} else { // Here is the section in which the email actually gets sent
// Run any filtering here
$senderName = strip_tags($senderName);
$senderName = stripslashes($senderName);
$senderEmail = strip_tags($senderEmail);
$senderEmail = stripslashes($senderEmail);
$senderMessage = strip_tags($senderMessage);
$senderMessage = stripslashes($senderMessage);
and here is part which im not sure about
// End Filtering
$to = "test#gmail.com";
$from = "contact#your_website_here.com";
$subject = "You have a message from your website";
// Begin Email Message Body
$message = "
$senderMessage
$senderName
$senderEmail
";
// Set headers configurations
$headers = 'MIME-Version: 1.0' . "rn";
$headers .= "Content-type: textrn";
// Mail it now using PHP's mail function
mail($to, $subject, $message, $headers);
$formMessage = "Thanks, your message has been sent.";
$senderName = "";
$senderEmail = "";
$senderMessage = "";
} // close the else condition
} // close if (POST condition
?>
here is html form
<form id="form1" name="form1" method="post" action="contact.php">
<?php echo $formMessage; ?>
<br />
<br />
Your Name:<br />
<input name="username" type="text" id="username" size="36" maxlength="32" value="<?php echo $senderName; ?>" />
<br />
<br />
Your Email:
<br />
<input name="email" type="text" id="email" size="36" maxlength="32" value="<?php echo $senderEmail; ?>" />
<br />
<br />
Your Message:
<br />
<textarea name="msg" id="msg" cols="45" rows="5"><?php echo $senderMessage; ?> </textarea>
<br />
<br />
<br />
<input type="submit" name="button" id="button" value="Send Now" />
</form>

Contact form not providing error, confirmation or email

I've used this same form a number of times but this particular occasion it doesn't seem to actually be sending any email nor providing me any error messages or confirmation.
The output of the: print_r($_POST); from a test just a moment ago is the following:
Array ( [Name] => test name [Email] => test#test.com [Company] => example company [Position] => ceo [Captcha] => 1 )
So the form is grabbing all details correctly and attempting to pass them on, but no email or confirmation/error(s).
PHP / HTML
<?php
if (count($error)>=1) {
foreach($error as $one_error) {
echo '<h3>'.$one_error.'</h3>';
echo '<div class="clear"></div>';
}
}
if ($message_sent) {
echo '<h2>Thank you for your request. We will be in touch soon!</h2>';
echo '<hr class="hr" />';
}
?>
<form action="index.php" method="POST">
<span>
<label><strong>Name</strong></label><br />
<input type="text" name="Name" placeholder="Name" value="<?=$Name;?>" required />
</span>
<span>
<label><strong>Your Email Address</strong></label><br />
<input type="text" name="Email" placeholder="Email Address" value="<?=$Email;?>" required />
</span>
<br /><br />
<span>
<label><strong>Company</strong></label><br />
<input type="text" name="Company" placeholder="Company" value="<?=$Company;?>" required />
</span>
<span>
<label><strong>Position</strong></label><br />
<input type="text" name="Position" placeholder="Company Position" value="<?=$Position;?>" required />
</span>
<br /> <br />
<label><strong>Captcha</strong></label><br />
<img src="<?php HTTP_HOST ?>/Contact/answer.php">
<input type="text" placeholder="Enter Sum Calculation" name="Captcha" value="<?=$Captcha;?>" required />
<div class="clear"></div><br />
<input type="submit" value="Send Enquiry" />
</form>
PHP (Inside of the same index.php file)
<?php
session_start();
print_r($_POST);
// print_r($_SESSION['code']);
function isValidInetAddress($data, $strict = false) {
$regex = $strict ? '/^([.0-9a-z_-]+)#(([0-9a-z-]+\.)+[0-9a-z]{2,4})$/i' : '/^([*+!.&#$¦\'\\%\/0-9a-z^_`{}=?~:-]+)#(([0-9a-z-]+\.)+[0-9a-z]{2,4})$/i';
if (preg_match($regex, trim($data), $matches)) {
return array($matches[1], $matches[2]);
} else {
return false;
}
}
$Name = '';$Email = '';$Company = '';$Position = '';$Captcha = '';
if (isset($_POST)&&(isset($_POST['Submit'])&&(strlen($_POST['Submit'])>1))) {
$error = array();
if (isset($_POST['Name'])&&(strlen($_POST['Name'])>1)) {
$Name = $_POST['Name'];
} else {
$error[]='Name is empty';
}
if (isset($_POST['Email'])&&(strlen($_POST['Email'])>1)) {
$Email = $_POST['Email'];
} else {
$error[]='Email is empty';
}
if (isset($_POST['Company'])&&(strlen($_POST['Company'])>1)) {
$Company = $_POST['Company'];
} else {
$error[]='Company is empty';
}
if (isset($_POST['Position'])&&(strlen($_POST['Position'])>1)) {
$Position = $_POST['Position'];
} else {
$error[]='Position is empty';
}
if (isset($_POST['Captcha'])&&($_POST['Captcha']==$_SESSION['code'])) {
} else {
$error[]='Captcha is wrong';
}
if (count($error)<1) {
$headers = 'From: jordan#gmail.com' . "\r\n" .
'Reply-To: jordan#gmail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$message ='New DiSC Profile Request'."\r\n";
$message .= 'Name: '.$Name."\r\n";
$message .= 'Email: '.$Email."\r\n";
$message .= 'Company: '.$Company."\r\n";
$message .= 'Position: '.$Position."\r\n";
mail('jordan#gmail.com', 'New DiSC Profile Request', $message, $headers);
// mail('galin#mangcreative.com', 'New Enquiry from Urban Country', $message, $headers);
unset($Name);unset($Email);unset($Company);unset($Position);
$message_sent = TRUE;
}
}
?>
Change the form submit button and give it a name
<input type="submit" value="Send Enquiry" />
to
<input type="submit" value="Send Enquiry" name="Submit"/>
You need to have name="" on the button;
<input type="submit" value="Send Enquiry" name="submit" />
You also don't need to have action="index.php" if the php is on the same page;
<form action="" method="POST">
Also you don't need
if (isset($_POST)&&(isset($_POST['Submit'])&&(strlen($_POST['Submit'])>1))) {
Use: if (isset($_POST['submit'])===true) {

jQuery.post not POSTing

I am trying to use jQuery to post data to an emailus.php script.
Here is the script part:
<script>
jQuery(document).ready(function(){
jQuery('#submitt').click(function(){
jQuery.post("/emailus.php", jQuery("#mycontactform").serialize(), function(response) {
jQuery('#success').html(response);
});
return false;
});
});
</script>
and here is the HTML used:
<form action="" method="get" id="mycontactform" >
<label for="name">Your Name:</label><br />
<input type="text" name="name" class="cleann" /><br />
<label for="email">Your Email:</label><br />
<input type="text" name="email" class="cleann" /><br />
<label for="message">Your Message:</label><br />
<textarea name="message" class="cleann" rows="7"></textarea><br />
<input type="button" value="send" id="submitt" class="cleannsubmit" /><div id="success" style="color:green;"></div>
</form>
and here is the php script:
<?php
// Here we get all the information from the fields sent over by the form.
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = 'nohanada#gmail.com';
$subject = 'Fortrove Contact';
$message = 'FROM: '.$name.' Email: '.$email.'Message: '.$message.'\n\nItem:'.$itemname;
print_r($_POST);
if($name && $email && $message){
if (eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)){
mail($to, $subject, $message);
echo "Your email was sent!";
}
else echo "<span style='color:red'>Invalid email format.</span>";
}
else echo "<span style='color:red'>Please fill all fields</span>";
?>
Problem is that it does not POST the actual fields to the php script. What am i doing wrong?
you have placed the mycontactform inside product_addtocart_form that is the reason which is not allowed so the browser seems to be remocing the mycontactform

Categories