This topic is locked
[SOLVED]

 Embed Image In Email

4/19/2015 10:40:03 PM
PHPRunner General questions
lefty author

Issue with emailing selected records . I get all data except image . Using in root URL to get image .

Email does not add embedded image. This is not an attachment question.
Here is the code
// Put your code here.

$body = "";
while($data = $button->getNextSelectedRecord())
{
$body.= "Date: ".$data["flddate"]. "
". "\n";
//This is the line below where the issue is Just get http://www.mywebsite.com/photos/1

// Don't know where the integer 1 is coming from? Cannot grab the database name after Photos/.

// This must be a path problem but if I actually add the photo url after Photos/test.jpg // I get same error
$body.= "<img src=http://www.mywebsite.com/photos/".$data["photo_upload"]."/>"; . "\n";
$body.= "Merchandiser: ".$data["Employee"]. "
". "\n";

$body.= "Customer: ". $data["custname"]. "
" ."\n";

$body.= "Town: ". $data["cust_town"]. "
" ."\n";

$body.= "Brand ".$data["brand"]. "
" ."\n";

$body.= "Cases " . $data["cases"]. "
" ."\n";

$body.= "Supplier AR " . $data["supplierar"]. " / Purchasing AR" . $data["vendorar"]. "
". "\n";

$body.= "Bill Back $ " . $data["bill_amt"]. "
" ."\n";

$body.= "\n------------------------------------------

\n\n";
}
//send email
$email = $_SESSION["fldemail"];
$subject = "Merchandiser Display Photos";
runner_mail(array('to' => $email, 'subject' => $subject, 'htmlbody' => $body));
$result["txt"] = "Emails were sent.";
// if error happened print a message on the web page
if( !$arr["mailed"] )
{
$errmsg = "Error happened:
";
$errmsg.= "File: " . $arr["errors"][0]["file"] . "
";
$errmsg.= "Line: " . $arr["errors"][0]["line"] . "
";
$errmsg.= "Description: " . $arr["errors"][0]["description"] . "
";
$result["txt"] = $errmsg;
}
//Anyone see anyting here?

Sergey Kornilov admin 4/20/2015

Make sure that image URL is correct. Print HTML code of this email on the page instead of sending it or send it as plain text so you can see the exact HTML code and find what is wrong.

lefty author 4/21/2015

Yes I have already tried that . Just need to view source code in email .
Returns :

Date: 2015-04-07
<img src=http://www.mywebsite.com/photos/1/>

Merchandiser: John Doe
Customer: Test Customer 2
Town: Shelton
Brand TAKEN RED
Cases 33
Supplier AR 0000001 / Purchasing AR0000001
Bill Back $ 49.5000
Not sure where the 1 is coming from in image URL. I have used this in ASP with no issues.

The only difference I see is the image field in database holding JSON string as file name instead of for example image1_xxxxxx.jpg . Even though I have commented out renaming files for testing. Seems this is where the issue is.
Does multiple upload set image to a JSON string automatically in the database in PHP 8.0 ? upload is set to 1
Note I am doing this in a button on the list page.

Sergey Kornilov admin 4/22/2015

Yes, by default all upload fields are stored in JSON format unless you choose 'Basic upload' option.
Check this article:

http://xlinesoft.com/phprunner/docs/rename_uploaded_file.htm
It explains how to extract the name of the file from JSON.
PS. There is also an error in your code, remove the trailing slash from image URL.

lefty author 4/22/2015



Yes, by default all upload fields are stored in JSON format unless you choose 'Basic upload' option.
Check this article:

http://xlinesoft.com/phprunner/docs/rename_uploaded_file.htm
It explains how to extract the name of the file from JSON.
PS. There is also an error in your code, remove the trailing slash from image URL.


Thanks for the update but I already use this in before add and before update event to rename files. I now need to do the opposite . I need to convert the string back to an image temporarily and email it in URL.

This is used in a button on the list page so the string is already stored in the database and new filename is already stored in the directory.
I suppose I need to Decode all the records one at a time after getting each selected record. How do I grab the image from the directory if my data["photo_upload"] field is already in JSON format. I don't see an explanation in the documentation on how to extract the image name only to rename it? Also where would I enter this code if I did have it ? In the On Server Tab?

Sergey Kornilov admin 4/23/2015

Yes, this need to be done on Server tab.
That code example does contain info on image name extraction. The following line of code is the one that renames the image:

$fileArray[$i]["usrName"] = strtolower($fileArray[$i]["usrName"]);


Do not rename it, just use $fileArray[$i]["usrName"] to do something else. This is your file name.

lefty author 4/25/2015



Yes, this need to be done on Server tab.
That code example does contain info on image name extraction. The following line of code is the one that renames the image:

$fileArray[$i]["usrName"] = strtolower($fileArray[$i]["usrName"]);


Do not rename it, just use $fileArray[$i]["usrName"] to do something else. This is your file name.


The Something Else is where the problem is .I need to get the 'usrName': PATH TO IMAGE in the email body .

In ASP when using Compatability mode or basic upload it was easy just add data("photo_upload") in my url and the image was embedded.

In new version of PHP the image uses mfhandler for URL and I haven't been able to either decode the image back or use mfhandler url to get image embedded in email . I can use properties of the mfhandler image in typing url in browser and wow it comes up . That's good for testing path . If I add in php html email the url instead of the actual image name like ". data["photoupload"] after decoding nothing comes up. So I am missing something here. This seems to apply for PHP runner 8.0 or down to 7.2 . So I tried changing the pre version in project to 1 so compatability mode comes back which is great as I can add timestamp also; and guess what wrong? the new version 8.0 Still uses mfhandler as image path. old versions used just the image name and was easy to parse. So I am stuck big time. 11 Hours on this. Looked at Stackflow.com and see some promising ideas but PHPrunner functions don't act this way.
It appears this is very difficult to do in a button and email . I have tried numerous ways to extract usrName from JSON in button but to no avail. Just need to know how to pass just the 'usrName' JSON value to my data field. There are many examples on "stackoverflow.com" but because the source code in PHPrunner has it's own functions I can't seem to find an answer.
My email results are as follows : Source Code : The image is available as In testing just typed url in browser with usrName: value and image comes up.
But NOT in Email
Test 1
// Throws the integer 1 as the value of the field photo_upload

<img height=300 width=300 src=http://www.myweb.com/photos/1
// same as above_

<img height=300 width=300 src=https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=77180&image=1&table=forumreplies,!:sStrEnd"}]>; // this is the JSON Data Stored In The database_

Merchandiser: John Doe
Town: Shelton
Brand YAGO SANGRIA
Cases 55
Supplier AR 900003751 / Purchasing AR300983
Bill Back $ 82.5000


CODE:
$htmlbody = "";
while($data = $button->getNextSelectedRecord())
{
$fileArray = my_json_decode($data["photo_upload"]);
$fileArray[$i]["usrName"] // Stuck here how to assign 'usrName' part of JSON String to $data["photo_upload"] // Tried $data["photo_upload"] = $fileArray[$i]["usrName"] // does not even send the email
$htmlbody.= "Test ".$data["photo_upload"]."
" . "\n";

$htmlbody.= "<img height=300 width=300 src=http://www.myweb.com/photos/"'>http://www.myweb.com/photos/";. $data["photo_upload"]."
" . "\n";

$htmlbody.= "<img height=300 width=300 src=http://www.myweb.com/photos/"'>http://www.myweb.com/photos/";. $data["photoname"] .">" . "\n"; // This is for testing to see JSON_

$htmlbody.= "Merchandiser: ".$data["Employee"]. "
". "\n";

$hmlbody.= "Customer: ". $data["custname"]. "
" ."\n";

$htmlbody.= "Town: ". $data["cust_town"]. "
" ."\n";

$htmlbody.= "Brand ".$data["brand"]. "
" ."\n";

$htmlbody.= "Cases " . $data["cases"]. "
" ."\n";

$htmlbody.= "Supplier AR " . $data["supplierar"]. " / Purchasing AR" . $data["vendorar"]. "
". "\n";

$htmlbody.= "Bill Back $ " . $data["bill_amt"]. "
" ."\n";

$htmlbody.= "\n------------------------------------------

\n\n";
}
//send email
$email = $_SESSION["fldemail"];
$subject = "Merchandiser Display Photos";
runner_mail(array('to' => $email, 'subject' => $subject, 'htmlbody' => $htmlbody ));
if( !$arr["mailed"] )
{
$errmsg = "Error happened:
";
$errmsg.= "File: " . $arr["errors"][0]["file"] . "
";
$errmsg.= "Line: " . $arr["errors"][0]["line"] . "
";
$errmsg.= "Description: " . $arr["errors"][0]["description"] . "
";
$result["txt"] = $errmsg;
}

Sergey Kornilov admin 4/27/2015

A few things come to mind when I see this.

  1. It's not clear why you need to fields to store uploaded file info. "photo_name" and "photo_upload". You normally need just one field. In compatibility mode it only stores the file name. In regular mode it stores JSON with single or multiple files info.
  2. JSON may contain several files info. This is why in supplied example (http://xlinesoft.com/phprunner/docs/rename_uploaded_file.htm) we use a loop go through all files that might be stored in that field. If you use $fileArray[$i]["usrName"] outside of the loop it won't work because variable $i is not defined.
  3. > "source code in PHPrunner has it's own functions I can't seem to find an answer"



This is very wrong. This is just a PHP code and you can use any sample PHP code to extract values from JSON format and do what you need to do with it. You also don't even need to use PHPRunner's functions to send the email. The only PHPRunner-specific piece of code here is the loop through all selected records and you got that par right.

while($data = $button->getNextSelectedRecord())

{

...

}


What you need to do is to troubleshoot your PHP code and the common practice is to print everything on the web page i.e.

  • got the value with JSON string - print it
  • extracted file name from JSON - print it
  • built the image URL - print it
    Do it step by step to find what exactly doesn't work and fix it.