Image I insert into the email body using Tinymce does not appear in the email

Asked

Viewed 50 times

0

I’m trying to send an email with one or more images I uploaded using the tinymce textarea, where I write the email to be sent, I write what I want and upload the image, it appears there but does not send in the email, when I look at the e-sent mail only appears what was typed and not the image

Code of the function that sends the e-mail:

function send_mail($con){
    if(isset($_POST['env']) && $_POST['env'] == "email"){


        $destinos= explode(',',$_POST['destinos']);


        $mail = new PHPMailer();
        $mail->Host = 'smtp.gmail.com';
        $mail->isSMTP();
        $mail->SMTPAuth = true;
        $mail->SMTPSecure = 'ssl';
        $mail->Username = '[email protected]';
        $mail->Password = 'senha';
        $mail->Port = 465;
        #$mail->Port = 587;

        for($i=0; $i<count($destinos); $i++){

        $mail->setFrom('[email protected]', 'Stephanto');
        $mail->addAddress($destinos[$i]);
        $mail->addReplyTo('[email protected]');

        $mail->isHTML(true);
        $mail->Subject = $_POST['assunto'];
        $mail->Body = $_POST['mensagem'];

    }

        if(!$mail->send()){
            echo "<div class='alert alert-danger'>Erro ao enviar o E-mail! </div><br>";
            echo "Erro: ".$mail->ErrorInfo;
    }
}

E-mail sending page code:

<script src="https://cloud.tinymce.com/5/tinymce.min.js"></script>

  <script>
tinymce.init({
    selector: 'textarea',
    plugins: 'image code',
    toolbar: 'undo redo | image code',


    images_upload_url: '../lib/upload.php',


    images_upload_handler: function (blobInfo, success, failure) {
        var xhr, formData;

        xhr = new XMLHttpRequest();
        xhr.withCredentials = false;
        xhr.open('POST', '../lib/upload.php');

        xhr.onload = function() {
            var json;

            if (xhr.status != 200) {
                failure('HTTP Error: ' + xhr.status);
                return;
            }

            json = JSON.parse(xhr.responseText);

            if (!json || typeof json.location != 'string') {
                failure('Invalid JSON: ' + xhr.responseText);
                return;
            }

            success(json.location);
        };

        formData = new FormData();
        formData.append('file', blobInfo.blob(), blobInfo.filename());

        xhr.send(formData);
    },
});
</script>



<form method="POST" enctype="multipart/form-data">
    <label>Destinatários</label>
    <input type="text" name="destinos" class="form-control" value="<?php get_emails($con); ?>"><br>

    <label>Assunto</label>
    <input type="text" name="assunto" class="form-control"><br>

    <label>Mensagem</label>
    <textarea name="mensagem" rows="20"></textarea><br>

    <p align="right"><input type="submit" name="" value="Enviar e-mail" class="btn btn-outline-success btn-lg btn-block"></p>
    <input type="hidden" name="env" value="email">
</form>
        <p align="left"><button class="btn btn-outline-primary btn-lg btn-block" onclick="window.location.href='index.php?pagina=inicio'">Cadastrar e-mails</button></p>

</div>

<?php send_mail($con); ?>

Code of upload.php

<?php

$accepted_origins = array("http://localhost", "http://107.161.82.130", "http://codexworld.com");


$imageFolder = "../imagens/";

reset($_FILES);
$temp = current($_FILES);
if(is_uploaded_file($temp['tmp_name'])){
    if(isset($_SERVER['HTTP_ORIGIN'])){

        if(in_array($_SERVER['HTTP_ORIGIN'], $accepted_origins)){
            header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']);
        }else{
            header("HTTP/1.1 403 Origin Denied");
            return;
        }
    }


    if(preg_match("/([^\w\s\d\-_~,;:\[\]\(\).])|([\.]{2,})/", $temp['name'])){
        header("HTTP/1.1 400 Invalid file name.");
        return;
    }


    if(!in_array(strtolower(pathinfo($temp['name'], PATHINFO_EXTENSION)), array("gif", "jpg", "png"))){
        header("HTTP/1.1 400 Invalid extension.");
        return;
    }


    $filetowrite = $imageFolder . $temp['name'];
    move_uploaded_file($temp['tmp_name'], $filetowrite);


    echo json_encode(array('location' => $filetowrite));
} else {

    header("HTTP/1.1 500 Server Error");
}
?>
  • I’ve had this problem, and it was because my image was not hosted on the server, in case you are doing location could be this problem, I decided to put my image on the server site, and then put the link to it.

  • @Can Larissasilva do this using localhost? It’s only for a school project not something you need to host

  • Local I’ve never tried, but since it’s an academic project, one idea that you can try and do and see if it works is to leave this image in the cloud, with sharing for everyone, so you take the link from it and put it in your project. I’ve never tried to do it like this, but it may be for certain

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.