My php script for mailing a form is not working

2019-03-05 02:36发布

I am trying to set up a form that will send attachments along with the email content, but I have no idea what I am doing.

I am completely new to PHP, and just learned to do the basic mail form work by, basically, trial and error, along with tutorials on the internet. But, when it came to attachments. Now I am completely at loss. And, although the PHP script supposedly runs, and the file is uploaded, all the operation stops all of a sudden. Neither do I get the messages that should show when the scripts finishes running nor do I get an email with the test message and its attachment.

Can anyone help me, or at least walk me through so I can understand what's my error? My HTML and PHP are as follows:

HTML (Form and title)

<h3 style="padding-left:290px">Consulta de orçamento</h3>
<form id="form" method="post" action="formulario_orcamento.php" style="padding-left:100px" enctype="multipart/form-data">
    <fieldset>
        <label><input name="Nome" type="text" value="Nome" id="Nome" onBlur="if(this.value=='') this.value='Nome'" onFocus="if(this.value =='Nome' ) this.value=''"></label>
        <label><input name="E-mail" type="text" value="E-mail" id="E-mail" onBlur="if(this.value=='') this.value='E-mail'" onFocus="if(this.value =='E-mail' ) this.value=''">
        </label>
        <label><input name="Telefone" type="text" value="Telefone" id="Telefone" onBlur="if(this.value=='') this.value='Telefone'" onFocus="if(this.value =='Telefone' ) this.value=''"></label>
        <label>
            <select name="Duvidas" id="Duvidas" style="height:20px; width: 623px">
                <option value="Elaboração de questionários">Elaboração de questionários</option>
                <option value="Amostragem">Amostragem</option>
                <option value="Análise exploratória">Análise exploratória</option>
                <option value="Pesquisas online">Pesquisas online</option>
                <option value="Tabulação">Tabulação</option>
                <option value="Análises específicas">Análises específicas</option>
                <option value="Outras Dúvidas">Outras Dúvidas</option>
            </select>
        </label>
        <label><input name="Outras" type="text" value="Outras Dúvidas - Especificar" id="Outras Duvidas" onBlur="if(this.value=='') this.value='Outras Dúvidas - Especificar'" onFocus="if(this.value =='Outras Dúvidas - Especificar' ) this.value=''"></label>
        <label><input name="Arquivos" type="file" style="height:25px"></label>
        <label><textarea name="Mensagem" id="Mensagem" onBlur="if(this.value==''){this.value='Mensagem'}" onFocus="if(this.value=='Mensagem'){this.value=''}">Mensagem</textarea></label>
        <input type="submit" name="Enviar" id="Enviar" value="Enviar" class="button" style="background:#64d0ff; font-size:14px; color:#fff; display:inline-block; padding:6px 20px 5px 20px; box-shadow:0 1px 1px #fff; width:70px; height:35px" onmouseover="this.style.backgroundColor='#1f497d', this.style.color='#fecf06'" onmouseout="this.style.backgroundColor='#64d0ff', this.style.color='#ffffff'">
    </fieldset>
</form>

PHP

<?php
$name      = $_POST['Nome'];
$email     = $_POST['E-mail'];
$telephone = $_POST['Telefone'];
$message   = $_POST['Mensagem'];

if ($_POST['Duvidas'] = "Outras Dúvidas") {
    $question == $_POST['Outras'];
} else {
    $question == $_POST['Duvidas'];
}

$mime_boundary = "==Multipart_Boundary_x" . md5(mt_rand()) . "x";
$tmp_name      = $_FILES['filename']['tmp_name'];
$ftype         = $_FILES['filename']['type'];
$fname         = $_FILES['filename']['name'];
$fsize         = $_FILES['filename']['size'];
if (file_exists($tmp_name)) {
    if (is_uploaded_file($tmp_name)) {
        $file = fopen($tmp_name, 'rb');
        $data = fread($file, filesize($tmp_name));
        fclose($file);
        $data = chunk_split(base64_encode($data));
    }

    $to      = "quick.analytics@2frame.com.br";
    $subject = "Consulta de orçamento";
    $header  = "From: danielle.steffen@2frame.com.br" . "\r\n";
    $header .= "Content-type: multipart/mixed;\r\n";
    $header .= " boundary=\"{$mime_boundary}\"";
    $header .= "MIME-Version: 1.0\r\n";

    $msg = "This is a multi-part message in MIME format.\n\n";
    "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
    "Mensagem enviada em " . date("d/m/Y") . ", os dados seguem abaixo:\n\n" . "Nome: $name\n\n" . "E-mail: $email \n\n" . "Telefone: $telephone \n\n" . "Dúvida: $question \n\n";

    $message .= "--{$mime_boundary}\n";
    "Content-Type: {$ftype};\n" . " name=\"{$fname}\"\n";
    //"Content-Disposition: attachment;\n" .
    //" filename=\"{$fileatt_name}\"\n" .
    "Content-Transfer-Encoding: base64\n\n";
    $data . "\n\n" . "--{$mime_boundary}--\n";

    if (isset($_POST['Enviar'])) {
        $res = mail($to, $subject, $msg, $header);
    }
    if ($res) {
        echo 'Mensagem enviada para ' . $to . '';
    } else {
        echo 'Por favor corrija seus erros.';
    }
}
?>

1条回答
Emotional °昔
2楼-- · 2019-03-05 03:23

You probably have PHP errors that you are not seeing, try adding these lines to the top of your php:

error_reporting(E_ALL);
ini_set('display_errors', true);

This code will enable error reporting, which probably will make you see something..

Also, this line: if($_POST['Duvidas'] = "Outras Dúvidas")

is not comparing $_POST['Duvidas'] with "Outras Dúvidas" but is assigning the value "Outras Dúvidas" to $_POST['Duvidas'], which is always true.. Use if($_POST['Duvidas'] == "Outras Dúvidas") instead (note the ==). This might be cause of your problems.

查看更多
登录 后发表回答