0

I have created a automatic mail with some texts,tables and charts. The charts are designed using quickcharts and used following method.

$chartConfig= "{
  type: 'line',
  data: {
    labels: [$date_array2],
    datasets: [{
      data: [$value_array2],
      backgroundColor: ['#F5DEB3'],
    }]
  },
  options: {
    title: {
      display: true,
      text: 'UGW Peak Throughput in Malabe_LD_vUGW, in Gbps',
    },
    legend: {
      display: false    
    }
  }
}";


$url1 = 'https://quickchart.io/chart?w=500&h=200&c=' . urlencode($chartConfig);  


$messagenew.= "Chart :<br><br><img src=\"$url1 \">

I am getting the email correctly. But the issue is I cannot see the chart directly. Every time I need to click download image to see the chart. Further I am using VPN connection to see the mails and I cannot see the images(even though it has downloaded). Without VPN connection I can see the images after downloading.

Can someone propose any better way to get charts attached to emails in PHP?

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
Shehan
  • 417
  • 2
  • 11
  • Images requested from web servers will be blocked by default by many mail clients (because they would allow tracking, when the recipient has opened the mail.) You should really rather embed this image into the mail directly. – CBroe Jan 26 '22 at 14:09
  • @CBroe thank you. Can you provide some working example to do this? How should I do this? – Shehan Jan 26 '22 at 14:32
  • If you are not already sending your mails using a dedicated mailer library, then you should get one first - PHPMailer is a popular choice. How to embed images into a mail using that you can research, that isn't a new topic, and also covered in the library's documentation. – CBroe Jan 26 '22 at 15:22
  • You will need a local copy of the image for that though; but with file_get_contents or cURL you should be able to request the image data from the URL you got there, and store it locally. – CBroe Jan 26 '22 at 15:23

2 Answers2

0

You need to add the image as an attachment and reference it in the HTML body of the email. See https://stackoverflow.com/a/17197140/332059

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
0

Web-hosted images can be blocked by Outlook automatically. Only users can unblock them in Outlook manually. To prevent such cases you can attach the required image and hide it from the view in Outlook by setting the PR_ATTACHMENT_HIDDEN property on the attachment:

Const PR_ATTACHMENT_HIDDEN As String = "http://schemas.microsoft.com/mapi/proptag/0x7FFE000B"

Most probably you also need to set the PR_ATTACH_CONTENT_ID property in the code on the attachments to refer to such images in the message body:

Const PR_ATTACH_CONTENT_ID As String = "http://schemas.microsoft.com/mapi/proptag/0x3712001F"

And then from the message body you may refer to the property value (cid:) set earlier:

attachment = MailItem.Attachments.Add("c:\temp\chart.jpg")
attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F", "YourChart1")
MailItem.HTMLBody = "<html><body>Test image <img src=""cid:YourChart1""></body></html>"

Read more about that in the Distinguish visible and invisible attachments with Outlook VBA thread.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • Thank you.I am using php and i am not using PHPMailer. How then I should implement these? – Shehan Jan 27 '22 at 00:50
  • That code uses the Outlook object model for adding an image and editing the message body. But you can use the same approach in any tool or programming language. – Eugene Astafiev Jan 27 '22 at 22:07