Plain Flailing Posted August 12, 2012 Share Posted August 12, 2012 Using imagecreate() I'm generating a temporary image unique to each user (the result is stored in a variable called $my_img), which I then want to send the user as a PNG attachment in a plain email without having to store the image as a file on my server. I'm reasonably familiar with the mail() function but creating attachments out of system generated images doesn't seem to be widely documented. Unfortunately the answers I've found addressing this on Google have been too vague for me to grasp so I'd really appreciate some straightforward advice on how to do this. Many thanks! Quote Link to comment https://forums.phpfreaks.com/topic/266986-attaching-a-php-generated-image-to-an-email/ Share on other sites More sharing options...
Christian F. Posted August 12, 2012 Share Posted August 12, 2012 I recommend using an existing mailer class to help you do this. PHPMailer have had a couple of favourable mentions here, so taking a look at it should provide helpful. In short what you need to do is to add the picture to the mail as a MIME-encoded attachment. To do this you can either use a class, or read up on the mail RFC. The former is the easiest one by far. Quote Link to comment https://forums.phpfreaks.com/topic/266986-attaching-a-php-generated-image-to-an-email/#findComment-1368859 Share on other sites More sharing options...
Plain Flailing Posted August 17, 2012 Author Share Posted August 17, 2012 Thanks for the reply ChristianF. Looking through the PHPMailer documentation, the closest thing seems to be this feature: String Attachments String attachments have been supported since PHPMailer version 1.29. This method works much like AddAttachment(), and is called with AddStringAttachment($string,$filename,$encoding,$type). The string data is passed to the method with the first parameter, $string. Because the string will become a standard file (which is what the attachment will be when received via e-mail), the $filename parameter is required. It's used to provide that filename for the string data. The rest is just the same as described in detail above. So, why use AddStringAttachment instead of AddAttachment? Is it for text-only files? No, not at all. It's primarily for databases. Data stored in a database is always stored as a string (or perhaps, in the case of binary data, as as a BLOB: Binary Large OBject). You could query your database for an image stored as a BLOG and pass the resulting string to the AddStringAttachment. If this can in fact be used with images, then does anyone know how I'd convert the output of PHP's imagecreate() function into a compatible, correctly formatted string to be input into PHPMailer's AddStringAttachment() function? I should point out, if it's not already apparent, that my skills and knowledge are not great, so please forgive my need for a little hand holding! Quote Link to comment https://forums.phpfreaks.com/topic/266986-attaching-a-php-generated-image-to-an-email/#findComment-1370329 Share on other sites More sharing options...
Christian F. Posted August 17, 2012 Share Posted August 17, 2012 ...Is it for text-only files? No, not at all... Seems so, yes. As for what you need to do, read the text that follows what I quoted above. Quote Link to comment https://forums.phpfreaks.com/topic/266986-attaching-a-php-generated-image-to-an-email/#findComment-1370351 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.