PHP Form Checkboxes - php

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']

Related

Angularjs POST to PHP printing blank values in email and console

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.

Write from WordPress plugin to text file with PHP

I am trying to write date to a text file from a WordPress plugin. While this works for a single PHP file it doesn't write when I add the code to the plugin. The TXT file has permission 777 and is in the same directory as the plugin file.
What am I doing wrong?
This is the plugin and the lines I have added are in the block //log 404s to text file:
<?php
/*
Plugin Name: Mail me 404 errors
Plugin URI: http://me.com
Description: A 404 status triggers an email with details.
Version: 1.0
Author: Me
Author URI: http://me.com
*/
//SENDS 404 EMAIL TO ADMIN
function email_admin($location){
// ip address
$ipaddress = $_SERVER['REMOTE_ADDR'];
if (!empty($_SERVER['X_FORWARDED_FOR'])) {
$X_FORWARDED_FOR = explode(',', $_SERVER['X_FORWARDED_FOR']);
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$X_FORWARDED_FOR = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
}
else {$ipaddress = "undefined";}
if (!empty($X_FORWARDED_FOR)) {
$ipaddress = trim($X_FORWARDED_FOR[0]);
}
// site info
$blname=get_option('blogname');
$admemail = get_option('admin_email');
$honeypot = "http://www.projecthoneypot.org/ip_".$ipaddress;
// time log
$time = date("F jS Y, H:i", time()+25200);
//referrer
function current_page_url(){
$page_url = 'http';
if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on'){
$page_url .= 's';
}
return $page_url.'://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
}
if(isset($_SESSION['referrer'])){
$referrer = $_SESSION['referrer'];
} elseif(isset($_SERVER['HTTP_REFERER'])){
$referrer = $_SERVER['HTTP_REFERER'];
} else {$referrer = "undefined";}
$_SESSION['referrer'] = current_page_url();
// query string
if (isset($_SERVER['QUERY_STRING'])) {
$string = $_SERVER['QUERY_STRING'];
} else {
$string = "undefined";
}
// request URI
if (isset($_SERVER['REQUEST_URI']) && isset($_SERVER["HTTP_HOST"])) {
$request = 'http://' . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
} else {
$request = "undefined";
}
// identity
if (isset($_SERVER['REMOTE_IDENT'])) {
$remote = $_SERVER['REMOTE_IDENT'];
} else {
$remote = "undefined";
}
// user agent
if (isset($_SERVER['HTTP_USER_AGENT'])) {
$agent = $_SERVER['HTTP_USER_AGENT'];
} else {
$agent = "undefined";
}
//log 404s to txt file
$ipad = $_SERVER['REMOTE_ADDR'];
$ban = "#$time\r\n$ipad\r\n";
$file = "errors.txt";
$open = #fopen($file, "a");
$write = #fputs($open, $ban);
#fclose($open);
//log 404s to txt file
$mailhead = "MIME-Version: 1.0\r\n";
$mailhead .= "Content-type: text/plain; charset=UTF-8\r\n";
$mailhead .= 'From: "' . $blname . '" <' .$admemail. ">\r\n";
$mailsubj= $blname.': 404 error';
$mailintro = "Someone wanted to go to ".$request.", but it doesn't exist. Maybe you can have a look and see if anything needs to be fixed.\r\n";
$mailbody=
$mailintro . "\n" .
"TIME: " . $time . "\n" .
"*404: " . $request . "\n" .
"REFERRER: " . $referer . "\n" .
"QUERY STRING: " . $string . "\n" .
"REMOTE ADDRESS: " . $ipaddress . "\n" .
"REMOTE IDENTITY: " . $remote . "\n" .
"USER AGENT: " . $agent . "\n" .
"CHECK WHOIS: https://who.is/whois-ip/ip-address/". $ipaddress . "\n" .
"CHECK IP ADDRESS: " . $honeypot . "\n\n\n";
#mail($admemail,$mailsubj,$mailbody,$mailhead);
}
function mail_me_errors(){
global $wp_query;
$location=$_SERVER['REQUEST_URI'];
if ($wp_query->is_404){
email_admin($location);
}
}
add_action('get_header', 'mail_me_errors');
?>
Pass the full path of the file /wp-content/plugins/your-plugin/errors.txt to fopen with plugin_dir_path():
$file = plugin_dir_path( __FILE__ ) . '/errors.txt';
$open = fopen( $file, "a" );
The following is a minimum example:
add_action( 'get_header', 'mail_me_errors' );
function mail_me_errors() {
if ( is_404() ) {
email_admin( $_SERVER['REQUEST_URI'] );
}
}
function email_admin( $location ) {
$time = date( "F jS Y, H:i", time()+25200 );
$ban = "#$time\r\n$location\r\n";
$file = plugin_dir_path( __FILE__ ) . '/errors.txt';
$open = fopen( $file, "a" );
$write = fputs( $open, $ban );
fclose( $open );
}

Send email according to input select value

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

My PHP form will not send to multiple users

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);

PHP script performance issue

There seems to be a performance issue with the script as it is really slow. I was wondering what I could do to speed this up. If you have any ideas, please let me know. I can't seem to figure it out.
Below is the code:
<?php
include_once("connect.php.inc");
class HtmlEnc{
static function uniord($c) {
$ud = 0;
if (ord($c{0}) >= 0 && ord($c{0}) <= 127) $ud = ord($c{0});
if (ord($c{0}) >= 192 && ord($c{0}) <= 223) $ud = (ord($c{0})-192)*64 + (ord($c{1})-128);
if (ord($c{0}) >= 224 && ord($c{0}) <= 239) $ud = (ord($c{0})-224)*4096 + (ord($c{1})-128)*64 + (ord($c{2})-128);
if (ord($c{0}) >= 240 && ord($c{0}) <= 247) $ud = (ord($c{0})-240)*262144 + (ord($c{1})-128)*4096 + (ord($c{2})-128)*64 + (ord($c{3})-128);
if (ord($c{0}) >= 248 && ord($c{0}) <= 251) $ud = (ord($c{0})-248)*16777216 + (ord($c{1})-128)*262144 + (ord($c{2})-128)*4096 + (ord($c{3})-128)*64 + (ord($c{4})-128);
if (ord($c{0}) >= 252 && ord($c{0}) <= 253) $ud = (ord($c{0})-252)*1073741824 + (ord($c{1})-128)*16777216 + (ord($c{2})-128)*262144 + (ord($c{3})-128)*4096 + (ord($c{4})-128)*64 + (ord($c{5})-128);
if (ord($c{0}) >= 254 && ord($c{0}) <= 255) $ud = false; // error
return $ud;
}
static function toHtml($str){
$html_str = "";
while (strlen($str) > 0) {
preg_match("/^(.)(.*)$/u", $str, $match);
$test = utf8_decode($match[1]);
if ($test != "?") {
$html_str .= htmlentities(htmlentities($test));
} else if (strlen($match[1]) > 1) {
$html_str .= "&#".self::uniord($match[1]).";";
} else $html_str .= htmlentities(htmlentities($match[1]));
$str = $match[2];
}
return $html_str;
}
}
/*
List of mail servers
*/
function alreadyDone($domain){
$domain = strtolower($domain);
$qry = "SELECT * FROM emdb WHERE domain ='" . $domain . "'";
$result = mysql_query($qry);
return (mysql_num_rows($result)!=0);
}
$template_fn = $_REQUEST['template'];
//"mail_template.html";
$keywords = HtmlEnc::toHtml($_REQUEST['Keywords']);
$keywords = str_replace("&","&",$keywords);
$domain = $_REQUEST['Domain'];
$rank = $_REQUEST['Rank'];
$to = $_REQUEST['Email'];
$adminEmail = "test#example.com";
if (!alreadyDone($domain)) {
if ($to=="") {
$to = "info#" . $domain;
}
function int_divide($x, $y) {
if ($x == 0) return 0;
if ($y == 0) return FALSE;
return ($x - ($x % $y)) / $y;
}
$page = int_divide($rank,10) + 1;
if ($template_fn == "mail_template_nick.html" || $template_fn == "mail_template_chet.html" || "mail_template_salesperson.php")
$subject = $domain." is on Page ".$page." of Google - Want to be #1?";
elseif ($template_fn == "seo_template.html")
$subject = "Outsource your SEO - Lowest rates guaranteed!";
elseif ($template_fn == "adwords_template.html")
$subject = $domain . " - Save your money on Google Adwords";
else $subject = $domain . " is ranked " . $rank . " on Google - Be 1st!";
$message = file_get_contents($template_fn);
/*$message = "<body>
<p>Hi There,</p>
<p>How's your week been so far?</p>
<p>When I Googled "{KEYWORD}", I found {WEBSITE} on page {PAGE}, not on page 1. This means consumers will find your competitors before they find you!</p>
<p>93% of all people, never go past the 1st page of Google, so at this very moment you're losing sales & leads to a competitor. </p>
<p>If you agree your Google exposure needs drastic improvement, please call me for a chat, I'm sure I can give some good, free advice. </p>
<p> </p>
<p><strong>Best Regards,</strong></p>
<p><strong>Kayne Chong </strong><strong>- Business Development Director</strong></p>
<p><strong>Tel:</strong> | <strong>Fax: </strong><br />
<strong>Office:</strong> <br />
<strong>Web:</strong> <a href='http://www.seoagency.com.sg/' target='_blank'><em>www.seoagency.com.sg</em></a><br />
<strong><em>Web marketing that brings BUSINESS to you!</em></strong></p>
</body>";*/
$message = str_replace("{WEBSITE}", $domain , $message);
$message = str_replace("{PAGE}", $page , $message);
//$message = str_replace("{RANK}", $rank , $message);
$message = str_replace("{KEYWORD}", $keywords , $message);
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
/*$headers .= 'Bcc: ' . $adminEmail . "\r\n";
if ($template_fn == "mail_template_salesperson.php")
{ $headers .= 'From: Kayne - Web Marketing Experts <test#example.com>' . "\r\n";
$headers .= 'Reply-To: test#example.com' . "\r\n";}
elseif ($template_fn == "mail_template_chet.html")
{ $headers .= 'From: Chester - Web Marketing Experts <test#example.com>' . "\r\n";
$headers .= 'Reply-To: test#example.com' . "\r\n";}*/
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'Bcc: ' . $adminEmail . "\r\n";
$headers .= 'From: Info - Web Marketing Experts <test#example.com>' . "\r\n";
$headers .= 'Reply-To: test#example.com' . "\r\n";
if (mail($to, $subject, $message, $headers)) {
echo "Mail successfully sent to $to and $adminEmail";
} else echo "Mail sending failed!";
$qry = "INSERT INTO emdb (domain, keywords, rank, last, count) VALUES ('$domain','$keywords','$rank',CURDATE(), '1')";
mysql_query($qry) or die(mysql_error());
echo "<BR />";
echo "DB updated";
} else {
echo "Domain name $domain has already been processed";
}
?>
Thank you.
Jae
Replace every string concatenation with the "." operator with an array push, for example array [ ] = "foo" and then return a string concatenation implode ( array );
Use ob_start(); to cache the output:
ob_start();
echo $a,$b,$c;
$str = ob_get_contents();
ob_end_clean();
You can optimize if to switch and change the order to your expected result. For example if result a is more likely then result b the condition to catch result a should be the first condition.
Put a primary key and secondary key on your table(s).
1.1) Don't use a glue and don't add the construction of the array to the time. Here is a benchmark for http://www.sitecrafting.com/blog/php-string-concat-vs-array
1.2) http://dan.doezema.com/2011/07/php-output-profiling-echo-vs-concat ( although echo is fastest concat is slower then array and also he uses a glue!
1.3) https://stackoverflow.com/questions 1.4) http://www.sitepoint.com/high-performance-string-concatenation-in-php/
Here are my results (30000 strings, time in milliseconds) (Script is taken from 1.4):
standard: 0.02418089
implode w/ glue: 0.00435901
implode w/o glue: 0.02205801
foreach: 0.02081609
Conclusion: use implode with glue.
Your toHtml() is pointless (not to mention it's implemented poorly hence the low performance), you don't need to convert every unicode character to &#...; notation, just put this in your <head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
and print utf-8 strings as they are, your browser will know how to deal with them.

Categories