Jump to content

Attaching a PHP generated image to an email


Plain Flailing

Recommended Posts

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.