png attachment generated by an API in phpmailer - php
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?
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");
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!
Related
Email Tracking using "image" method won't work with Gmail
If I'm not being clear about anything let me know... I'm new to SO. I'm having troubles getting Gmail to approve of my image URL that will activate my PHP script to track when people open my emails. Gmail only allows me to "insert" photos that are specifically image files, such as png or jpg files. I have tried using a file-include mechanism: http://www.example.com/?file=image.png with a two-line PHP script that includes the png file and then writes to a log all the tracking info. However, I have found this doesn't work since I'm technically referencing index.php in the URL. I have also tried http://www.example.com/script.php where script.php would look like this: <img src=image.png> <?php // write "Someone viewed your mail" in logs ?> This doesn't work either because I'm still referencing a PHP file. I'm using the Insert Photo > Web Address feature in Gmail... should I be inserting the file differently to fix my problem? I need to find a way to directly reference an image file on my server but also activate the PHP script. Any ideas? Am I overlooking something obvious?
Figured out I can do this with Thunderbird: Write --> Insert --> HTML In the HTML box: <img src="www.mysite.com/script.php"> Hopes this helps anyone who had my problem
PNG image required to be converted from JSON and email
So I have used a signature pad from another source and the pad works fine. When I submit the form, it saves the image as JSON code and this is useful as I need to store it within a database as well. However upon submission, I need to convert the code to png and email the image. This is the code I use to convert the code to image, $output2 is the JSON code from the signature capture, signature-to-image.php is the file actually converting the image: require_once 'signature-to-image.php'; $img = sigJsonToImage($output2); // Save to file imagepng($img); // Destroy the image in memory when complete - I destroy the image at the very end of the PHP file This code converts the code fine as I have tested it opening a header with just $img in it and the image appears fine. However when I try to add the png image to the body of the email, it just shows up as Resource id#7. I use the line of code below to send the image: $email_message .= "Signature: ".clean_string($img)."\n"; I am assuming I need to either embed the image within the body of the email (which I am unfamiliar with) or I need to send the image as an attachment (which I am also unfamiliar with). Am I correct in thinking this? which option is the better choice or is there a better alternative? So I write in the signature pad, it captures the signature and saves the image in JSON (as far as I am aware, I have zero experience using JSON) This is an example of the output I would receive: [{"lx":46,"ly":49,"mx":46,"my":48},{"lx":46,"ly":48,"mx":46,"my":49},{"lx":46,"ly":51,"mx":46,"my":48},{"lx":46,"ly":55,"mx":46,"my":51},{"lx":46,"ly":62,"mx":46,"my":55},{"lx":47,"ly":69,"mx":46,"my":62},{"lx":52,"ly":94,"mx":47,"my":69},{"lx":54,"ly":105,"mx":52,"my":94},{"lx":56,"ly":114,"mx":54,"my":105},{"lx":58,"ly":126,"mx":56,"my":114},{"lx":59,"ly":133,"mx":58,"my":126},{"lx":60,"ly":136,"mx":59,"my":133},{"lx":60,"ly":140,"mx":60,"my":136},{"lx":88,"ly":25,"mx":88,"my":24},{"lx":88,"ly":24,"mx":88,"my":25},{"lx":88,"ly":28,"mx":88,"my":24},{"lx":89,"ly":33,"mx":88,"my":28},{"lx":90,"ly":39,"mx":89,"my":33},{"lx":91,"ly":43,"mx":90,"my":39},{"lx":93,"ly":60,"mx":91,"my":43},{"lx":94,"ly":69,"mx":93,"my":60},{"lx":96,"ly":79,"mx":94,"my":69},{"lx":98,"ly":94,"mx":96,"my":79},{"lx":99,"ly":102,"mx":98,"my":94},{"lx":100,"ly":114,"mx":99,"my":102},{"lx":100,"ly":117,"mx":100,"my":114},{"lx":100,"ly":118,"mx":100,"my":117},{"lx":100,"ly":120,"mx":100,"my":118},{"lx":28,"ly":116,"mx":28,"my":115},{"lx":28,"ly":115,"mx":28,"my":116},{"lx":28,"ly":114,"mx":28,"my":115},{"lx":30,"ly":113,"mx":28,"my":114},{"lx":32,"ly":112,"mx":30,"my":113},{"lx":36,"ly":110,"mx":32,"my":112},{"lx":57,"ly":104,"mx":36,"my":110},{"lx":67,"ly":100,"mx":57,"my":104},{"lx":91,"ly":92,"mx":67,"my":100},{"lx":121,"ly":75,"mx":91,"my":92},{"lx":127,"ly":72,"mx":121,"my":75},{"lx":129,"ly":72,"mx":127,"my":72},{"lx":130,"ly":72,"mx":129,"my":72},{"lx":130,"ly":73,"mx":130,"my":72},{"lx":196,"ly":48,"mx":196,"my":47},{"lx":196,"ly":47,"mx":196,"my":48},{"lx":198,"ly":45,"mx":196,"my":47},{"lx":200,"ly":43,"mx":198,"my":45},{"lx":204,"ly":42,"mx":200,"my":43},{"lx":209,"ly":41,"mx":204,"my":42},{"lx":219,"ly":40,"mx":209,"my":41},{"lx":232,"ly":39,"mx":219,"my":40},{"lx":238,"ly":39,"mx":232,"my":39},{"lx":244,"ly":39,"mx":238,"my":39},{"lx":257,"ly":38,"mx":244,"my":39},{"lx":261,"ly":38,"mx":257,"my":38},{"lx":263,"ly":38,"mx":261,"my":38},{"lx":223,"ly":42,"mx":223,"my":41},{"lx":223,"ly":41,"mx":223,"my":42},{"lx":223,"ly":44,"mx":223,"my":41},{"lx":223,"ly":45,"mx":223,"my":44},{"lx":223,"ly":50,"mx":223,"my":45},{"lx":223,"ly":52,"mx":223,"my":50},{"lx":223,"ly":56,"mx":223,"my":52},{"lx":223,"ly":75,"mx":223,"my":56},{"lx":223,"ly":84,"mx":223,"my":75},{"lx":223,"ly":91,"mx":223,"my":84},{"lx":223,"ly":110,"mx":223,"my":91},{"lx":223,"ly":119,"mx":223,"my":110},{"lx":223,"ly":123,"mx":223,"my":119},{"lx":223,"ly":127,"mx":223,"my":123},{"lx":222,"ly":129,"mx":223,"my":127},{"lx":221,"ly":129,"mx":222,"my":129},{"lx":219,"ly":130,"mx":221,"my":129},{"lx":217,"ly":130,"mx":219,"my":130},{"lx":211,"ly":131,"mx":217,"my":130},{"lx":201,"ly":131,"mx":211,"my":131},{"lx":192,"ly":132,"mx":201,"my":131},{"lx":181,"ly":132,"mx":192,"my":132},{"lx":177,"ly":132,"mx":181,"my":132},{"lx":173,"ly":132,"mx":177,"my":132},{"lx":163,"ly":132,"mx":173,"my":132},{"lx":161,"ly":132,"mx":163,"my":132},{"lx":159,"ly":132,"mx":161,"my":132},{"lx":161,"ly":132,"mx":159,"my":132},{"lx":166,"ly":131,"mx":161,"my":132},{"lx":172,"ly":130,"mx":166,"my":131},{"lx":176,"ly":130,"mx":172,"my":130},{"lx":192,"ly":130,"mx":176,"my":130},{"lx":203,"ly":130,"mx":192,"my":130},{"lx":211,"ly":130,"mx":203,"my":130},{"lx":219,"ly":130,"mx":211,"my":130},{"lx":222,"ly":130,"mx":219,"my":130},{"lx":228,"ly":130,"mx":222,"my":130},{"lx":232,"ly":130,"mx":228,"my":130},{"lx":235,"ly":130,"mx":232,"my":130},{"lx":240,"ly":130,"mx":235,"my":130},{"lx":241,"ly":130,"mx":240,"my":130},{"lx":242,"ly":130,"mx":241,"my":130},{"lx":243,"ly":130,"mx":242,"my":130},{"lx":246,"ly":130,"mx":243,"my":130},{"lx":250,"ly":129,"mx":246,"my":130},{"lx":261,"ly":128,"mx":250,"my":129},{"lx":265,"ly":128,"mx":261,"my":128},{"lx":267,"ly":127,"mx":265,"my":128},{"lx":269,"ly":127,"mx":267,"my":127}]
Can I use PHP in a email signature?
I have a question. I would like to add rotating links inside my email signature to track results on my site. I can make these dynamic tracking urls on google as you may know but I would like to rotate them inside my email signature to see which text draws the most conversions or returning visitors. Is this possible? I found this for instance: $mybanners[1] = '<img src="banner1.jpg">'; $mybanners[2] = '<img src="banner1.jpg">'; $id = rand(1,2); echo $mybanners[$id]; But when I look into my windows live mail I can only upload html files. Does someone know how to do this?
You can't provide PHP scripts in a mail, since it is a server-side language and it will be opened by a mail client. Even Javascript is very often blocked for security reason. What you can do is make a "fake" image which will be in fact generate by a PHP script. YOu can find inspiration by looking to script made for forum avatar rotation. The idea is to generate an image, which will be displayed to the client but, in the same time, save some data about the user who requests the image if you want: <?php // Save whatever you want about the user file_put_content("log/user.txt", $_SERVER['HTTP_REFERER']); // Render a valid PNG image header('Content-Type: image/png'); readfile("/path/to/banner.png"); ?> This script should be used as a standard image (with, if you want, a nice URL rewrite to make a .png link): <img src="http://www.example.com/my_super_banner.php" /> where my_super_banner.php is the script described before.
How to paste image in chrome, then upload it to a server with PHP
I actually want to upload an image to a server. To achieve this, i want the user just paste the image into chrome (the image is a print screen in fact), and then i post the stream to a php page, convert the stream as an image, and then upload it. How can i achieve this web application ? Today i have develop some differents parts : I used this script, and i create the Upload.php page which gets the post variable and try to Create and image. The problem i have, is that when i post the data, i only get a blob. I would like to get a base64 stream. Can you help me ? Thanks in advance.
I'm not sure why you are specifically looking for a "base 64 stream". If you are sending the Blob to your server via ajax, as far as your server is concerned, it's a file. Treat it no different than any other upload server-side. A Blob is a File without a name property. That's perhaps a bit overly-simplistic, but my point is that, again, this is really nothing more than a file as far as your server knows. Assuming you are sending a multipart-encoded request, I'd like to point out that most user agents will set the filename property of the item's Content-Disposition header in the request to "blob" when the item you are uploading is a Blob instead of a file. It is possible to change this value in some browsers via the 3rd argument in FormData's append method, but I wouldn't rely on this just yet. Also note that, if you are interested in a library that handles all of this already, I maintain, Fine Uploader which natively supports uploading images via paste in Chrome.
To answer this old question: Posting an image from clipboard with chrome is pretty much the same as posting a dropped file - except that the image/blob doesn't have the properties "name" and "lastModified". var entry = items[i].webkitGetAsEntry(); if (!entry) entry = items[i].getAsFile(); if (entry instanceof Blob) /** CHROME pastet Bilder als Blob **/ { entry.isFile = true; entry.lastModifiedDate = new Date(); entry.name = ""+new Date().getTime()+"."+entry.type.split('/')[1]; } if (entry.isFile) { //handle dropped file }
How do I load an image in PHP
I want code that loads an image to a PHP server and then send it to browser. For example I want sample.php to send an image to browser once it is requested. in other words, I want to create a PHP file that acts like a proxy for an image.
why are you doing this? why don't deliver the image directly? if you are trying to display a random image you may as well just redirect to the image using header("Location: address-of-image"); for delivering the file to your clients from your server and not from its original location you can just do. however your php.ini settings need to allow external file opens readfile("http://www.example.com/image.jpg") correct headers are not required if you are going to display the image in an img tag, altough i would recommend it. you should check the filetype of the image or in most cases just set an octet-stream header so the browser doesnt assume an incorrect type like text or something and tries to display binary data. to do so just do header("Content-type: application/octet-stream") one more thing to consider may be setting correct headers for caching...
You need to use $image = fopen("image.png"); Modify the headers(not sure exacly if it's correct) headers("Content-type: image/png"); And then send the image echo fread($image, file_size("image.png"));