This is the first project in which I am facing a problem with the mail function. I want to send the option tag value in a mail. At that time I am not getting the of option tag but I am taking name of the select tag in the array because I want to store multiple values in database, that's why I am using an array. Can anyone tell me the solution to this problem?
This is my code:
<select multiple id="00N7F000001F2kO" name="soft_skill[]">
<option value=""selected disabled>soft Skills</option>
<option name="Personality Development" value="Personality Development">personality development</option>
<option name="Communication Skills" value="Communication Skills">communication skills</option>
</select>
And my code with mail function is:
if(isset($_POST['submit']))
{
$name = $_POST['name'];
$soft_skill = $_POST['soft_skill'];
$question = $_POST['question'];
$about_us = $_POST['about_us'];
$to = "kuljeet#mightymente.com";
$subject = "Web Enquiry";
$message = "................";
$header = "From:$email"."\r\n"."CC: kuljeetdhiman06#gmail.com";
if(mail($to, $subject, $message, $header))
{
echo "";
} else {
echo "";
}
}
You can not pass the array directly in mail,
either You should use the implode method and specify any delimiter you wish to use. If you will select multiple option then by using implode you will get result like Personality Development , Communication Skills
if(isset($_POST['submit']))
{
$name=$_POST['name'];
$soft_skill= implode("," , $_POST['soft_skill']);
$question=$_POST['question'];
$about_us=$_POST['about_us'];
$to="kuljeet#mightymente.com";
$subject="Web Enquiry";
$message="................";
$header="From:$email"."\r\n"."CC: kuljeetdhiman06#gmail.com";
if(mail($to, $subject, $message, $header)) {
echo "";
} else{
echo "";
}
}
And i saw you message variable is like
$message="................";
Here you need to concate all the values using .= like,
$message = "Name : ".$name."\n\n";
$message.= "Soft Skils : ".$soft_skill."\n\n";
$message.= "Question : ".$question."\n\n";
$message.= "About Us : ".$about_us."\n\n";
Hope this will be helpful to you.
You can use
$temp_softSkill=implode(",",$_POST['soft_skill']);
Because it is an array so you have to use indexes of soft skill array like $_POST['soft_skill'][0] which one u want
Related
I read this post: What is a good invisible captcha? about using a hidden field in a web form to stop basic bots from pelting your website with spam mail via your web sites form mail. I'm currently using a php script to process my form mail. I built the script by following a 'bullet proff web form' tutorial I found. It looks like this:
<?php
// Pick up the form data and assign it to variables
$name = $_POST['name'];
$email = $_POST['email'];
$topic = $_POST['topic'];
$comments = $_POST['comments'];
// Build the email (replace the address in the $to section with your own)
$to = 'hello#cipherbunny.com';
$subject = "New message: $topic";
$message = "$name said: $comments";
$headers = "From: $email";
// Data cleaning function
function clean_data($string) {
if (get_magic_quotes_gpc()) {
$string = stripslashes($string);
}
$string = strip_tags($string);
return mysql_real_escape_string($string);
}
// Mail header removal
function remove_headers($string) {
$headers = array(
"/to\:/i",
"/from\:/i",
"/bcc\:/i",
"/cc\:/i",
"/Content\-Transfer\-Encoding\:/i",
"/Content\-Type\:/i",
"/Mime\-Version\:/i"
);
$string = preg_replace($headers, '', $string);
return strip_tags($string);
}
// Pick up the cleaned form data
$name = remove_headers($_POST['name']);
$email = remove_headers($_POST['email']);
$topic = remove_headers($_POST['topic']);
$comments = remove_headers($_POST['comments']);
// Send the mail using PHPs mail() function
mail($to, $subject, $message, $headers);
// Redirect
header("Location: http://foobar/success.html");
I'd like to modify this script so that if a hidden field with the identifier 'other_email' was filled in then the form email wouldn't get sent. I'm guess it's as straight forward as wrapping the above code in an if statement to check if the field is complete. I've tried adding this under the "//Pick up the form data and assign it to variables" code:
$testBot = $_POST['other_email'];
then writing:
if(other_email == "") //If other_email form section is blank then...
{
run all the code above inserted here;
}
else
{
Don't know what I should put here to stop it posting, yet still show the success form so
the spam bot don't know
}
any help much appreciated. I have to say I don't really have a lot of php knowledge, I'm just starting to learn about it and thought form mail would be a good start.
How do I make this work in PhP?
if(other_email == "") //If other_email form section is blank then...
{
run all the code above inserted here;
}
else
{
header("Location: http://foobar/success.html");
}
keeping it very simple, it will work for you..
actually, it will
not submit / mail you anything...so NO SPAM
a simple bot will take it as it did it...
if you can use php on success page, then set a session variable (to make bot think it did its job, something like email_sent=true or success=true) and use that variable in success page, you will do it in else case where bot submitted the form..
Do you mean send message with fields?
Try this:
<?php
// Pick up the form data and assign it to variables
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$topic = $_REQUEST['topic'];
$comments = $_REQUEST['comments'];
// Build the email (replace the address in the $to section with your own)
if($name !== null && $email !== null && $topic !== null && $comments !== null){
$to = 'hello#cipherbunny.com';
$subject = "New message: $topic";
$message = "$name said: $comments";
$headers = "From: $email";
// Data cleaning function
function clean_data($string) {
if (get_magic_quotes_gpc()) {
$string = stripslashes($string);
}
$string = strip_tags($string);
return mysql_real_escape_string($string);
}
// Mail header removal
function remove_headers($string) {
$headers = array(
"/to\:/i",
"/from\:/i",
"/bcc\:/i",
"/cc\:/i",
"/Content\-Transfer\-Encoding\:/i",
"/Content\-Type\:/i",
"/Mime\-Version\:/i"
);
$string = preg_replace($headers, '', $string);
return strip_tags($string);
}
// Pick up the cleaned form data
$name = remove_headers($_POST['name']);
$email = remove_headers($_POST['email']);
$topic = remove_headers($_POST['topic']);
$comments = remove_headers($_POST['comments']);
// Send the mail using PHPs mail() function
mail($to, $subject, $message, $headers);
// Redirect
header("Location: http://foobar/success.html");
}
?>
<!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=shift_jis" />
<title>Send</title>
</head>
<body>
<form action="#" method="POST">
Name : <input type="text" name="name" /><br />
Email : <input type="text" name="email" /><br />
Topic : <input type="text" name="topic" /><br />
Comments : <textarea name="comments"></textarea><br />
<input type="submit" value="Send" />
</form>
</body>
</html>
Hi I currently have some PHP code which will put previously selected timeslots from checkboxes on a previous page into my database, but I'm not sure how to store each one in a variable before I do this, so that I can use them to list the appointment slot's in my e-mail function
At the moment when I try to insert '$key' to my e-mail function it only retains the last selected value...
My code on the first page reads:
<?php
for ($i=0;$i<=31;$i++){ while ($myrow = mysql_fetch_row($result)) {
printf("<tr><td>%s</td><td>%s</td></tr>\n",
$myrow[0]);
}
echo "<td><input type='checkbox' name='$displayTime2' onclick='KeepCount()'></td>";
echo "<td>$pagetime</td>"; ?>
My code at the moment is as follows:
<?php
foreach ($_POST as $key=>$value)
{
echo $key; // show times selected on previous page
mysql_query("INSERT INTO appointment(Patient_ID, Appointment_Date, Appointment_Time
, Practice_ID, Appointment_ID)
VALUES('$patid','$insertdate','$key','$pracid','$apptype')");
}
?>
my mail function is as follows:
$to = "$pemail";
$subject = "Talking Teeth Check Up Booking Confrmation";
$message = "Hello! This e-mail is to confirm your ".$appname." appointment has been
booked for ".$insertdate. " at ".$key." at the ".$pracname." practice";
$from = "talkingteethdentists#talkingteeth.co.uk";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
Although the comments above are right, let me help you out.
First off, you need to figure out which checkboxes were selected, so do some IF statements to figure that out on each of these inside of your foreach loop.
Try something like this:
<?php
$savedData = array();
foreach ($_POST as $key=>$value){
$key = mysql_real_escape_string($key);
echo($key. "<br>"); // show times selected on previous page
mysql_query("INSERT INTO appointment(Patient_ID, Appointment_Date, Appointment_Time
, Practice_ID, Appointment_ID)
VALUES('$patid','$insertdate','$key','$pracid','$apptype')");
//To save the variables for later:
$savedData[] = $key;
}
?>
And now you can access all of our KEYs with $savedData[0] .... $savedData[1] ... $savedData[2] .... etc....
So your email function might look like:
foreach($savedData as $time){
$to = "$pemail";
$subject = "Talking Teeth Check Up Booking Confirmation"; //mis-spelled confirmation =)
$message = "Hello! This e-mail is to confirm your ".$appname." appointment has been
booked for ".$insertdate. " at ".$time." at the ".$pracname." practice";
$from = "talkingteethdentists#talkingteeth.co.uk";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
}
So this will send out an email for each piece inside $savedData - if that truly is the intent.
I have a mysql database table called "leads" with an id column, a name column, and an email column. I currently have users selecting a lead from a multiple select box, which is returned as an array from an html form with $_POST method. I have the value for the select box items as the email column, and what is shown to the user on the front end is the name column. However, I want to be able to return two values, the email and the name, for later use. I need to have the email value(s) able to be imploded with commas between them, so I can later send them emails using PHP's mail function. However, when a user looks at sent emails, I want to show them the name value(s). I have read that I can either parse the array, or use the id column set as the value, which seems more practical, however, I am not sure how I would go about this. Any ideas? Thanks in advance for any help!
*UPDATE*
Basically, the users that log into the site have their own leads in a mysql database. I need them to be able to go to a 'send email' page, where they can select a lead, or multiple leads, by name. Then there is a subject field, and a message field so they can write the email, and then I have a 'send email' form submit button. After they click 'send email' I want to get back the email addresses and names of the leads they selected, for later use. I need to have the email addresses separated by commas, so that I can send them through a php mail function... But the important thing is, I want the info from the form to be stored in a database table called 'PendingEmails', and I am going to run a cron job to run a script that finds what emails need to be sent, and sends them... After an email is sent, I need to put the names, subject, and message from the email in a separate database, called 'SentEmails'. On a different page on the site, I want the users to be able to see all the emails they sent, but I want them to see the names of who they sent it to, rather than the email addresses. This is why I need both the names and the emails. I need emails for the mail function, and names to be displayed back to the user. I like your idea of using the id and running the rest server side, I just dont know how I would do this. Hope this helped.
Here is the select box:
<select data-placeholder="Select Lead(s) To Email..." multiple="true" class="chzn-container-multi" name="selectleads[]"style="width:505px;">
<?php
do {
?>
<option value="<?php echo $row_rsAllLeads['Email']?>"><?php echo $row_rsAllLeads['FullName']?></option>
<?php
} while ($row_rsAllLeads = mysql_fetch_assoc($rsAllLeads));
$rows = mysql_num_rows($rsAllLeads);
if($rows > 0) {
mysql_data_seek($rsAllLeads, 0);
$row_rsAllLeads = mysql_fetch_assoc($rsAllLeads);
}
?>
</select>
And here is the actual form action:
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form")) {
$startcode = $_POST['messagefield'];
$replaced = preg_replace( '/\\\\(?="|\')/', '', $startcode );
$selected = $_POST['selectleads'];
$collectedleads = implode(", ", $_POST['selectleads']);
$to = $collectedleads;
$subject = $_POST['subjectfield'];
$body = $replaced;
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: " . $row_rs_CurrentUser['FirstName'] . " " . $row_rs_CurrentUser['LastName'] . " <" . $row_rs_CurrentUser['Email'] . ">";
if (mail($to, $subject, $body, $headers)) {
} else {
echo("<p>Message delivery failed...</p>");
}
}
The code you gave me and I tried (and failed) to edit and make work:
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form")) {
$startcode = $_POST['messagefield'];
$replaced = preg_replace( '/\\\\(?="|\')/', '', $startcode );
mysql_select_db($database_myBackOfficeConn, $myBackOfficeConn);
$collectedleads = implode(", ", $_POST['selectleads']);
//arrays to store email addresses and names
$emails = array();
$names = array();
foreach ($collectedleads as $id) {
mysql_query("SELECT Email, FullName FROM Leads
WHERE Id = " . mysql_real_escape_string($id));
while ($row = mysql_fetch_assoc()) {
$emails[] = $row['Email'];
$names[] = $row['FullName'];
}
}
$to = $collectedleads;
$subject = $_POST['subjectfield'];
$body = $replaced;
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: " . $row_rs_CurrentUser['FirstName'] . " " . $row_rs_CurrentUser['LastName'] . " <" . $row_rs_CurrentUser['Email'] . ">";
if (mail($to, $subject, $body, $headers)) {
} else {
echo("<p>Message delivery failed...</p>");
}
}
?>
I think it would make more sense to do the retrieving of data on the server-side. Instead of the email addresses, set the <option> values to your database's id column values.
EDIT: Changed most of the answer after discussing with OP
Bearing in mind that the code you posted was just a snippet, and so I have no way of testing this, you could try something along these lines:
<select data-placeholder="Select Lead(s) To Email..." multiple="true"
class="chzn-container-multi" name="selectleads[]"style="width:505px;">
<?php
/* I think while is better, in case there are no results.
do...while would attempt to echo the first result in a set even
if there were no results, which would give an error, methinks. */
//Use the id field of your database when you populate the select
while ($row_rsAllLeads = mysql_fetch_assoc($rsAllLeads)) { ?>
<option value="<?php echo $row_rsAllLeads['id']?>">
<?php echo $row_rsAllLeads['FullName']?></option>
<?php
}
/* What is this block used for? */
$rows = mysql_num_rows($rsAllLeads);
if($rows > 0) {
mysql_data_seek($rsAllLeads, 0);
$row_rsAllLeads = mysql_fetch_assoc($rsAllLeads);
}
?>
</select>
I'm uncertain as to the purpose of the code block at the end so I just left it. I assume you do something else with that data...?
So now your <select> is exactly the same as before, but instead of actual email addresses it will hand back the corresponding database ids instead.
Now, on submitting the data, we change our tack slightly:
<?php
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form")) {
$startcode = $_POST['messagefield'];
$replaced = preg_replace( '/\\\\(?="|\')/', '', $startcode );
$selected = $_POST['selectleads'];
/* NEW CODE */
/*selectleads[] is now an array of your database's ids.
You can now run a query to get whatever information you
need. In this case, you want the email addresses, so:
*/
$collectedleads = $_POST['selectleads'];
$emailaddylist = array();
foreach ($collectedleads as $id) {
$x = mysql_query("SELECT `Email` FROM `leads` WHERE `id` = " .
mysql_real_escape_string($id));
$x_assoc = $x->fetch_assoc();
$emailaddylist[] = $x['Email'];
}
/*Now you have an array of email addresses that correspond to
the ids you were sent via $_POST.
*/
$emailaddystring = implode(", ", $emailaddylist);
$to = $emailaddystring;
/*If you want to use any other data from the database,
you still have an array of ids in $collectedleads.
Just run another query to get the data you want.*/
/* END OF NEW CODE */
$subject = $_POST['subjectfield'];
$body = $replaced;
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: " . $row_rs_CurrentUser['FirstName'] . " " . $row_rs_CurrentUser['LastName'] . " <" . $row_rs_CurrentUser['Email'] . ">";
if (mail($to, $subject, $body, $headers)) {
} else {
echo("<p>Message delivery failed...</p>");
}
}
?>
Because we are handed the ids in an array, we can use them to get whatever information we want from the database. In this case we're just getting the email addresses. If you want to retrieve any other information, you can use the same technique - the list of ids still exists in $collectedleads, so you can run another query to get whatever data you need.
After an email is sent, I need to put the names, subject, and message
from the email in a separate database, called 'SentEmails'
In that case, use the id data in $collectedleads to run a SELECT query on leads to get the information you need (name, email address, whatever), and run an INSERT query on the SentEmails table to copy that information.
Hope this helps.
CAVEAT:
The use of mysql is discouraged in favour of mysqli. To quote the page:
If you are using MySQL versions 4.1.3 or later it is strongly
recommended that you use the mysqli extension instead.
It works almost exactly the same; in most cases all you need to do is to put the i in front of it. Some functions don't work with it, or have better alternatives. You should check it out.
What I would do, I would add a hidden field named something along the lines of "lead_name" and fill it with jQuery or just plain JS on an onblur event with the value of the selected lead.
$("#lead_name").val($("#lead option:selected").text());
I am trying to take a two text areas with multiple emails, one with the "from" emails, and one with the "to" emails. Combine the emails line by line and send emails out accordingly.
Ex:
"To" list:
mike#gmail.com
nick#hotmail.com
adam#yahoo.com
"From" list:
ashley#gmail.com
brittney#yahoo.com
racheal#hotmail.com
I want a email sent to:
mike#gmail.com from ashley#gmail.com
nick#hotmail.com from brittney#yahoo.com
adam#yahoo.com from racheal#hotmail.com
Any help would be greatly appreciated. Thanks in advanced.
Below is the script I got so far, It sends to multiple emails from one email.
<?php
if (isset($_POST['submit']))
{
// Execute this code if the submit button is pressed.
$raw_email_account = $_POST['email_from'];
$email = $_POST['email_to'];
$sent = "";
$invalid = "";
//Separating each line to be read by foreach
$list = explode("\n",$email);
//Rendering the separeted data from each line
foreach($list AS $data) {
//Separating each line to be read by foreach
$item = explode(":",$data);
$mail_body = '<html><body>email here</body></html>';
$subject = "subject here";
$headers = "From:".$raw_email_account."\r\n";
$headers .= "Content-type: text/html\r\n";
$to = $item[0];
$mail_result = mail($to, $subject, $mail_body, $headers);
if ($mail_result) {
$valid++;
} else {
// write into an error log if the mail function fails
$invalid++;
}
}
}
?>
<html>
<head>
</head>
<body>
<form action="email_sender.php" method="POST">
<div align="center">From Email Accounts: <textarea name="email_from" cols="100" rows="60"></textarea></div><br />
<div align="center">To Email Accounts: <textarea name="email_to" cols="100" rows="60"> </textarea></div><br />
<div align="center"><input type="submit" name="submit"></div>
<br>
Valids: <?php echo $valid;?>
<br>
Invalids: <?php echo $invalid;?>
</body>
</html>
If the array is by default, the indexes will coincide. So you can do something like this.
$toList = array(..);
$fromList = array(...);
foreach($toList as $key => $value) {
$toAddress = $value;
$fromAddress = $fromList[$key];
//..
//.. Go on with you mail function
}
Add logic that provides same count of $email and $raw_email_account arrays before foreach loop.
$list = explode("\n",$email);
$list2 = explode("\n", $raw_email_account);
foreach($list AS $key=>$data) {
...
$headers = "From:".$list2[$key]."\r\n";
...
}
You can use array_combine() to solve that problem easily.
In that case, you first combine the two arrays, and then loop through the resulting array to perform the sending e-mail action.
Ok so long story short, I have a simple mailto function I want to apply/run for every name on a db list. Since it's not working, I removed all the mail stuff from it and to test to make sure the while loop was working with the db, did this
<?php
$connect2db = mysqli_connect('127.0.0.1','root','pass','dbnamehere');
if(!$connect2db){
die("Sorry but theres a connection to database error" . mysqli_error);
}
$sn_query = "SELECT * FROM email_list";
$sn_queryResult = mysqli_query($connect2db, $sn_query) or die("Sorry but theres a connection to database error" . mysqli_error);
$sn_rowSelect = mysqli_fetch_array($sn_queryResult);
$to = $sn_rowSelect;
?>
<br/><br/>
////lower part on page //////<br/><br/>
<?php
while($sn_rowSelect = mysqli_fetch_array($sn_queryResult) ) {
echo "hello there" . " " . $sn_rowSelect['firstname'] . " <br/>";
}
?>
Now this works, it goess through my db and prints out all my first names from the database list. In my noob brain, id think that if i remove the echo lines, and enter the appropriate mailto information, that it would loop just like before, but send mail to each name. so i did this:
<?php
$sn_query = "SELECT email FROM email_list";
$sn_queryResult = mysqli_query($connect2db, $sn_query) or die("Sorry but theres a connection to database error" . mysqli_error);
$sn_rowSelect = mysqli_fetch_array($sn_queryResult);
$to = implode(",",$sn_rowSelect);
$from = $_POST['sender'];
$subject = $_POST['subj'];
$mssg = $_POST['message'];
$headers = "MIME-Version: 1.0rn";
$headers .= "From: $from\r\n";
$mailstatus = mail($to, $subject, $mssg, $headers);
?>
<br/><br/>
//////////<br/><br/>
<?php
while($sn_rowSelect = mysqli_fetch_array($sn_queryResult) ) {
$mailstatus;
if($mailstatus) {
echo "Success";
}else{
echo "There was a problem sending the mail. Check your code and make sure that the e-mail address $to is valid\n";
}
}
?>
now this emails the first name on my list, but not the rest.
I don't get any errors so not sure what the problem is. I was going to try an if statement with num_rows but somewhere else, on StackOverflow, someone said that didn't help since the while loop took care of it by itself. (I tried it either way and it still emailed only the first name) I'm trying here but to no avail.
You have not called the mail() function inside your loop. You call it once outside. Instead try something like the following.
Assuming you have retrieved the $to address from your database query (like you did with the firstname in testing), pull it from the rowset, and use it in mail():
while($sn_rowSelect = mysqli_fetch_array($sn_queryResult) ) {
// Get the $to address:
$to = $sn_rowSelect['email'];
// Call mail() inside the loop.
$mailstatus = mail($to, $subject, $mssg, $headers);
if($mailstatus) {
echo "Success";
}else{
echo "There was a problem sending the mail. Check your code and make sure that the e-mail address $to is valid\n";
}
}
Note also, that since you call mysql_fetch_array() at the top of your script, your while loop will start with the second row. You should remove the first call to mysql_fetch_array() that occurs before the loop.
$sn_queryResult = mysqli_query($connect2db, $sn_query) or die("Sorry but theres a connection to database error" . mysqli_error);
// Don't want this...
//$sn_rowSelect = mysqli_fetch_array($sn_queryResult);