由phpmailer中的API生成的png附件

I'm trying to send a .png image to my user via phpmailer. The image is shown when I use <img> tags, but I want it to display as a real attachment that the user can open/save/print (like in this screenshot). I read that I can use $mail->addStringAttachment for this. So I tried this, and it does send an attachment with the email, but when I try to open it, it says that Windows Picture Viewer can't open the file. Also saving to my computer and then opening with Paint doesn't work, it tells me thats not a valid file or something. I think this is because it's no static image, but an image generated by an API, namely:

$qr = 'http://api.qrserver.com/v1/create-qr-code/?data=' . $guid . '&size=250x250';

So this image should be sent as an attachment. Does anyone know how I can make this work?

The reason it's failing is that you're trying to attach the URL as image data. You need to fetch the data from the URL first, then attach it to something.

Go one step at a time - make sure that you're getting back valid image before trying to email it - e.g.

file_put_contents('qr.png', file_get_contents($qr));

and make sure you get a valid image saved in there. When you know that's working, then try and email it with

$mail->addStringAttachment(file_get_contents($qr) 'qr.png');

Though perhaps with a bit more error checking!

I got it to work fine as an attachment by doing the following:

$qr = file_get_contents("https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=Example");

$mail->addStringAttachment($qr, "qr.png");