Phpmailer does not attach

Asked

Viewed 103 times

-1

I’ve looked at Stack’s answers, I’ve done it before, but it’s still going wrong. So I’d like you to take a look and maybe find something I can’t see

Is that simply the email sent by Phpmailer does not go with attachment.

Follows codes:

HTML

<!DOCTYPE html>

<html lang="pt">


<head>
    <title>Formulário TIVIT</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="icon" type="image/png" href="images/icons/favicon.ico"/>

    <!--========================================== IMPORTAÇÕES CSS ====================================================-->
    <link rel="stylesheet" type="text/css" href="vendor/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="fonts/font-awesome-4.7.0/css/font-awesome.min.css">
    <link rel="stylesheet" type="text/css" href="vendor/animate/animate.css">
    <link rel="stylesheet" type="text/css" href="vendor/css-hamburgers/hamburgers.min.css">
    <link rel="stylesheet" type="text/css" href="vendor/animsition/css/animsition.min.css">
    <link rel="stylesheet" type="text/css" href="vendor/select2/select2.min.css">
    <link rel="stylesheet" type="text/css" href="vendor/daterangepicker/daterangepicker.css">
    <link rel="stylesheet" type="text/css" href="css/util.css">
    <link rel="stylesheet" type="text/css" href="css/main.css">
    <!--========================================== FIM IMPORTAÇÕES CSS=====================================================-->

</head>



    <!--========================================== IMPORTAÇÕES JS =====================================================-->

    <script type="text/javascript">
        function submitForm(action) {
                var form = document.getElementById('form1');
                form.action = action;
                form.submit();
            }
    </script>
    <!--========================================== FIM IMPORTAÇÕES JS =====================================================-->

<body>
<body>

    <div class="container-contact100">
        <div class="wrap-contact100">
            <form class="contact100-form validate-form" method="POST" action="gravar_enviar.php">
                <span class="contact100-form-title">
                    Demandas TIVIT
                </span>

                <div class="wrap-input100 validate-input" data-validate="Campo Obrigatório">
                    <span class="label-input100">Data</span>
                    <input class="input100" type="text" name="Datas" placeholder="Formato DD/MM/AAAA">
                    <span class="focus-input100"></span>
                </div>

                <div class="wrap-input100 validate-input" data-validate = "Campo Obrigatório">
                    <span class="label-input100">Módulo</span>
                    <input class="input100" type="text" name="Modulo" placeholder="Especifique o módulo">
                    <span class="focus-input100"></span>
                </div>

                <div class="wrap-input100 validate-input" data-validate = "Campo Obrigatório">
                    <span class="label-input100">Assunto / Script</span>
                    <input class="input100" type="text" name="AssuntoScript" placeholder="Especifique qual assunto/script">
                    <span class="focus-input100"></span>
                </div>

                <div class="wrap-input100 validate-input" data-validate = "Campo Obrigatório">
                    <span class="label-input100">Descrição da Alteração</span>
                    <textarea class="input100" name="DescricaoAlteracao" placeholder="Descreva quais foram as modificações"></textarea>
                    <span class="focus-input100"></span>
                </div>

                <div class="wrap-input100 input100-select">
                    <span class="label-input100">Script ou Informação?</span>
                    <div>
                        <select class="selection-2" name="ScriptInformacao">
                            <option>Selecione</option>
                            <option>Script</option>
                            <option>Informação</option>
                        </select>
                    </div>
                    <span class="focus-input100"></span>
                </div>


                <div class="wrap-input100 input100-select">
                    <span class="label-input100">Responsável</span>
                    <div>
                        <select class="selection-2" name="Responsavel">
                            <option>Selecione</option>
                            <option>Adriana</option>
                            <option>Fernanda</option>
                        </select>
                    </div>
                    <span class="focus-input100"></span>
                </div>

                <div class="wrap-input100 input100-select">
                    <span class="label-input100">Tipo</span>
                    <div>
                        <select class="selection-2" name="Tipo">
                            <option>Selecione</option>
                            <option>Alteração</option>
                            <option>Inclusão</option>
                            <option>Exclusão</option>
                            <option>Melhoria</option>
                            <option>Incidente</option>
                            <option>Comunicado</option>
                            <option>Atualização</option>
                            <option>Campanha</option>
                        </select>
                    </div>
                    <span class="focus-input100"></span>
                </div>


                <div class="wrap-input100 input100-select">
                    <span class="label-input100">Origem</span>
                    <div>
                        <select class="selection-2" name="Origem">
                            <option>Selecione</option>
                            <option>Cerat</option>
                            <option>Geate</option>
                            <option>Tivit</option>

                        </select>
                    </div>
                    <span class="focus-input100"></span>
                </div>


            <div class="wrap-input100 input100-select">
                    <span class="label-input100">Anexar</span>
                        <input type="file" name="arquivo" value="Anexar">
                    <span class="focus-input100"></span>
                </div> 


                <div class="container-contact100-form-btn">
                    <div class="wrap-contact100-form-btn">
                        <div class="contact100-form-bgbtn"></div>
                        <button class="contact100-form-btn">
                            <span>
                                Enviar
                                <i class="fa fa-long-arrow-right m-l-7" aria-hidden="true" type="submit"></i>


                            </span>
                        </button>
                    </div>
                </div>


                    <a href="excel_cadop.php"><div class="container-contact100-form-btn">
                    <div class="wrap-contact100-form-btn">
                        <div class="contact100-form-bgbtn"></div>
                        <span class="contact100-form-btn">
                                Gerar Planilha do Mês Atual
                                <i class="fa fa-long-arrow-right m-l-7" aria-hidden="false" "submit 2"></i>


                            </span>
                    </div>
                </div></a>


            </form>
        </div>
    </div>



    <div id="dropDownSelect1"></div>

    <!--========================================== IMPORTAÇÕES JS =====================================================-->

    <script src="vendor/jquery/jquery-3.2.1.min.js"></script>
    <script src="vendor/animsition/js/animsition.min.js"></script>
    <script src="vendor/bootstrap/js/popper.js"></script>
    <script src="vendor/bootstrap/js/bootstrap.min.js"></script>
    <script src="vendor/select2/select2.min.js"></script>
    <script>
        $(".selection-2").select2({
            minimumResultsForSearch: 20,
            dropdownParent: $('#dropDownSelect1')
        });
    </script>
    <script src="vendor/daterangepicker/moment.min.js"></script>
    <script src="vendor/daterangepicker/daterangepicker.js"></script>
    <script src="vendor/countdowntime/countdowntime.js"></script>
    <script src="js/main.js"></script>

    <!--========================================== FIM IMPORTAÇÕES JS =====================================================-->


</body>
</html>

gravar_enviar.php

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>

<body>

<?php

echo $msg = '';
if( $_POST ){

    $servidor = 'localhost';
    $usuario = 'sitedoda_multi';
    $senha = 'xxxxxxxx';
    $banco = 'sitedoda_xxxxxxxxxxx';
    // Conecta-se ao banco de dados MySQL
    $mysqli = new mysqli($servidor, $usuario, $senha, $banco);
    // Caso algo tenha dado errado, exibe uma mensagem de erro
    if (mysqli_connect_errno()) trigger_error(mysqli_connect_error());



    $Datas = $_POST['Datas']; 
    $Modulo = $_POST['Modulo'];
    $AssuntoScript = $_POST['AssuntoScript'];
    $DescricaoAlteracao = $_POST['DescricaoAlteracao'];
    $ScriptInformacao = $_POST['ScriptInformacao'];
    $Responsavel = $_POST['Responsavel']; 
    $Tipo = $_POST['Tipo']; 
    $Origem = $_POST['Origem'];
    $query = "INSERT INTO formulario (Datas, Modulo, AssuntoScript, DescricaoAlteracao, ScriptInformacao, Responsavel, Tipo, Origem)
    VALUES ('".$Datas."', '".$Modulo."', '".$AssuntoScript."', '".$DescricaoAlteracao."', '".$ScriptInformacao."', '".$Responsavel."', '".$Tipo."', '".$Origem."') ";

# $sql = "Update [DB_Daniel].[dbo].[formulario] Set Data = '".$Data."', Modulo = '".$Modulo."', AssuntoScript = '".$AssuntoScript."', DescricaoAlteracao = '".$DescricaoAlteracao."', ScriptInformacao = '".$ScriptInformacao."', Responsavel = '".$Responsavel."', Tipo = '".$Tipo."', Origem = '".$Origem."', Anexos = '".$Anexos."' where id = '".$id."' "; 



    $q = mysqli_query($mysqli,$query);

    if($q){$msg = 'Tudo certo';
} else {
    $msg = 'Deu ruim';
}
}

?>


<!------- ENVIO EMAIL ------------------ !>



<?php


//incluir o phpmailer
require_once("phpMailer/PHPMailerAutoload.php");

//criar instancia do phpmailer
$mail = new PHPMailer();

//criar variável do Anexo
$arquivo = $_FILES['arquivo'];

//habilitar SMTP
$mail->isSMTP();

$mail->SMTPOptions = array (
'ssl' => array(
    'verify_peer'=> false,
    'verify_peer_name' =>false,
    'allow_self_signed' =>true
     )
);

//debug para informar os erros
$mail->SMTPDebug=2; // 0-nada 1-msg cliente e servidor 3-tudo


//Host
$mail->Host = 'smtp.gmail.com';

//tipo de proteção
$mail->SMTPSecure = "tls";

//porta
$mail-> Port = 587;

//autenticação
$mail->SMTPAuth = true;

//detalhes da conta de email
$mail->Username='[email protected]';
$mail->Password='xxxxxxxxx';

//detalhes do email
$mail->setFrom ('[email protected]', 'Site');
$mail->addAddress ('[email protected]');
$mail->Subject = 'Email de teste php mailer';
$mail->AddAttachment($arquivo['tmp_name'], $arquivo['name'] );
$mail->Body = 'Este é um email teste do phpmailer ok!!';

//mensagem de envio ou erro
if ($mail->send())
    echo "email enviado com sucesso";
else echo "Deu ruim";

?>



</body>
</html>

1 answer

0

Kali, thank you for suggesting reading the documentation. I discovered that the enctype="multipart/form-data" in the HTML form. Inserted and worked!

The section was like this:

<form class="contact100-form validate-form" enctype="multipart/form-data" method="POST" action="gravar_enviar.php">
                <span class="contact100-form-title">
                    Demandas TIVIT
                </span>

Thank you very much!!

Browser other questions tagged

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