代码之家  ›  专栏  ›  技术社区  ›  Tareq Jami

phpmailer没有发邮件,我找不到问题

  •  0
  • Tareq Jami  · 技术社区  · 6 年前

    所以我用phpmailer做了一个联系表格。 我做得很好,我知道。其他人可以通过某种方式发送电子邮件。我不使用表单(不提交)就通过加载页面来发送电子邮件。

    但是我想将表单中的数据发送给我,但是它不适用于提交…

    这是我的代码,只需加载页面的自动故障现在在注释中。

        <?php
    $msg = "";
    
    if (isset($_POST['submit'])) {
        require 'phpmailer/PHPMailerAutoload.php';
    
        function sendemail($to, $from, $fromName, $body, $attachment) {
            $mail = new PHPMailer();
            $mail->addAddress($to);
            $mail->setFrom($from, $fromName);
            $mail->Subject = "Test email!";
            $mail->isHTML(false);
            $mail->Body = $body;
            $mail->addAttachment($attachment);
    
            return $mail->send();
        }
    
        $name = $_POST['vorname'] + ' ' + $_POST['nachname'];
        $email = $_POST['email'];
        $body = $_POST['address'] + ', ' + $_POST['plz'] + ', ' + $_POST['ort'];
    
        $file = "attachment/" . basename($_FILES['attachment']['name']);
    
        if (move_uploaded_file($_FILES['attachment']['tmp_name'], $file)) {
            if (sendemail('tareq@thejami.com', $email, $name, $body, $file))
                $msg = "email sent";
            else
                $msg = "failed";
            } else
                $msg= "fail";
    }
        /*
        //we need to create an instance of PHPMailer
        $mail = new PHPMailer();
    
        //set where we are sending email
        $mail->addAddress('tareq@thejami.com', 'testme');
    
        //set who is sending an email
        $mail->setFrom('myappkl@gmail.com', 'Admin at CPI');
    
        //set subject
        $mail->Subject = "Test email!";
    
        //type of email
        $mail->isHTML(true);
    
        //write email
        $mail->Body = "<p>this is our email body</p><br><br><a href='http://google.com'>Google</a>";
    
        //include attachment
        $mail->addAttachment('fbcover.png', 'Facebook cover.png');
    
        //send an email
        if (!$mail->send())
        echo "Something wrong happened!";
        else
        echo "Mail sent";
        */
    ?>
    
    <html lang="de">
        <body>
            <form method="post" action="index.php" enctype="multipart/form-data" class="formular">
            <h2>Bestellformular</h2>
            Name:<span class="required">*</span><br>
            <input class="texte" style="width:170px;" required type="text" name="vorname" placeholder="Vorname"><input class="texte" style="width:170px;" required type="text" name="nachname" placeholder="Nachname"><br>
            Email:<span class="required">*</span><br>
            <input class="texte" required style="width:350px;" type="email" name="email" placeholder="Email"><br>
            Adresse:<span class="required">*</span><br>
            <input class="texte" required style="width:350px;" type="text" name="address" placeholder="Straße, HausNr."><br>
            <input class="texte" required style="width:170px;" type="number" name="plz" placeholder="PLZ"><input class="texte" required style="width:170px;" type="text" name="ort" placeholder="Ort"><br>
            Handy:<br>
            <input class="texte" style="width:350px;" type="number" name="nummer" placeholder="Handynummer <optional>"><br>
            Bild aussuchen:<span class="required">*</span><br>
            <input required class="texte" style="width:350px;" type="file" name="attachment"><br>
            <input class="button" style="width:350px;" type="submit" value="bestellen"><br>
            </form><br>
            <?php echo $msg; ?>
        </body>
    </html>
    
    1 回复  |  直到 6 年前
        1
  •  3
  •   wodka    6 年前

    你在检查 $_POST['submit'] 但你的表格不包含它。

    为了让它发挥作用,你必须添加 name="submit" 提交按钮:

    <input class="button" style="width:350px;" name="submit" type="submit" value="bestellen">