Not picking up clients email address to send using MAIL function - php

<html>
<head>
<title>Max Design contact page</title>
<link href="mdstyle1.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="container">
<?php
$required = array("firstName" => "First Name",
"surname" => "Surname",
"email" => "Email Address",
"telephone" => "Telephone");
foreach($required as $field => $label) {
if (!$_POST[$field]) {
$warnings[$field] = "*";
}
}
if ($_POST["firstName"] &&
!ereg("[a-zA-Z]", $_POST["firstName"]))
$warnings["firstName"] = "Please check First Name for errors";
if ($_POST["surname"] &&
!ereg("[a-zA-Z]", $_POST["surname"]))
$warnings["surname"] = "Please check Surname for errors";
if ($_POST["email"] &&
!ereg("^[^#]+#([a-z\-]+\.)+[a-z]{2,4}$", $_POST["email"]))
$warnings["email"] = "Please check email for errors";
if ($_POST["telephone"] &&
!ereg("[0-9]", $_POST["telephone"]))
$warnings["telephone"] = "Please check Telephone for errors";
if (count($warnings) > 0) {
// sets the description of the sections of the form and must have an entry for each form element
$description = array();
$description{"firstName"} = "First Name";
$description{"surname"} = "Surname";
$description{"telephone"} = "Telephone Number";
$description{"email"} = "Email Address";
?>
<div class="main-paragraph">
<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<br />
<div class="warnings"> * Please fill in these boxes </div>
<br />
<br />
<TABLE BORDER=0>
<TR>
<TD><label>First Name</label>
</TD><TD><INPUT TYPE=TEXT SIZE=30 NAME="firstName"
VALUE="<?php echo $_POST["firstName"];?>"></TD>
<TD><div class="warnings"><?php echo $warnings["firstName"];?></div></TD>
</TR>
<TR>
<TD><label>Surname</label>
</TD><TD><INPUT TYPE=TEXT SIZE=30 NAME="surname"
VALUE="<?php echo $_POST["surname"];?>"></TD>
<TD><div class="warnings"><?php echo $warnings["surname"];?></div></TD>
</TR>
<TR>
<TD><label>Email Address</label>
</TD>
<TD><INPUT TYPE=TEXT SIZE=30 NAME="email"
VALUE="<?php echo $_POST["email"];?>"></TD>
<TD><div class="warnings"><?php echo $warnings["email"];?></div></TD>
</TR>
<TR>
<TD><label>Telephone</label>
</TD>
<TD><INPUT TYPE=TEXT SIZE=15 NAME="telephone"
VALUE="<?php echo $_POST["telephone"];?>"></TD>
<TD><div class="warnings"><?php echo $warnings["telephone"];?></div></TD>
</TR>
</TABLE>
<br />
<INPUT TYPE=SUBMIT VALUE="Send Enquiry">
</FORM>
</div> <!-- end of main paragraph -->
<?php
}
else { // start of send section
//stuff that goes to the enquirer
$clients_email = "From: sales#rmrc.co.uk";
$headers2 = "From: simon#maxdesign.org.uk";
$subject2 = "Thank you for contacting MAX Design";
$autoreply = "Thank you for contacting MAX Design. We will get back to you as soon as possible,
\n usualy within 48 hours. If you have any more questions,
\n please consult our website at www.maxdesign.org.uk/index";
//prints out each field's title and contents in turn each on new line
$body = "A quote request from the website:\n\n";
foreach($_POST as $description => $value) {
$body .= sprintf("%s = %s\n", $description, $value);
}
mail($email, $subject2, $autoreply, $headers2);
mail("sim.on#hotmail.co.uk", "MAX Design website enquiry", $body, $email);
header( "Location: http://www.maxdesign.org.uk/thank-you-for-quote-max-design.html" );
} //end of send section
?>
</div><!--end of container/wrapper div -->
</body>
</html>
I am trying to send a fairly crudely formatted email (I am trying to get the email formatted a little bit using the $description variable but that isn't working either) from the clients website but the script won't send it to the clients email using the $email string so am having no luck. Have been at this for days now so any help really appreciated.
Simon

You are sending a variable called $email to your mail() which is not at all defined in the code. That is why your mail is not being sent. See below
mail($email, $subject2, $autoreply, $headers2);
//^^^^^ Undefined variable
Solution : Set the $email to some email.

Related

Google Recaptcha box not working. Works if it isn't ticked, but not if it is ticked

This is my enquiry.php file, the code you can see is for the recaptcha box / form, as you'll see I've included my response action, the show_error function outputs if the recaptcha box isn't ticked - this works, but if the box is ticked and the form filled correctly it will not redirect me to the thank-you.html
<?php
$email_to = "billy.farroll#hotmail.com";
if($_POST){
$name;
$telephone;
$email;
$comment;
$captcha;
if(isset($_POST['name']))
$name=$_POST["name"];
if(isset($_POST['telephone']))
$telephone=$_POST["telephone"];
if(isset($_POST['email']))
$email=$_POST["email"];
if(isset($_POST['comment']))
$comment=$_POST["comment"];
if(isset($_POST['g-recaptcha-response']))
$captcha=$_POST['g-recaptcha-response'];
if (empty($name))
{
show_error("Oops! you forgot to fill in your name, Please go back to the homepage and try again");
}
if (empty($telephone))
{
show_error("Oops! you forgot to fill in your telephone, Please go back to the homepage and try again");
}
if (empty($email))
{
show_error("Oops! you forgot to fill in your email, Please go back to the homepage and try again");
}
$email = htmlspecialchars($_POST['email']);
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email) || (!$captcha))
{
show_error("Please tick the box");
}
} else {
$response=json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=6Lf2sEIUAAAAAMio98GU_V6gki_mGxS3Z6WMBmA9&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']), true);
if($response['success'] == false)
{
show_error("Please tick the box");
}
else
{
$email_from = $_POST["email"];
$message = $_POST["message"];
$email_subject = "Enquiry Form";
$headers =
"From: $email_from .\n";
"Reply-To: $email_from .\n";
$message =
"Name: ". $name .
"\r\nTelephone Number: " . $telephone .
"\r\nEmail Address: " . $email .
"\r\n\r\n\r\nComments :" . $comment;
ini_set("sendmail_from", $email_from);
$sent = mail($email_to, $email_subject, $message, $headers, "-f" .$email_from);
if ($sent)
{
header ('location: thank-you.html');
}
}
}
?>
This is my HTML code for the form, you'll see I've inserted my recaptcha box code below as well as linked to my enquiry.php file
<form id="enquiry-form" action="enquiry.php" style="text-align:left; font-size:10px; margin-top: 20px;" method="post">
<fieldset style="border:#FFF; font-size:12px; background-color:#0d5ea9;">
<table width="100%" border="0" cellpadding="0" style="margin-top:15px;">
<tr>
<td class="tablebutton">Name:</td>
<td><input type="text" name="name" id="name" size="50" /></td>
<td class="tablebutton">Telephone:</td>
<td><input type="text" name="telephone" id="telephone" size="37" /></td>
</tr>
<tr>
<td class="tablebutton">Email:</td>
<td> <input type="text" name="email" id="email" size="50" /></td>
</tr>
</table>
<div class="hide-for-small-only" style="margin-bottom:10px; font-size: 18px; font-family: 'Saira Semi Condensed', sans-serif; margin-left:10px; color:#FFFFFF; ">Comments</div>
<div align="left" class="hide-for-small-only" style="margin-left:10px;"><textarea name="comment" id="comment" cols="30" rows="10" style="width:50%; float:left;"></textarea>
<!--I'm not a robot box -->
<div class="g-recaptcha" data-sitekey="6Lf2sEIUAAAAAPyFiim58oXAdkgOS-m5dPJd2f1g"></div>
<!--I'm not a robot box -->
<div class="contactMirror"><strong>Contact Information:</strong><br />
<br>
If you have any other queries regarding the site, it's functionality or any technical questions about our vinyl service, please do not hesitate to contact us as we are always happy to help.
<br>
<br>
Please fill in the form and we will get straight back to you.
</div>
</div>
<br />
<br /> <br />
<div class="submitbutton" align="center" style="float:left; margin-top: 75px;"><button type="submit"><strong>Send</strong></button></div>
</fieldset>
</form>
I found the answer to this question, please see the amended code for PHP:
$captcha = $_POST["g-recaptcha-response"]; // Declare variable
if(isset($_POST['g-recaptcha-response'])){
$captcha=$_POST['g-recaptcha-response'];
}
if(!$captcha){
header("Location: <!-- Error HTML page -->");
exit;
}
$secretKey = "<!-- SECRET KEY HERE -->";
$ip = $_SERVER['REMOTE_ADDR'];
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$ip);
$responseKeys = json_decode($response, true);
if(intval($responseKeys["success"]) !== 1) {
echo '<h2>Spam Detected</h2>';
}
else {
header("Location: <!-- Thank you HTML page -->");
}
//NEW RECAPTCHA

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>

how to decrypt a password and send mail in php

i had Encrypted my password in register.php
<?php
// Set error message as blank upon arrival to page
$errorMsg = "";
// First we check to see if the form has been submitted
if (isset($_POST['username'])){
//Connect to the database through our include
include_once "connect_to_mysql.php";
// Filter the posted variables
$username = ereg_replace("[^A-Za-z0-9]", "", $_POST['username']); // filter everything but numbers and letters
$country = ereg_replace("[^A-Z a-z0-9]", "", $_POST['country']); // filter everything but spaces, numbers, and letters
$state = ereg_replace("[^A-Z a-z0-9]", "", $_POST['state']); // filter everything but spaces, numbers, and letters
$city = ereg_replace("[^A-Z a-z0-9]", "", $_POST['city']); // filter everything but spaces, numbers, and letters
$accounttype = ereg_replace("[^a-z]", "", $_POST['accounttype']); // filter everything but lowercase letters
$email = stripslashes($_POST['email']);
$email = strip_tags($email);
$email = mysql_real_escape_string($email);
$password = ereg_replace("[^A-Za-z0-9]", "", $_POST['password']); // filter everything but numbers and letters
// Check to see if the user filled all fields with
// the "Required"(*) symbol next to them in the join form
// and print out to them what they have forgotten to put in
if((!$username) || (!$country) || (!$state) || (!$city) || (!$accounttype) || (!$email) || (!$password)){
$errorMsg = "You did not submit the following required information!<br /><br />";
if(!$username){
$errorMsg .= "--- User Name";
} else if(!$country){
$errorMsg .= "--- Country";
} else if(!$state){
$errorMsg .= "--- State";
} else if(!$city){
$errorMsg .= "--- City";
} else if(!$accounttype){
$errorMsg .= "--- Account Type";
} else if(!$email){
$errorMsg .= "--- Email Address";
} else if(!$password){
$errorMsg .= "--- Password";
}
} else {
// Database duplicate Fields Check
$sql_username_check = mysql_query("SELECT id FROM members WHERE username='$username' LIMIT 1");
$sql_email_check = mysql_query("SELECT id FROM members WHERE email='$email' LIMIT 1");
$username_check = mysql_num_rows($sql_username_check);
$email_check = mysql_num_rows($sql_email_check);
if ($username_check > 0){
$errorMsg = "<u>ERROR:</u><br />Your User Name is already in use inside our system. Please try another.";
} else if ($email_check > 0){
$errorMsg = "<u>ERROR:</u><br />Your Email address is already in use inside our system. Please try another.";
} else {
// Add MD5 Hash to the password variable
$hashedPass = md5($password);
// Add user info into the database table, claim your fields then values
$sql = mysql_query("INSERT INTO members (username, country, state, city, accounttype, email, password, signupdate)
VALUES('$username','$country','$state','$city','$accounttype','$email','$hashedPass', now())") or die (mysql_error());
// Get the inserted ID here to use in the activation email
$id = mysql_insert_id();
// Create directory(folder) to hold each user files(pics, MP3s, etc.)
mkdir("memberFiles/$id", 0755);
// Start assembly of Email Member the activation link
$to = "$email";
// Change this to your site admin email
$from = "geetha.victor#tryteksolutions.co.in";
$subject = "Complete your registration";
//Begin HTML Email Message where you need to change the activation URL inside
$message = '<html>
<body bgcolor="#FFFFFF">
Hi ' . $username . ',
<br /><br />
You must complete this step to activate your account with us.
<br /><br />
Please click here to activate now >>
<a href="http://www.trytek.tryteksolutions.co.in/activation.php?id=' . $id . '">
ACTIVATE NOW</a>
<br /><br />
Your Login Data is as follows:
<br /><br />
E-mail Address: ' . $email . ' <br />
Password: ' . $password . '
<br /><br />
Thanks!
</body>
</html>';
// end of message
$headers = "From: $from\r\n";
$headers .= "Content-type: text/html\r\n";
$to = "$to";
// Finally send the activation email to the member
mail($to, $subject, $message, $headers);
// Then print a message to the browser for the joiner
print "<br /><br /><br /><h4>OK $firstname, one last step to verify your email identity:</h4><br />
We just sent an Activation link to: $email<br /><br />
<strong><font color=\"#990000\">Please check your email inbox in a moment</font></strong> to click on the Activation <br />
Link inside the message. After email activation you can log in.";
exit(); // Exit so the form and page does not display, just this success message
} // Close else after database duplicate field value checks
} // Close else after missing vars check
} //Close if $_POST
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Member Registration</title>
</head>
<body>
<table width="600" align="center" cellpadding="4">
<tr>
<td width="7%">REGISTER AS A MEMBER HERE </td>
</tr>
</table>
<table width="600" align="center" cellpadding="5">
<form action="join_form.php" method="post" enctype="multipart/form-data">
<tr>
<td colspan="2"><font color="#FF0000"><?php echo "$errorMsg"; ?></font></td>
</tr>
<tr>
<td width="163"><div align="right">User Name:</div></td>
<td width="409"><input name="username" type="text" value="<?php echo "$username"; ?>" /></td>
</tr>
<tr>
<td><div align="right">Country:</div></td>
<td><select name="country">
<option value="<?php echo "$country"; ?>"><?php echo "$country"; ?></option>
<option value="Australia">Australia</option>
<option value="Canada">Canada</option>
<option value="Mexico">Mexico</option>
<option value="United Kingdom">United Kingdom</option>
<option value="United States">United States</option>
<option value="Zimbabwe">Zimbabwe</option>
</select></td>
</tr>
<tr>
<td><div align="right">State: </div></td>
<td><input name="state" type="text" value="<?php echo "$state"; ?>" /></td>
</tr>
<tr>
<td><div align="right">City: </div></td>
<td>
<input name="city" type="text" value="<?php echo "$city"; ?>" />
</td>
</tr>
<tr>
<td><div align="right">Account Type: </div></td>
<td><select name="accounttype">
<option value="<?php echo "$accounttype"; ?>"><?php echo "$accounttype"; ?></option>
<option value="a">Normal User</option>
<option value="b">Expert User</option>
<option value="c">Super User</option>
</select></td>
</tr>
<tr>
<td><div align="right">Email: </div></td>
<td><input name="email" type="text" value="<?php echo "$email"; ?>" /></td>
</tr>
<tr>
<td><div align="right"> Password: </div></td>
<td><input name="password" type="password" value="<?php echo "$password"; ?>" />
<font size="-2" color="#006600">(letters or numbers only, no spaces no symbols)</font></td>
</tr>
<tr>
<td><div align="right"> Captcha: </div></td>
<td>Add Captcha Here for security</td>
</tr>
<tr>
<td><div align="right"></div></td>
<td><input type="submit" name="Submit" value="Submit Form" /></td>
</tr>
</form>
</table>
</body>
</html>
This is my forgot password script in which i have a problem in sending encrypt password in mail. how to decrypt a password and send decrypted password in mail.
<?php session_start();
include "connect_to_mysql.php"; //connects to the database
if (isset($_POST['email'])){
$email = $_POST['email'];
$query="select * from members where email='$email'";
$result = mysql_query($query);
$count=mysql_num_rows($result);
// If the count is equal to one, we will send message other wise display an error message.
if($count==1)
{
$rows=mysql_fetch_array($result);
$password = $rows['password'];//FETCHING PASS
//echo "your pass is ::".($pass)."";
$to = $rows['email'];
//echo "your email is ::".$email;
//Details for sending E-mail
$from = "geetha.victor#tryteksolutions.co.in";
$url = "http://abc.co.in/";
$body = "TrytekSolutions password recovery <br />
---------------------------------------------------------- <br />
Url : $url;<br />
email Details is : $to;<br />
Here is your password : $password;<br /> <br />
Sincerely, <br />
TryTekSolutions";
$from = "abc#tryteksolutions.co.in";
$subject = "Tryteksolutions Password recovered";
$headers1 = "From: $from\n";
$headers1 .= "Content-type: text/html;charset=iso-8859-1\r\n";
$headers1 .= "X-Priority: 1\r\n";
$headers1 .= "X-MSMail-Priority: High\r\n";
$headers1 .= "X-Mailer: Just My Server\r\n";
$sentmail = mail ( $to, $subject, $body, $headers1 );
} else {
if ($_POST ['email'] != "") {
echo "<span style='color: #ff0000;'> Not found your email in our database</span>";
}
}
//If the message is sent successfully, display sucess message otherwise display an error message.
if($sentmail==1)
{
echo "<span style='color: #ff0000;'> Your Password Has Been Sent To Your Email Address.</span>";
}
else
{
if($_POST['email']!="")
echo "<span style='color: #ff0000;'> Cannot send password to your e-mail address.Problem with sending mail...</span>";
}
}
?>
help me friends how to decrypt a password and send mail.
Don't. You should never be able to convert the stored password data into an actual password. They should be hashed, not encrypted.
MD5 is a hashing algorithm, but a very weak one that is entirely unsuitable for protecting passwords with today. You need to take better care of your users' passwords.
If someone loses their password, then generate a time-limited random reset token and email it to the user.
When they enter that token (usually by following a link in the email with the token embedded in it) allow them to choose a new password.

Php email send issue

Following is my HTML Select field which gets all the emails from my database. Now I'm trying to send single or multiple emails with php. But It doesn't send any emails. Can you tell me what is wrong with my code ?
Html Code:
<tr>
<td valign="top">To</td>
<td>
<select multiple="multiple" size="7" name="to[]">
<?php
$getemail = mysql_query("SELECT email FROM clients");
while($res = mysql_fetch_array($getemail)){
$email = inputvalid($res['email']);
echo "<option value='$email'>$email</option>";
}
?>s
</select>
</td>
</tr>
Php Code:
foreach($to as $total){
$total;
$headers = "From: $from\r\n";
$headers .= "Content-type: text/html\r\n";
$mail = mail($total, $subject, $msg, $headers);
}
Update-Full Code:
<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" />
<table width="400" border="0" cellspacing="10" cellpadding="0" style="float:left;
position:relative;">
<tr>
<td>Subject</td>
<td><input type="text" name="subject" value="<?php if(isset($_POST['subject'])) echo
$_POST['subject']; ?>" class="tr"/></td>
</tr>
<tr>
<td valign="top">Message</td>
<td><textarea name="msg" class="textarea_email"><?php if(isset($_POST['msg'])) echo
$_POST['msg']; ?></textarea></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Send Message" class=" submit" name="Submit"/></td>
</tr>
</table>
<table style="float:left; position:relative; border:0px #000 solid;" width="400" border="0"
cellspacing="10" cellpadding="0">
<tr>
<td valign="top">To</td>
<td>
<select multiple="multiple" size="7" name="to[]">
<?php
$getemail = mysql_query("SELECT email FROM clients") or die(mysql_error());;
while($res = mysql_fetch_array($getemail)){
$email = inputvalid($res['email']);
echo "<option value='$email'>$email</option>";
}
?>s
</select>
</td>
</tr>
</table>
</form>
Php code:
if(isset($_POST['Submit']) && $_POST['Submit'] == "Send Message")
{
$subject = inputvalid($_POST['subject']);
$msg = inputvalid($_POST['msg']);
$to = $_POST['to'];
if(isset($subject) && isset($msg) && isset($to)){
if(empty($subject) && empty($msg) && empty($to))
$err[] = "All filed require";
}
else{
if(empty($to))
$err[] = "Please select email address";
if(empty($subject))
$err[] = "Subject require";
if(empty($msg))
$err[] = "Message require";
}
if(!empty($err))
{
echo "<div class='error'>";
foreach($err as $er)
{
echo "<font color=red>$er.</font>
<br/>";
}
echo "</div>";
echo "<br/>";
}
else{
foreach($to as $total){
echo $total;
$headers = "From: $from\r\n";
$headers .= "Content-type: text/html\r\n";
$mail = mail($total, $subject, $msg,
$headers);
}
if($mail)
echo "<font color=green>Successfully sent your message. We will be get in
touch with you. Thank You.</font/><br/><br/>";
header("Refresh:10; url=email.php");
}
}
You can send multiple emails for one mail function.
Only pass email address with comma separated.
like "test#gmail.com,test1#gmail.com"
first make sure that you query is correct.
Change the following line:
$getemail = mysql_query("SELECT email FROM clients");
to
$getemail = mysql_query("SELECT email FROM clients") or die(mysql_error());
Change the following line:
$total;
to
echo $total . '<BR />';
Save and refresh the page
What is your output?
Do you get an errormessage?
Do you see emailadresses?
edit:
change the following line
$mail = mail($total, $subject, $msg, $headers);
to
if (!mail($total, $subject, $msg, $headers);) {
echo 'This is not working';
}
What is your result?
Check your apache-configs! I had the same problem on my local machine. as soon as i uploaded it on a fully configured Webserver, the code started to work! Maybe this solves your problem, too
Some time mail() function may not work so you should go with PHPMailer, for more details to use this, you can go through a good documentation :
Send mail using PHPMailer

Adding checkboxes to PHP POST email form

I'm trying to build a form for wordpress. I've used plugins in the past but I need maximum control for some specific styling. I'm not very good with PHP yet, so am struggling trying to add checkboxes to the script.. I've removed my attempts and left the checkboxes in the html, but not in the PHP - can someone advise me of the best way to make any selected checkboxes visible in the email that is sent? Everything else works at the moment.
the html:
<form method="post" action="<?php bloginfo('template_directory'); ?>/contact.php">
<h3>Your Details</h3>
<div class="formrow">
<input type="text" name="Name" maxlength="99" id="fullname" placeholder="Name" />
<input type="email" name="Email" maxlength="99"placeholder="Email Address" />
<input type="tel" name="Phone" maxlength="25" placeholder="Phone Number" />
</div>
<h3>Project Type</h3>
<div class="formrow">
<fieldset>
<input type="checkbox" name="project1" value="Web">
<label for="type1">Web</label>
<input type="checkbox" name="project2" value="Digital">
<label for="type2">Digital</label>
<input type="checkbox" name="project3" value="Consultancy">
<label for="type3">Consultancy</label>
</fieldset>
</div>
<table align=center>
<tr><td colspan=2><strong>Contact us using this form:</strong></td></tr>
<tr><td>Department:</td><td><select name="sendto"> <option value="general#mycompany.com">General</option> <option value="support#mycompany.com">Support</option> <option value="sales#mycompany.com">Sales</option> </select></td></tr>
<tr><td>Company:</td><td><input size=25 name="Company"></td></tr>
<tr><td>Subscribe to<br> mailing list:</td><td><input type="radio" name="list" value="No"> No Thanks<br> <input type="radio" name="list" value="Yes" checked> Yes, keep me informed<br></td></tr>
<tr><td colspan=2>Message:</td></tr>
<tr><td colspan=2 align=center><textarea name="Message" rows=5 cols=35></textarea></td></tr>
<tr><td colspan=2 align=center><input type=submit name="send" value="Submit"></td></tr>
<tr><td colspan=2 align=center><small>A <font color=red>*</font> indicates a field is required</small></td></tr>
</table>
</form>
the PHP:
<?php
$to = $_REQUEST['sendto'] ;
$from = $_REQUEST['Email'] ;
$name = $_REQUEST['Name'] ;
$headers = "From: $from";
$subject = "Web Contact Data";
$fields = array();
$fields{"Name"} = "Name";
$fields{"Company"} = "Company";
$fields{"Email"} = "Email";
$fields{"Phone"} = "Phone";
$fields{"list"} = "Mailing List";
$fields{"Message"} = "Message";
$body = "We have received the following information:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }
$headers2 = "From: noreply#YourCompany.com";
$subject2 = "Thank you for contacting us";
$autoreply = "Thank you for contacting us. Somebody will get back to you as soon as possible, usualy within 48 hours. If you have any more questions, please consult our website at www.oursite.com";
if($from == '') {print "You have not entered an email, please go back and try again";}
else {
if($name == '') {print "You have not entered a name, please go back and try again";}
else {
$send = mail($to, $subject, $body, $headers);
$send2 = mail($from, $subject2, $autoreply, $headers2);
if($send)
{print "Success";}
else
{print "We encountered an error sending your mail, please notify webmaster#YourCompany.com"; }
}
}
?>
Thanks! MC
An array of checkboxes would be most appropriate. By using [] in the checkbox names, PHP will automatically parse them into a native array.
<input type="checkbox" name="projects[]" value="Web">
<label for="type1">Web</label>
<input type="checkbox" name="projects[]" value="Digital">
<label for="type2">Digital</label>
<input type="checkbox" name="projects[]" value="Consultancy">
<label for="type3">Consultancy</label>
On the PHP side:
$selectedProjects = 'None';
if(isset($_POST['projects']) && is_array($_POST['projects']) && count($_POST['projects']) > 0){
$selectedProjects = implode(', ', $_POST['projects']);
}
$body .= 'Selected Projects: ' . $selectedProjects;
Outputs (if all checked)
Selected Projects: Web, Digital, Consultancy

Categories