-2

I have created a chart and I need to add it to my email body. Here I have created a sample bar graph.

$chartConfigArr = array(
    'type' => 'bar',
    'data' => array(
      'labels' => array(2012, 2013, 2014, 2015, 2016),
      'datasets' => array(
        array(
          'label' => 'Users',
          'data' => array(120, 60, 50, 180, 120),
        )
      )
    )
  );

Now I have encoded the url and pass it to a src and attached in the email body($messagebody).

$chartUrl = 'https://quickchart.io/chart?w=500&h=300&c=' . urlencode($chartConfigArr);  
$messagebody.= "Please see the chart below:<br><br><img src=\"$chartUrl\">";

I am not getting any errors, but I cannot see the graph in my email. I can see only a box with a red cross. Can someone show me where I messed the code?

Update: tried with var_dump(). Here is the result.

"

Shehan
  • 417
  • 2
  • 11
  • a box with red cross means your url is not accessible. Try to var_dump($messagebody); and check the src – Aqib Javed Jan 24 '22 at 05:11
  • where to add this? – Shehan Jan 24 '22 at 05:17
  • After $messagebody.= "Please see the chart below:

    "; add var_dump($messagebody); exit(); and execute email function.
    – Aqib Javed Jan 24 '22 at 05:19
  • Please see the chart below:

    " Here is the result what I got. seems the url is not correct. How to overcome this?
    – Shehan Jan 24 '22 at 05:25
  • 1
    Please edit the question with new info, do not add as a comment. Generally, always integrate answers to comments into the question – AD7six Jan 24 '22 at 07:04
  • Note also looking at the source of the email would have made this problem more obvious – AD7six Jan 24 '22 at 07:05
  • Please share more details. Are you sending HTML mails? – Nico Haase Jan 24 '22 at 08:23
  • yes.It is html mails – Shehan Jan 24 '22 at 08:31
  • `Update: tried with var_dump(). Here is the result.` that result is valid html - which doesn't match the originally described problem/question. If your problem is solved, you can write an answer; if it is not please clarify by editing the question what the problem (now?) is. Please note that questions that mutate (question -> answered -> new question ...) are not well received. – AD7six Jan 24 '22 at 09:34
  • @AD7six eventhough it is a valid html the issue is still there. I cannot see the graph. Do I need to edit? – Shehan Jan 24 '22 at 12:05
  • the question is still missing the source for the email you're speaking about - yes you need to edit the question :) – AD7six Jan 26 '22 at 11:23

1 Answers1

1

urlencode expects string to be the argument while you pass an array. This is a fatal error.

Change,

$chartUrl = 'https://quickchart.io/chart?w=500&h=300&c=' . urlencode($chartConfigArr);

to,

$chartUrl = 'https://quickchart.io/chart?w=500&h=300&c=' . urlencode(json_encode($chartConfigArr)); // passing it is as JSON is from the Quickchart documentation

Sarvap Praharanayuthan
  • 4,212
  • 7
  • 47
  • 72
  • `This is a fatal error.` clearly it is not as the code (in the question) continues :) – AD7six Jan 24 '22 at 07:02
  • @AD7six Unsure about how it is used, but that is actually a fatal error. https://prnt.sc/26ier7o You can see the code execution is stopped. `Hello` is not in the output. – Sarvap Praharanayuthan Jan 24 '22 at 07:10
  • You are clearly using a different version of php to the OP. Alternatively see the question `I am not getting any errors, but I cannot see the graph in my email. I can see only a box with a red cross` – AD7six Jan 24 '22 at 07:32
  • When I use this code, now I am getting errors. PHP Fatal error: Uncaught Error: Call to undefined function json_encode() – Shehan Jan 24 '22 at 07:37
  • @AD7six I understood. I am on PHP 8. – Sarvap Praharanayuthan Jan 24 '22 at 08:07
  • @Shehan You need to enable JSON extension. Refer https://stackoverflow.com/questions/18239405/php-fatal-error-call-to-undefined-function-json-decode – Sarvap Praharanayuthan Jan 24 '22 at 08:09
  • Tried. Now code runs. But issue is till there. – Shehan Jan 24 '22 at 08:16
  • var_dump value.No error here. But I cannot see the graph " – Shehan Jan 24 '22 at 08:22
  • @Shehan you have a URL now, that contains `"` characters. So what do you think happens, when you enclose that URL with `"` as attribute delimiters in your HTML now ...? (Plus, we don't even know whether your script is able to work with the value of the `c` parameter encoded as JSON.) – CBroe Jan 24 '22 at 08:25
  • @CBroe can you provide me a working example to try? – Shehan Jan 24 '22 at 08:32
  • If you want to insert JSON-encoded data into a URL, then you best use single quotes for the attribute delimiters in HTML, that causes the least amount of problems. But we still don't know, whether your script is properly set up to accept input in the form of JSON to begin with. – CBroe Jan 24 '22 at 08:35
  • @CBroe $messagenew.= 'Please see the chart below:

    '; . Is this the fomat you are recommending? But now the $charturl is as a text.not as variable(as I can see in the editor)
    – Shehan Jan 24 '22 at 09:07
  • No, that is not what I mean, I am talking about the quotes that delimit the value of the `src` attribute of the image, not those delimiting the outer string. _"But now the $charturl is as a text.not as variable"_ - of course it is, because you used single quotes as delimiters for the string now - inside which, no variables are parsed. And the latter are _basics_ you should rather have read up on, before you come here. https://www.php.net/manual/en/language.types.string.php – CBroe Jan 24 '22 at 09:13
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/241355/discussion-between-shehan-and-cbroe). – Shehan Jan 24 '22 at 12:06