I want to include the output of a foreach loop + echo $total as the variable $order in my sendmail.php. Can somebody help me? I am a bit stuck.
My sendmail.php:
<?php
if(!isset($_SESSION)) {
session_start();
}
$to = $_SESSION['email'];
$firstname = $_SESSION['firstname'] ;
$lastname = $_SESSION['lastname'] ;
$email = $_SESSION['email'] ;
$addressline1 = $_SESSION['addressline1'] ;
$towncity = $_SESSION['towncity'] ;
$postcode = $_SESSION['postcode'] ;
foreach ($_SESSION['invoice'] as $value) { //needs to = $order
echo $value."<br>";} //needs to = $order
echo "Total: $".$_SESSION['total']; //needs to = $order
//set subject
$subject = "Crystal Fusion - New Order";
//body of the e-mail
$body = "New Order Received:\n\n\n\n
From: $firstname $lastname\n
Email: $email\n
Address: $addressline1\n
Town/City: $towncity\n
Postcode: $postcode\n
Order: $order"; //needs to = foreach loop above
$sent = mail($to, $subject, $body);
if($sent)
{echo "<script language=javascript>window.location = 'mail_succeed.php';</script>";}
else
{echo "<script language=javascript>window.location = 'mail_fail.php';</script>";}
?>
The solution would be to use something along the lines of
ob_start();
// Add your output
foreach ($_SESSION['invoice'] as $value) { //needs to = $order
echo $value."<br>";} //needs to = $order
echo "Total: $".$_SESSION['total']; //needs to = $order
// if you need your logic at multiple places, consider using a separate php file and including it here
// ...
$body=ob_get_contents();
ob_end_clean();
mail($to, $subject, $body);
How about changing your foreach loop to:
$order = '';
foreach ($_SESSION['invoice'] as $value) { //needs to = $order
echo $value."<br>"; //needs to = $order
$order .= $value."\n";
}
echo "Total: $".$_SESSION['total']; //needs to = $order
$order .= "Total: $".$_SESSION['total'];
Related
I'm trying to submit values from angularjs to a php page, then have the php page send the values to an email. The problem is that the variables sent to the email are printing blank. When in my IDE, the variables will dump to the log properly, but when the same action is performed on a site on the internet (tried on two separate websites), the variables intended to be printed into the email do not print to the log on a dump.
https://github.com/Cameron64/BCIS4610
PHP code:
if ( $_SERVER['REQUEST_METHOD'] == 'POST' && strpos($_SERVER['CONTENT_TYPE'], 'application/json') === 0 ) {
$postdata = file_get_contents('php://input');
$_POST = json_decode($postdata, true);
$_REQUEST = array_merge($_REQUEST, $_POST);
}
//$data['message'] = $_POST['customer'];
$data = json_decode($_POST['customer']);
//var_dump($data);
$basket = array();
$basketQuantities = array();
$email = $data->email;
$phone = $data->num;
$company = $data->restaurant;
$products = $data->product;
$quantities = $data->quantities;
$comments = $data->comments;
$counter = 0;
//var_dump($_POST['customer']);
for($i=0; $i<= sizeof($products,0)-1; $i++){
array_push($basket, $data->product[$i]->id);
}
for($i=0; $i<= sizeof($products,0)-1; $i++){
if($data->quantities[$i] != null)
{$basketQuantities[$i]=$data->quantities[$i];
}
else{
array_push($basketQuantities[$i], "1");
};
}
$final = "";
$final .= "Email: " . $email . "\n"
. "Company Name: ". $company . "\n"
. "Phone Number: ". $phone . "\n".
"They ordered: \n";
for($i=0; $i <= sizeof($products,0)-1; $i++){
$final .= $basket[$i] . "\t" . $basketQuantities[$i] . "\n";
}
if($comments != null){
$final .= "They also left a comment with their order:" . "\n" . wordwrap($comments);
}
$mailTo = "myemail#ymail.com";
$mailSubject = "New Order";
var_dump($final);
if($counter < 1){
mail($mailTo,$mailSubject,$final);
$counter++;
}
AngularJS code
this.basket =[];
this.customer ={};
this.customer.email = "";
this.customer.num = "";
this.customer.restaurant = "";
this.customer.product=[];
this.customer.quantities=[];
this.customer.comments= "";
this.formSubmit = function(){
if(document.getElementById('email').checkValidity() && document.getElementById('tel').checkValidity()
&& document.getElementById('name').checkValidity()){
lol = this;
$http({
method: 'POST',
url: 'php/SendOrder.php',
data: $.param({'customer' : JSON.stringify(lol.customer)}),
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).
success(function(response) {
console.log(response);
window.location = "invoice.html";
})
}
};
The values are populated via an nd-model in for form. I don't believe it is being submitted as a form, don't know if that changes anything.
I am new to php and my webhost does not allow me to run simple php form available on http://www.freecontactform.com/email_form.php
they guided me to a knowledge base and asked me to use this php code i tried to intergrate this with my current form and it just doesn't send any email
Here is the php code:
<?
$mailto = "xyz#yourdomain.com";
$file = "thanks.htm";
$pcount = 0;
$gcount = 0;
$subject = "Mail from Enquiry Form";
$from = "some-name#yourdomain.com";
while (list($key, $val) = each($_POST)) {
$pstr = $pstr . "$key : $val \n ";
++$pcount;
}
while (list($key, $val) = each($_GET)) {
$gstr = $gstr . "$key : $val \n ";
++$gcount;
}
if ($pcount > $gcount) {
$message_body = $pstr;
mail($mailto, $subject, $message_body, "From:" . $from);
include("$file");
} else {
$message_body = $gstr;
mail($mailto, $subject, $message_body, "From:" . $from);
include("$file");
}
?>
Here is the html
http://pastebin.com/Nzq0TCVp
Any suggestions?
I'm trying to make this secret santa auto-email script, and it's really confusing me... It doesn't email properly (one person gets a TON of emails, no one else gets any...)
I tried to just change the length on the array in the for loops, did I do it right?
I use the shuffle function and the email function, which seem to work well (aside from the for loop on the email function - did I do that right?). It's the main function I need help on.
Here's the code:
<?php
function twodshuffle($array)
{
// Get array length
$count = count($array);
// Create a range of indicies
$indi = range(1,$count);
// Randomize indicies array
shuffle($indi);
// Initialize new array
$newarray = array($count);
// Holds current index
$i = 0;
// Shuffle multidimensional array
foreach ($indi as $index)
{
$newarray[$i] = $array[$index];
$i++;
}
return $newarray;
}
function email($Name, $Email, $Likes)
{
$to = $Email;
$subject = '[Secret Santa] Happy Lolidays from Santabot 2020! Your match is here!';
$message = "HEY! You are " . $Name ."'s Secret Santa. Isn't that exciting? \r\n\r\nThis is what your match filled in for likes/dislikes: \r\n----------------------------\r\n" . $Likes . "\r\n----------------------------\r\n\r\n\r\nNow get them something nice and thoughtful. We will be swapping sometime around Christmas, so you have a lot of time to put some thought in it.\r\nLet's keep gifts around the $30 mark.\r\n\r\nHappy Lolidays,\r\nDCo's Santabot 2020.\r\n\r\nPS: I have set this up so not even I know the matches nor is a list stored anywhere. DO NOT delete this email if you are at risk of forgetting your match.";
$headers = 'From: Santabot 2020 <santabot2020#mydomain.com>' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
sleep(1000);
}
$con = mysql_connect("mysql.myserver.com", "username", "password") or die(mysql_error());
mysql_select_db("lolidays_dco", $con) or die(mysql_error());
$array;
//I just changed this array size now. Is this the right syntax?
for ($i = 1; $i <= array.length() -1; $i++) {
$sql = "SELECT * FROM tbl_data WHERE Number = " . $i;
$result = mysql_query($sql);
$count=mysql_num_rows($result);
while($row = mysql_fetch_array($result))
{
$Name = $row['Name'];
$Email = $row['Email'];
$Likes = $row['Likes'];
}
$array[$i] = array("Name" => $Name, "Email" => $Email, "Likes" => $Likes);
}
$array = twodshuffle($array);
$string;
//changed this array length as well, is this the right syntax?
for ($i = 0; $i <= array.length(); $i++) {
if ($i == 2) {
$n = $array['0']["Name"];
$e = $array[$i]["Email"];
$l = $array['0']["Likes"];
email($n, $e, $l);
echo "Email Sent!<br />";
$string = $string . $array[$i]["Name"] . " => " . $array['0']["Name"] . "\r\n";
email('Matches', 'backup#email.com', $string);
}
else {
$j = $i + 1;
$n = $array[$j]["Name"];
$e = $array[$i]["Email"];
$l = $array[$j]["Likes"];
email($n, $e, $l);
echo "Email Sent!<br />";
$string = $string . $array[$i]["Name"] . " => " . $array[$j]["Name"] . "\r\n";
}
}
echo "<strong>Done!</strong>";
?>
You've hard-coded the recipient:
$n = $array['0']["Name"]; <---should be $i, not '0'
$e = $array[$i]["Email"];
$l = $array['0']["Likes"]; <---should be $i, not '0'
Hi I have a php form that works perfectly when it sends an email to one person but when I add another email address it doesn't send an email to either address. I have been looking on php sites but can't see why my form is now refusing to email once the second email address is added.
<?php
function isRequestSet( $name ) {
if ( isset ( $_REQUEST[$name] ) ) {
return ( $_REQUEST[$name] != "" ) ;
}
return false;
}
$name = "";
if ( isRequestSet('name' ) ) {
$name = $_REQUEST['name'];
}
$number = "";
if ( isRequestSet('number') ) {
$number = $_REQUEST['number'];
}
$email = "";
if ( isRequestSet( 'email' ) ) {
$email = $_REQUEST['email'];
}
$postcode = "";
if ( isRequestSet('postcode' ) ) {
$location = $_REQEUST['postcode'];
}
$how_did_you_hear_about_us = array();
if ( isset( $_REQUEST['how_did_you_hear_about_us'] ) ) {
$how_did_you_hear_about_us = $_REQUEST['how_did_you_hear_about_us'];
}
$message = "";
if ( isRequestSet('message' ) ) {
$location = $_REQEUST['message'];
}
$apartment_price_range = array();
if ( isset( $_REQUEST['apartment_price_range'] ) ) {
$apartment_price_range = $_REQUEST['apartment_price_range'];
}
$url = "";{
$url = $_REQUEST['url'];
}
$property = "";{
$property = $_REQUEST['property'];
}
if ( ($name !="") && ($number != "") && ($email != "") && ($isspam !="yes") ) {
$to = 'name#email.com,name#email2.com';
$from = $to;
$headers = 'From: ' . $to . "\n" .
'Reply-To: ' . $to . "\n";
$vars = array( 'name' , 'number' , 'email' , 'postcode' , 'message' ) ;
$message = "-----------\n" ;
foreach ( $vars as $v ) {
$value = $_REQUEST[$v];
$message .= "$v:\t$value\n";
}
$message .= "-----------\n" ;
$message .= "\nHow did you hear about apartments?:\n" ;
foreach ( $how_did_you_hear_about_us as $how_did_you_hear_about_us ) {
$message .= "$how_did_you_hear_about_us\n" ;
}
$message .= "-----------\n" ;
$message .= "\nApartment price range:\n" ;
foreach ( $apartment_price_range as $apartment_price_range ) {
$message .= "$apartment_price_range\n" ;
}
$subject = "From: $name <$email>";
mail( $to , $subject , $message , $headers, "-f $from" );
$confirm = true;
//redirect to the 'thank you' page
header("Location:http://website.com/file/thankyou.php");
} else {
$confirm = false;
}
?>
Most likely it is because you use multiple addresses for From and Reply-to fields:
$to = 'name#email.com,name#email2.com';
$from = $to;
Change it to use either first email or something like your-service-name#you-domain-name.com
Use only one address for From and Reply-To.
$from = 'me#my-domain.com';
$headers = 'From: ' . $from . "\n" .
'Reply-To: ' . $from . "\n";
"In the line below you should break the $to field into an array.
mail( $to , $subject , $message , $headers, "-f $from" );
For example
$address_array = split(",", $to);
foreach($address_array as $address)
{
mail( $address, $subject , $message , $headers, "-f $from" );
}
This allows for you $to string to contain as many emails as desired.
N.b if you wish to skip the split line just store the $to as an array
$to = array("name#email.com","name#email2.com");
make your email part into a function.
will much more easier for you to pass the email address.
function email_to_user($email_address){
$to = $email_address;
<rest of the codes>
}
and you can easily pass the email address
email_to_user(name1#mail.com);
email_to_user(name2#mail.com);
How do I get the values of php checkboxes in a form to show when emailed to the recipient?
I am learning how to use php but I can't figure this one out with the form i have generated.
Below is the checkbox code from the form itself:
<input type="checkbox" value="Please send me a Travel Planner" name="options[]">
<input type="checkbox" value="Please send me a Visitor Map" name="options[]" />
<input type="checkbox" value="Please sign me up for the email newsletter" name="options[]" />
Now here's the form code from the feedback page that processes it:
#<?php
// ------------- CONFIGURABLE SECTION ------------------------
// $mailto - set to the email address you want the form
// sent to, eg
//$mailto = "youremailaddress#example.com" ;
$mailto = 'xxxxx#xxxxxxxxx.com' ;
// $subject - set to the Subject line of the email, eg
//$subject = "Feedback Form" ;
$subject = "Request For Visitor Guide" ;
// the pages to be displayed, eg
//$formurl = "http://www.example.com/feedback.html" ;
//$errorurl = "http://www.example.com/error.html" ;
//$thankyouurl = "http://www.example.com/thankyou.html" ;
$formurl = "http://www.example.com/requestform_mtg.php" ;
$errorurl = "http://www.example.com/error.php" ;
$thankyouurl = "http://www.example.com/thankyou.php" ;
$email_is_required = 1;
$name_is_required = 1;
$address_is_required = 1;
$contactname_is_required = 1;
$city_is_required = 1;
$zip_is_required = 1;
$phone_is_required = 1;
$uself = 0;
$use_envsender = 0;
$use_webmaster_email_for_from = 1;
$use_utf8 = 1;
// -------------------- END OF CONFIGURABLE SECTION ---------------
$headersep = (!isset( $uself ) || ($uself == 0)) ? "\r\n" : "\n" ;
$content_type = (!isset( $use_utf8 ) || ($use_utf8 == 0)) ? 'Content-Type: text/plain; charset="iso-8859-1"' : 'Content-Type: text/plain; charset="utf-8"' ;
if (!isset( $use_envsender )) { $use_envsender = 0 ; }
$envsender = "-f$mailto" ;
$name = $_POST['name'] ;
$contactname = $_POST['contactname'] ;
$title = $_POST['title'] ;
$email = $_POST['email'] ;
$address = $_POST['address'] ;
$city = $_POST['city'] ;
$state = $_POST['state'] ;
$zip = $_POST['zip'] ;
$fax = $_POST['fax'] ;
$phone = $_POST['phone'] ;
$mtgname = $_POST['mtgname'] ;
$dates = $_POST['dates'] ;
$attendance = $_POST['attendance'] ;
$guestroom = $_POST['guestroom'] ;
$mtgroom = $_POST['mtgroom'] ;
$timeframe = $_POST['timeframe'] ;
$options = $_POST['options'] ;
$comments = $_POST['comments'] ;
$http_referrer = getenv( "HTTP_REFERER" );
if (!isset($_POST['email'])) {
header( "Location: $formurl" );
exit ;
}
if (($email_is_required && (empty($email) || !ereg("#", $email))) || ($name_is_required && empty($name)) || ($address_is_required && empty($address)) || ($contactname_is_required && empty($contactname)) || ($city_is_required && empty($city)) || ($zip_is_required && empty($zip)) || ($phone_is_required && empty($phone))) {
header( "Location: $errorurl" );
exit ;
}
if ( ereg( "[\r\n]", $name ) || ereg( "[\r\n]", $email ) || ereg( "[\r\n]", $address ) || ereg( "[\r\n]", $contactname ) ) {
header( "Location: $errorurl" );
exit ;
}
if (empty($email)) {
$email = $mailto ;
}
$fromemail = (!isset( $use_webmaster_email_for_from ) || ($use_webmaster_email_for_from == 0)) ? $email : $mailto ;
if (get_magic_quotes_gpc()) {
$comments = stripslashes( $comments );
}
$messageproper =
"This message was sent from:\n" .
"$http_referrer\n" .
"------------------------------------------------------------\n" .
"Organization Name: $name\n" .
"Contact Name: $contactname\n" .
"Email of sender: $email\n" .
"Address of sender: $address\n" .
"City of sender: $city\n" .
"State of sender: $state\n" .
"Zip Code of sender: $zip\n" .
"Fax of sender: $fax\n" .
"Phone of sender: $phone\n" .
"Meeting Name: $mtgname\n" .
"Preferred Dates: $dates\n" .
"Expected Attendance: $attendance\n" .
"Guest Rooms: $guestroom\n" .
"Largest Meeting Room Needed: $mtgroom\n" .
"Decision Timeframe: $timeframe\n" .
"Options: $options\n" .
"------------------------- COMMENTS -------------------------\n\n" .
$comments .
"\n\n------------------------------------------------------------\n" ;
$headers =
"From: \"$name\" <$fromemail>" . $headersep . "Reply-To: \"$name\" <$email>" . $headersep . "X-Mailer: chfeedback.php 2.13.0" .
$headersep . 'MIME-Version: 1.0' . $headersep . $content_type ;
if ($use_envsender) {
mail($mailto, $subject, $messageproper, $headers, $envsender );
}
else {
mail($mailto, $subject, $messageproper, $headers );
}
header( "Location: $thankyouurl" );
exit ;
?>
All I get via email for the checkboxes is "array".
Thanks
Update:
Just noticed I get these errors if I DON'T select a checkbox and submit:
Warning: implode() [function.implode]: Invalid arguments passed in /home/content/o/l/t/oltvcb/html/feedback_mtg.php on line 148
Warning: Cannot modify header information - headers already sent by (output started at /home/content/o/l/t/oltvcb/html/feedback_mtg.php:148) in /home/content/o/l/t/oltvcb/html/feedback_mtg.php on line 162
I did notice that the form data actually came through in my email.
$options is an array. Try imploding it.
$options = implode(', ', $options);
Checkboxes are treated as an array when they are submitted.
foreach($options as $option) {
print $option."\n";
}
or
print implode("\n", $options);
Here's a suggestion. Instead of this:
"Options: $options\n" .
Try this:
"Options:".implode("\n",$options)."\n".
There's always the possibility that no array of $options will exist (no check boxes were checked). In this case, you can do something like:
"Options:".(isset($options) ? implode("\n",$options) : "")."\n".
$options = join(', ', $_POST['options']);
First, you need to get rid of your "[ ]" in the name of your variable for the HTML checkboxes.
If you name it "options" when it is submitted to the processing page it comes through as an array.
From that point, you now have an array of the values selected.
$_POST['options']