rotate recipient of my webform submit - java

I need to add a code to my sendEmail.php bellow which would rotate evenly between two or more recipient. Right now all goes to one recipent "sendto" myemail#gmail.com. So I want to add more myemail-1#gmail.com, myemail-2#gmail.com and so on. This way each will receive fresh leads.
<?php
ob_flush();
session_start();
$_SESSION['username'] = $_POST['username'];
$_SESSION['email'] = $_POST['email'];
$_SESSION['phone'] = $_POST['phone_1']."- ".$_POST['phone_2']."- ".$_POST['phone_3'];
$_SESSION['liberation'] = $_POST['liberation'];
$sendto = "myemail#gmail.com";
$email = $_POST['email'];
$username= nl2br($_POST['username']);
$subject = "New lead from my website";
$headers = "From: <form#manysites.com> \r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html;charset=utf-8 \r\n";
$msg = "<html><body style='font-family:Arial,sans-serif;'>";
$msg .= "<p><strong>Name:</strong> ".$_POST['username']."</p>\r\n";
$msg .= "<p><strong>Sent by:</strong> ".$_POST['email']."</p>\r\n";
$msg .= "<p><strong>Phone No.: </strong> ".$_POST['phone_1']."- ".$_POST['phone_2']."- ".$_POST['phone_3']."</p>\r\n";
$msg .= "<p><strong> conviction date:</strong> ".$_POST['liberation']."</p>\r\n";
$msg .= "</body></html>";
#mail($sendto, $subject, $msg, $headers);
header("Location:continue.php");
?>

You can simply use the comma operator and then send it right away.. like this..
$sendto = 'myemail1#gmail.com,myemail2#gmail.com,myemail3#gmail.com';
mail($sendto, $subject, $msg, $headers);
Alternatively, you can use an implode() too..
$myemails = array('myemail1#gmail.com','myemail2#gmail.com','myemail3#gmail.com');
$sendto = implode(',',$myemails);
mail($sendto, $subject, $msg, $headers);
Sending them separately....
$myemails = array('myemail1#gmail.com','myemail2#gmail.com','myemail3#gmail.com')
foreach($myemails as $email)
{
mail($email, $subject, $msg, $headers);
}

I think the challenge here is that this script runs with no knowledge of what happened on any occasion that it previously ran. So, one option would be to store the information about who received the last lead. This way when the next person fills in your web form, you can retrieve the value of the last recipient and send the current lead to the next recipient in an array of defined recipients (then update the information about who was the last recipient for the next time).
If you don't have access to a database (or file or something else that is a 'permanent' type of storage) to store the value of the last recipient in, you could simply randomize who receives the current lead. The law of averages suggests that over time, the lead distribution should be relatively equal, though in practice this could result in one person getting all of the leads in a given period.
$recipients=array("recipient1#email.com","recipient2#email.com");
$randnum = mt_rand(0,1);
$sendto = $recipients[$randnum];

Related

Mail script needs running every hour, but cron not working

Below is the fully functioning code. It works when I call it via the browser. However, I can't get my Cron job to run it. I have tried coming up with any solution and am open to any ideas that will make this script fire off once every hour 24/7 365 so to speak.
Info:
Hostgator
PHP 5.4
Link for Hostgator Cron commands: http://support.hostgator.com/articles/cpanel/what-do-i-put-for-the-cron-job-command?utm_source=cPanel&utm_medium=message&utm_campaign=Cron%20Jobs
I have tried the following:
When doing any of these I have yet to have it add a sale to the database or send a single email:
/opt/php54/bin/php /home1/user/public_html/Sales/scripts/sales-notif_em.php - Could not open input file: /home1/user/public_html/scripts/sales-notif_em.php
php /home1/user/public_html/Sales/scripts/sales-notif_em.php - blank email with nothing but this showing: Content-type: text/html
php -q /home1/lotscav1/public_html/Sales/scripts/sales_notif_em.php - I don't get any emails from the system of course
/usr/bin/curl home1/user/public_html/scripts/sales-notif_em.php - curl: (3) malformed
any command (like php or /usr/bin/curl) that has the second half with http://website.c0m/Sales/scripts/sales_notif_em.php - causes a JSON Error when trying to add the Cron job
I have checked the following things:
File permissions are 655
Timestamp is less than an hour old under the customers table
Here is my code:
<?php
//find out current time and 1 hour ago
date_default_timezone_set('America/New_York');
$current_time = strtotime("now");
$hour_ago = strtotime('-1 hour');
//////////////////////////////////////////////////////////////////
/////////////////l// Connect to Sales Database ///////////////////
//////////////////////////////////////////////////////////////////
$mysqli_s = new mysqli("localhost", "user", "password",
"server_sales_data");
if ($mysqli_s->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli_s->connect_errno . ")
" . $mysqli_s->connect_error;
}
//////////////////////////////////////////////////////////////////
///////////////////// Connect to EM Database ////////////////////
//////////////////////////////////////////////////////////////////
$mysqli_em = new mysqli("localhost", "user", "password",
"server_dlgEM");
if ($mysqli_em->connect_errno) {
echo "Failed to connect to MySQL_EM: (" . $mysqli_em->connect_errno .
") " . $mysqli_em->connect_error;
}
//Grab store name
$dlg_store = "EM";
$em_request = "SELECT * FROM customers WHERE date BETWEEN '$hour_ago'
AND '$current_time'";
$em_result = mysqli_query($mysqli_em, $em_request) or die("Error No
Sales EM");
while ($em_row = mysqli_fetch_array($em_result)) {
$em_prod_num = $em_row["prod_num"];
$em_receipt = $em_row["receipt"];
//////////////////////////////////////////////////////////////////
///////////////////// Grab info for EM Sales ////////////////////
//////////////////////////////////////////////////////////////////
$request_s = "SELECT * FROM all_products WHERE
dlgprod_num='$em_prod_num' AND dlg_store='$dlg_store'";
$result_s = mysqli_query($mysqli_s, $request_s) or die("Error dlg
prod num EM");
while ($row_s = mysqli_fetch_array($result_s)) {
$sku_s = $row_s["sku"];
$dlgprod_num_s = $row_s["dlgprod_num"];
$book_title_s = addslashes($row_s["book_title"]);
$dlgprod_price_s = $row_s["dlgprod_price"];
$author_name_s = addslashes($row_s["author_name"]);
$author_email_s = $row_s["author_email"];
$publisher_s = $row_s["publisher"];
$dlg_store_s = $row_s["dlg_store"];
$add_sql_s = "INSERT INTO all_author_sales SET
`sku`='$sku_s',
`dlgprod_num`='$dlgprod_num_s',
`dlgprod_nam`='$book_title_s',
`dlgprod_price`='$dlgprod_price_s',
`author_name`='$author_name_s',
`author_email`='$author_email_s',
`publisher`='$publisher_s',
`dlg_store`='$dlg_store_s',
`dlgcustomer_receipt`='$em_receipt' ";
//create signature
$sig = "The Admin Team at www.website.com";
//to
$admin_email = "admin#website.com";
$to = array($author_email_s, $admin_email);
//setup email headers
$headers='From: ' . $admin_email . "\r\n" .
'Reply-To: ' . $admin_email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= $emailbody."\n\n";
//email subject and body
$subject = "Your book stats";
$message = "
Hi $author_name_s,<br />
I just wanted to send you a message and let you know that the book or
books below have just been purchased.<br /><br />
Store: $dlg_store_s<br />
Receipt: $em_receipt<br />
Sku Number: $sku_s<br /><br />
Book Title: $book_title_s<br />
Publisher: $publisher_s<br />
Product Number: $dlgprod_num_s<br />
Price: $dlgprod_price_s<br /><br />
Sincerely,<br />
$sig<br /><br />
To remove yourself from this notification, please send an email to
$admin_email with Unsubscribe in the subject line.
";
if ($mysqli_s->multi_query($add_sql_s) === TRUE) {
mail (implode(',', $to), $subject, $message, $headers);
} else {
echo "Error: " . $add_sql_s . "<br>" . $mysqli_s-
>error . "<br>" . $string;
}
}
}
?>
My bet is with wrong path to the php file you want to execute.
Try using terminal if you can, navigate to the folder where the file is placed and try running it from there via command line.
Also mind You are refering to two different names: sales-notif_em.php & sales-notif_me.php.
(Posted on behalf of the OP).
Thank you for all your help. I made a custom product with my email as the author for testing purposes. In the customers table I inserted products 111111 and 111110. However, in all_products I gave products 11111111 and 11111110. tHE end result is I made a mistake.
I hope you are able to learn something from this event. Knowledge is more important than my ego. Thank you!

Convert Currency

I have got an App where it's needed to convert a set amount of money at runtime, as the App will be deployed in different countries.
Let's say I have a set price at 100$. Is there a simple way to convert it to another currency (that I get based on the Locale of the device)?
Cheers
I am using google finance calculator as user interact with your app you can fetch converted data by using it in php.
function convertCurrency($amount, $from, $to){
$url = "https://www.google.com/finance/converter?a=$amount&from=$from&to=$to";
$data = file_get_contents($url);
preg_match("/<span class=bld>(.*)<\/span>/",$data, $converted);
$converted = preg_replace("/[^0-9.]/", "", $converted[1]);
return round($converted, 3);
}
I'm using USD to INR converter.
call this function
echo convertCurrency(1, "USD", "INR");
you can't do that by java code cause currency rates flatuating every time.. you can use this api it will updated automatically when currency rates change---
<?php
$from = $_POST["from"];
$to = $_POST["to"];
$amount = $_POST["amount"];
$rawData = currencyConvert($from,$to,$amount);
$regex = '#\<span class=bld\>(.+?)\<\/span\>#s';
preg_match($regex, $rawData, $converted);
$result = $converted[0];
preg_match('!\d+(?:\.\d+)?!', $result, $result);
echo $result[0];
function currencyConvert($from,$to,$amount){
$url = "http://www.google.com/finance/converter?a=$amount&from=$from&to=$to";
$request = curl_init();
$timeOut = 0;
curl_setopt ($request, CURLOPT_URL, $url);
curl_setopt ($request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($request, CURLOPT_USERAGENT,"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
curl_setopt ($request, CURLOPT_CONNECTTIMEOUT, $timeOut);
$response = curl_exec($request);
curl_close($request);
return $response;
}
?>

PHPRunner: email array , can't personalize message

I’m using PHPRunner and I'm trying to create a button to send an email to selected users with personalized body message. I found an example in PHPRunner support page (Send an email to selected users) but it is only with an hardcoded message, so I’m trying to modify the example.
I managed to send correctly the email with personalized message if I only select one user, but if I select more than one all of them will receive the same message.
This is what I have now:
$emails = array();
while( $data = $button->getNextSelectedRecord() )
{
if( $data["EMAIL_FIELD"] )
$emails[] = $data["EMAIL_FIELD"];
$body = $data["MESSAGE_FIELD"];
}
// send the email
$email = implode(", ", $emails);
$subject = "";
$arr = runner_mail(array('to' => $email, 'subject' => $subject, 'body' => $body));
$result["txt"] = "Email were sent.";
// if error happened print a message on the web page
if( !$arr["mailed"] )
{
$errmsg = "Error happened: <br>";
$errmsg.= "File: " . $arr["errors"][0]["file"] . "<br>";
$errmsg.= "Line: " . $arr["errors"][0]["line"] . "<br>";
$errmsg.= "Description: " . $arr["errors"][0]["description"] . "<br>";
$result["txt"] = $errmsg;
}
What should I change? Thank you.
Problem:
In the foreach loop you are appending every new email address to one variable:
$emails[] = $data["EMAIL_FIELD"];
and you are assigning new value to $body variable (overriding the old one) in each iteration:
$body = $data["MESSAGE_FIELD"];
Therefore when you are calling runner_mail just once outside of the while loop, you will send same body (from the last iteration) to all recipients (you built an array of all recipients using while loop).
Solution:
You need to call runner_mail for each recipient (as the body is different). You can achieve it by moving the call to runner_mail inside the while loop:
while( $data = $button->getNextSelectedRecord() )
{
// Send only if email field not empty
if( $data["EMAIL_FIELD"] ) {
// This no longer should be an array of emails
$email = $data["EMAIL_FIELD"];
$body = $data["MESSAGE_FIELD"];
$subject = "";
// Email will be sent for each record
$arr = runner_mail(array('to' => $email, 'subject' => $subject, 'body' => $body));
}
}

Post in a message on the wall using Graph API?

I am learning Facebook app development and I want my app to post a simple message on the wall of the user.
How do I do it using the graph API?
I am using servlets for the development of app.
Firstly you need to get the access_token of the user (after it makes a login) with 'https://graph.facebook.com/oauth/'.
https://developers.facebook.com/docs/authentication/
Notice that his access_token will be retrieved to your own php or whatelse "&redirect_uri=WWW.YOUR_WEB.PHP" by a $REQUEST['code'] that you'll have to uncode in this way:
$code = $_REQUEST['code'];
$url = "https://graph.facebook.com/oauth/access_token?";
$url .= "client_id=" . $APP_ID;
$url .= "&client_secret=" . $APP_SECRET;
$url .= "&code=" . $code;
$url .= "&redirect_uri=" . $MY_URL;
$c = curl_init();
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_URL, $url);
$response = curl_exec($c);
curl_close($c);
$response = explode("&", $response);
foreach($response as $key => $value)
{
$pair = explode("=", $value);
$response[$pair[0]] = $pair[1];
unset($response[$key]);
}
$access_token = $response['access_token'];
$expires = $response['expires'];
Later to post on a wall you need to call an url in this way:
_url = "https://graph.facebook.com/" + user_id + "/feed?message=MSG_STRING"
_url += "&access_token=" + access_token;
_url += "&name=NAME_STRING";
_url += "&link=LINK_URL";
_url += "&description=DESCRIPTION_STRING";
_url += "&method=post";
https://developers.facebook.com/docs/reference/api/post/
You can use the socialauth library to post a message on FB wall.
http://code.google.com/p/socialauth/
How to post a message on friend's wall, Visit a following link:-
http://code.google.com/p/socialauth/issues/detail?id=233.

Recognizing a character to be Chinese and get Chinese "pinyin" phonetics from simplified characters?

Is it possible to
A. find out if a character is Chinese (simplified) and in that case
B. get the pinyin? example: 你好 => nǐhǎo using java or php?
Cheers
A)
Yes. All characters represented in unicode have a unique numeric index called a codepoint.
If you know the range of codepoints for simplified Chinese and you know how to get the unicode codepoint of a given character, a simple comparison will tell you if the given character is within the simplified Chinese range.
An existing question has a solution for getting the unicode codepoint for a character in PHP:
How to get code point number for a given character in a utf-8 string?
In Java, the static java.lang.Character::codePointAt() method will give you what you need.
B)
Converting a simplified Chinese character, or string, to Pinyin would most likely require some form of map with the unicode code point as the key and the corresponding pinyin as the value.
An example of this in PHP is shown at http://kingphp.com/108.html.
A simple Google search for [java pinyin] reveals a range of options, two of which being Chinese to pinyin libraries at http://kiang.org/jordan/software/pinyinime/ and http://pinyin4j.sourceforge.net/.
Bit late, but solved!
<?php
function curl($url,$params = array(),$is_coockie_set = false)
{
if(!$is_coockie_set){
/* STEP 1. let¡¯s create a cookie file */
$ckfile = tempnam ("/tmp", "CURLCOOKIE");
/* STEP 2. visit the homepage to set the cookie properly */
$ch = curl_init ($url);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($ch);
}
$str = ''; $str_arr= array();
foreach($params as $key => $value)
{
$str_arr[] = urlencode($key)."=".urlencode($value);
}
if(!empty($str_arr))
$str = '?'.implode('&',$str_arr);
/* STEP 3. visit cookiepage.php */
$Url = $url.$str;
$ch = curl_init ($Url);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($ch);
return $output;
}
function Translate($word,$from,$to)
{
$word = urlencode($word);
$url = 'http://translate.google.com/translate_a/t?client=t&text='.$word.'&hl=' . $from . '&sl=' . $from . '&tl=' . $to . '&ie=UTF-8&oe=UTF-8&multires=1&otf=2&pc=1&ssel=0&tsel=0&sc=1';
$name_en = curl($url);
$name_en = explode('"',$name_en);
return $name_en[1];
}
function pinyin($word)
{
$word = urlencode($word);
$url = 'http://translate.google.com/translate_a/t?client=t&text='.$word.'&hl=zh&sl=zh&tl=zh&ie=UTF-8&oe=UTF-8&multires=1&otf=2&pc=1&ssel=0&tsel=0&sc=1';
$name_en = curl($url);
$name_en = explode('"',$name_en);
return str_replace(" ", "", strtolower($name_en[5]));
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
<?php
echo pinyin(urldecode($_GET['phrase']));
?>
</body>
</html>
If you put this at http://www.example.com/foo.php, type in http://www.example.com/foo.php?phrase=你好, and it will give you the pinyin.
Tested, and works.
If you are using utf-8 to interpret your files and calls to the DB, i guess a simple
$new_text = preg_replace(array('/你好/',...), array('nǐhǎo',...), $old_text);
should do the trick.
Where are you getting your string from?

Categories

Resources