Search code examples
phpphpmailertinymce-4responsive-filemanager

Sending HTML emails using PHPMailer with TinyMCE inline images


Please help! I have been trying to figure this out for days and I am unable to. I am trying to send HTML emails using PHPMailer. To include/insert inline images, I use TinyMCE's plugin Responsive FileManager. The email sends successfully and displays HTML but the problem is, the images within TinyMCE Editor are not getting displayed.

Below is a screenshot of the test email I received with an inline-image within TinyMCE Editor.

enter image description here

Below is the form code:

<head>
<script src="../js/tinymce/tinymce.min.js"></script>
<script language="javascript" type="text/javascript">
tinymce.init({ 
    selector:'textarea', 
    menubar: false,
    height: '300px',
    themes: 'modern',
    plugins: [
    'advlist autolink lists link image charmap print preview hr anchor pagebreak',
    'searchreplace wordcount visualblocks visualchars code fullscreen',
    'insertdatetime media nonbreaking save table contextmenu directionality',
    'emoticons template paste textcolor colorpicker textpattern imagetools responsivefilemanager'],
    toolbar: ['undo redo | styleselect | bold italic | forecolor backcolor | imageupload responsivefilemanager',
    'alignleft aligncenter alignright | bullist numlist | outdent indent | table | preview'],     

    external_filemanager_path:'/responsivefilemanager/filemanager/',
    filemanager_title:'Responsive Filemanager',
    external_plugins: { 'filemanager' : '/responsivefilemanager/filemanager/plugin.min.js'}
}); 
</script>
</head>
<body>
    <form action="CascadeFunction.php" method="post" name="cascader" id="cascader" enctype="multipart/form-data">
         <label for="details"><span class="required">*</span>DETAILS</label>
            <textarea name="details" cols="70%" rows="10" id="details"></textarea>
         <input class="btn btn-primary" name="Submit" type="submit" id="submit" value="POST &amp; CASCADE" />
    </form>
</body>

Below is my code to process the email (CascadeFunction.php).

require_once('../PHPMailer_5.2.1/class.phpmailer.php');
$mail = new PHPMailer();
$subject = "CASCADE: Test Subject Cascade"; 
$msgbody = $upload_name;        
$mail->Subject = "$subject";
$mail->IsHTML(true); // This tells the PhPMailer that the messages uses HTML.
$mail->MsgHTML(str_replace(
    array(
        '%Audience%',
        '%Category%',
        '%Title%',
        '%Type%',
        '%quickDescr%',
        '%details%'
    ), 
    array(
        $Audience,
        $Category,
        $Title,
        $Type,
        $QuickDescr,
        $CascadeDetails
    ), file_get_contents('emailtemplates/email.php')), dirname(__FILE__));
$mail->AddEmbeddedImage('images/LogoWhiteBG.png', 'logo');

NOTE: the $mail->AddEmbeddedImage, image displays correctly on emails. Only those images within the TinyMCE editor are displayed as broken.


Solution

  • I found the fix to my issue. Just adding this 1 liner code within tinymce.init, fixes it.

    tinymce.init({ 
        convert_urls : false
    });