PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, July 7, 2022

[FIXED] What is the most common way to send email that contains an image from a database via phpMailer?

 July 07, 2022     php, phpmailer     No comments   

Issue

I am able to send an html email template using phpMailer, as well as using the addEmbeddedImage() to use a static img in the html template.

phpMailer instance

...
$w_mail->addEmbeddedImage(__DIR__ . '/html_templates/noimage2.png', 'noImage');

in the html template file:

<html>
...
<td style='padding: 0 20px 20px 0;'>
      <img src='cid:image' alt="user photo" />
</td>
...
</html>

This static image in a local file (html_templates/noimage2.png) shows up in the email without a problem.

I want to convert this static image and use an image from the db. The goal is to use the logged-in user's icon as the image.

This image data is stored in session variable as: (this is the raw image that's uploaded, as base64 in the db)

$image_data = $_SESSION['icon_data'];

but I'm not sure which phpMailer method I can use to convert the current static setup to a bit more dynamic. Static, I mean that the addEmbeddedImage takes the actual file directory.

Is there a best-method to using an image from a db for email template via phpMailer?

thank you in advance.

EDITED:

this is the phpMailer instance currently trying to use the addStringAttachment and addStringEmbeddedImage methods. the html is the same as above:

function f_common_send_mail($i_to_addresses, $i_subject = "", $i_body = ""){
        
        $image_data = $_SESSION['icon_data'];

        $w_mail = new PHPMailer(true);

        try {
            $w_mail->CharSet        = MAIL_CHARA;               // Mail character set
            $w_mail->Encoding       = MAIL_ENCODIN;             // Mail encoding

            // Server Settings
            $w_mail->isSMTP();                                  // Set mailer to use SMTP
            $w_mail->Host           = SMTP_HOST;                // Specify main and backup SMTP servers
            $w_mail->SMTPAuth       = SMTP_AUTH;                // Enable SMTP authentication
            $w_mail->Username       = SMTP_USERNAME;            // SMTP username
            $w_mail->Password       = SMTP_PASSWORD;            // SMTP password
            $w_mail->SMTPSecure     = SMTP_ENCRPT;              // Enable TLS encryption
            $w_mail->Port           = SMTP_PORT;                // TCP port to connect to
            
            // Mail Settings
            $w_mail->addAddress($i_to_addresses);               // To address
            $w_mail->setFrom(MAIL_ADDRESS);                     // From address
            $w_mail->Subject = $i_subject;                      // Subject
            $w_mail->isHTML(true);                              // Email format to HTML

            $w_email_body = '';
            if($i_body == ""){
                $w_email_body = " ";                            // Email body(Because string empty is Error)
            }else{
                $w_email_body = $i_body;                        // Email body
            }
            // $w_email_body .= c_email_footer;

            $w_mail->Body = $w_email_body;

            // embedding image ('Logo' is the identifier inside the html file's img tag)
            $w_mail->addEmbeddedImage(__DIR__ . '/html_templates/CARTRIDGE_Logo_R.png', 'Logo');

            // $w_mail->addEmbeddedImage(__DIR__ . '/html_templates/noimage2.png', 'noImage');
            
            $w_mail->addStringAttachment($image_data, 'image.png', PHPMailer::ENCODING_BASE64, 'image/png');
            $w_mail->addStringEmbeddedImage($image_data, 'image', 'image.png', PHPMailer::ENCODING_BASE64, 'image/png');

            if($w_mail->send()) {
                // Email sent successfully
                $w_mail = null;
                return true;
            }else{
                // Failed to send email.
                $w_mail = null;
                return false;
            }
        }
        catch (Exception $e) {
            // Failed to send email.
            $w_mail = null;
            return false;
        }
    }

Solution

The solution that worked for me using an image that was encoded as base64.

$image_data = explode(',', $_SESSION['icon_data']);
        $image_data = base64_decode($image_data[1]);
$icon = $image_data;

Before, I just tried to decode the whole column cell; I didn't realize there was a comma in the data that I needed to split using explode, and only use the second index which was the actual base64 data.

from here, I just used the addStringEmbeddedImage:

$w_mail->addStringEmbeddedImage($icon, 'img', '', 'base64', "image/png");

Works for now. Thank you all for the feedback and the help



Answered By - funtkungus
Answer Checked By - Marie Seifert (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing