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);
Related
We have a php script that emails form field values when a user submits the form. The form action points to the script below.
We've been asked to configure things to use expressmail explicitly. My question is, would this entail a modification to the script or is this a config setting on the server somewhere?
<?php
if (! $_POST) {
header('HTTP/1.0 405 Method Not Allowed');
exit;
}
$redirectTo = html_entity_decode($_POST['post']);
$body = '<html><body>';
$content = array();
foreach ($_POST as $key => $value) {
if ('-label' !== substr($key, -6)) {
continue;
}
$field = substr($key, 0, strlen($key) - 6);
$content[$field]['value'] = $_POST[$field];
$content[$field]['label'] = $_POST[$key];
}
$body .= '<h1>' . htmlentities($_POST['formName']) . '</h1>';
foreach ($content as $field => $value) {
$data = $value['value'];
$label = $value['label'];
$body .= '<p><b>' . htmlentities($label) . '</b><br />';
if (false === is_array($data) && (null === $data OR "" === trim($data))) {
$body .= 'N/A';
} elseif (is_array($data)) {
$body .= '<ul>';
foreach ($data as $val) {
$val = htmlentities($val);
$body .= '<li>' . $val . '</li>';
}
$body .= '</ul>';
} else {
$body .= htmlentities($data);
}
$body .= '</p>';
}
$body .= '</body></html>';
$to = strip_tags($_POST['emailTo']);
$subject = strip_tags($_POST['emailSubject']);
$headers = "From: " . strip_tags($_POST['emailFrom']) . "\r\n";
$headers .= "Reply-To: " . strip_tags($_POST['emailFrom']) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
mail($to, $subject, $body, $headers);
header('Location: ' . $redirectTo);
?>
I need to make changes to an existing form by adding a dropdown menu where I will have two inputs and their values. The purpose is to send the form to recipient_one if Address 1 is selected or to recipient_two when Address 2 is selected. Address 1 needs to be the default value when nothing is selected.
Here is just the added HTML:
<form method="post" action="./index.php" enctype="multipart/form-data">
<fieldset class="elist">
<legend>Select shop:</legend>
<select name="shop">
<option name="address-chosen" value="Tammsaare" >Tammsaare</option>
<option name="address-chosen" value="Ülemiste" >Ülemiste</option>
</select>
</fieldset>
</form>
and the PHP:
$action = isset($_POST['action']) ? $_POST['action'] : null;
$page = null;
$pages = array('info', 'en');
if( isset($_GET['page']) && in_array($_GET['page'], $pages) ) {
$page = $_GET['page'];
}
if( !in_array($page, $pages) ) {
$page = '';
}
$mail_sent = false;
if( $action == 'add' ) {
//Test if it is a shared client
if (!empty($_SERVER['HTTP_CLIENT_IP'])){
$ip = $_SERVER['HTTP_CLIENT_IP'];
//Is it a proxy address
}elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}else{
$ip = $_SERVER['REMOTE_ADDR'];
}
$message = '';
$message .= 'Name: '.safe($_POST['name'])."\r\n";
$message .= 'E-mail: '.safe($_POST['email'])."\r\n";
$message .= 'Phone: '.safe($_POST['telephone'])."\r\n";
$message .= 'Mark: '.safe($_POST['mark'])."\r\n";
$message .= 'Model: '.safe($_POST['model'])."\r\n";
$message .= 'Shop: '.safe($_POST['address-chosen'])."\r\n";
$message .= "Wants newsletter: ".$soovib_uudiskirja = isset($_POST['newsletter']) ? "Yes" : "No";
$message .= "\r\n";
$message .= "\r\n";
$message .= "\r\n";
$message .= 'Aeg: '.date('d.m.Y H:i')."\r\n";
$message .= 'IP: '.$ip."\r\n";
$mail_data = array(
'to_email' => 'email#mail.com',
'from_email' => 'email#mail.com',
'from_name' => 'Stock Cars',
'subject' => 'Reservation',
'message' => $message,
);
mail_send($mail_data);
$mail_sent = true;
}
function safe( $name ) {
return( str_ireplace(array( "%0a", "%0d", "Content-Type:", "bcc:","to:","cc:" ), "", $name ) );
}
function mail_send($arr)
{
if (!isset($arr['to_email'], $arr['from_email'], $arr['subject'], $arr['message'])) {
throw new HelperException('mail(); not all parameters provided.');
}
$to = empty($arr['to_name']) ? $arr['to_email'] : '"' . mb_encode_mimeheader($arr['to_name']) . '" <' . $arr['to_email'] . '>';
$from = empty($arr['from_name']) ? $arr['from_email'] : '"' . mb_encode_mimeheader($arr['from_name']) . '" <' . $arr['from_email'] . '>';
$headers = array
(
'MIME-Version: 1.0',
'Content-Type: text/plain; charset="UTF-8";',
'Content-Transfer-Encoding: 7bit',
'Date: ' . date('r', $_SERVER['REQUEST_TIME']),
'Message-ID: <' . $_SERVER['REQUEST_TIME'] . md5($_SERVER['REQUEST_TIME']) . '#' . $_SERVER['SERVER_NAME'] . '>',
'From: ' . $from,
'Reply-To: ' . $from,
'Return-Path: ' . $from,
'X-Mailer: PHP v' . phpversion(),
'X-Originating-IP: ' . $_SERVER['SERVER_ADDR'],
);
mail($to, '=?UTF-8?B?' . base64_encode($arr['subject']) . '?=', $arr['message'], implode("\n", $headers));}
if (isset($_GET['page'])) {}
So the question is how do I reconstruct the array?
Set the email variable based on the form value:
//default is email 1
$email='email#mail.com';
if(isset($_POST['shop']) && $_POST['shop']=='Ülemiste') {$email='email2#mail2.com';}
$mail_data = array(
'to_email' => $email,
'from_email' => $email,
'from_name' => 'Stock cars',
'subject' => 'Reservation',
'message' => $message,
);
edited as per your edit - not sure how the umlaut will effect things though
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 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'];
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']