Phpmailer我没有得到任何附件,但我收到邮件

时间:2018-02-22 05:29:30

标签: php html phpmailer

这是申请签证的表格。 代码中的一切都运行良好。上传文件也为用户和管理员获取邮件。但是对于管理员邮件文件没有附在邮件中。

下面是我的PHP代码。 (代码不仅仅是初学者的优化方式)

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

if (isset ($_POST['submit']) && ($_POST['submit']!='')) {
    $email      = $_POST['email'];
    $name       = $_POST['name'];
    $first_name = $string = preg_replace('/\s+/', '', $name);
    $phone      = $_POST['phone'];
     // file upload for passport document
    if (isset($_FILES["passport"]["name"]) && $_FILES["passport"]["name"] !='') {
        $file_name     = $_FILES["passport"]["name"];
        $target_dir    = "documents/";
        $imageFileType = pathinfo($file_name,PATHINFO_EXTENSION);
        $target_file   = $target_dir .$first_name.'passport'. time().'.'.$imageFileType;
        $passportnme   = $first_name.'passport'.time().'.'.$imageFileType;

        if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
            &&  $imageFileType != "pdf" && $imageFileType != "docx" && $imageFileType != "doc") {
            $passporterror = "Sorry, only JPG, JPEG, PNG, PDF & Docx files are allowed.";
        } else if ($_FILES["passport"]["size"] > 500000) {
            $passporterror = "Sorry, your file is too large.";
        } else {
            if (move_uploaded_file($_FILES["passport"]["tmp_name"], $target_file)) {
                $success       = "The file ". basename( $_FILES["passport"]["name"]). " has been uploaded.";
                $passporterror = "";
            } else {
                $passporterror = "Sorry, there was an error uploading your file.";
            }
        }
    } else {
        $passporterror = "Please choose file";
    }
     // file upload for photo document
    if (isset($_FILES["photo"]["name"]) && $_FILES["photo"]["name"] !='') {
        $file_name2     = $_FILES["photo"]["name"];
        $target_dir2    = "documents/";
        $imageFileType2 = pathinfo($file_name2,PATHINFO_EXTENSION);
        $target_file2   = $target_dir2 .$first_name.'photo'. time().'.'.$imageFileType2;
        $photonme       = $first_name.'photo'.time().'.'.$imageFileType2;

        if ($imageFileType2 != "jpg" && $imageFileType2 != "png" && $imageFileType2 != "jpeg"
            &&  $imageFileType2 != "pdf" && $imageFileType2 != "docx" && $imageFileType2 != "doc") {
            $photoerror = "Sorry, only JPG, JPEG, PNG, PDF & Docx files are allowed.";
        } else if ($_FILES["photo"]["size"] > 500000) {
            $photoerror = "Sorry, your file is too large.";
        } else {
            if (move_uploaded_file($_FILES["photo"]["tmp_name"], $target_file2)) {
                $success2 = "The file ". basename( $_FILES["photo"]["name"]). " has been uploaded.";
                $photoerror = "";
            } else {
                $photoerror = "Sorry, there was an error uploading your file.";
            }
        }
    } else {
        $photoerror = "Please choose file";
    }
     // file upload for Ticket document
    if (isset($_FILES["tickets"]["name"]) && $_FILES["tickets"]["name"] !='') {
        $file_name3     = $_FILES["tickets"]["name"];
        $target_dir3    = "documents/";
        $imageFileType3 = pathinfo($file_name3,PATHINFO_EXTENSION);
        $target_file3   = $target_dir3.$first_name.'tickets'. time().'.'.$imageFileType3;
        $ticketsnme     = $first_name.'tickets'.time().'.'.$imageFileType3;

        if ($imageFileType3 != "jpg" && $imageFileType3 != "png" && $imageFileType3 != "jpeg"
            &&  $imageFileType3 != "pdf" && $imageFileType3 != "docx" && $imageFileType3 != "doc") {
            $ticketserror = "Sorry, only JPG, JPEG, PNG, PDF & Docx files are allowed.";
        } else if ($_FILES["tickets"]["size"] > 500000) {
            $ticketserror = "Sorry, your file is too large.";
        } else {
            if (move_uploaded_file($_FILES["tickets"]["tmp_name"], $target_file3)) {
                $success3     = "The file ". basename( $_FILES["tickets"]["name"]). " has been uploaded.";
                $ticketserror = "";
            } else {
                $ticketserror = "Sorry, there was an error uploading your file.";
            }
        }
    } else {
        $ticketserror = "Please choose file";
    }
     // php mailer begins here for sending email for user
    //Load composer's autoloader
    require_once 'vendor/autoload.php';
    $mail = new PHPMailer(true);                              // Passing `true` enables exceptions

    try {
        //Server settings
        $mail->SMTPDebug = 4;                                 // Enable verbose debug output
        $mail->isSMTP();                                      // Set mailer to use SMTP         
        // Specify main and backup SMTP servers
        $mail->Host = 'smtpout.asia.secureserver.net';
        $mail->SMTPAuth = true;                               // Enable SMTP authentication
        $mail->Username = 'xxxx@xxxx.com';                 // SMTP username
        $mail->Password = 'xxxxxxx';                           // SMTP password
        //$mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
        $mail->Port = xxxx;                                    // TCP port to connect to
        //Recipients
        $mail->setFrom('support@visamalaysiaonline.com', 'Visa Malaysia Online');
        $mail->addAddress($email, $name);
        $mail->addReplyTo('support@visamalaysiaonline.com', 'Visa Malaysia Online');

        //Content$ticketsnme
        $mail->isHTML(true); // Set email format to HTML
        $mail->Subject = 'Malaysian Visa Online';
        $mail->Body = 'Hi '.$name.'<br> Thank you . We wil Reply or get back to you within 24hrs</h3>';
        $mail->send();
        echo "<h3>Thank you . We wil Reply or get back to you within 24hrs</h3>";
    } catch (Exception $e) {
        echo "<h4>Error try again later</h4>";
    }
     // php mailer begins here for sending email for Admin
    $mail2 = new PHPMailer(true);                              // Passing `true` enables exceptions
    try {
        //Server settings
        $mail2->SMTPDebug = 0;                                 // Enable verbose debug output
        $mail2->isSMTP();                                      // Set mailer to use SMTP        
        // Specify main and backup SMTP servers
        $mail2->Host = 'smtpout.asia.secureserver.net';
        $mail2->SMTPAuth = true;                               // Enable SMTP authentication
        $mail2->Username = 'xxxx@xxxxx.com';                 // SMTP username
        $mail2->Password = 'xxxxx';                           // SMTP password
        //$mail2->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
        $mail2->Port = xxxx;                                    // TCP port to connect to
        //Recipients
        $mail2->setFrom('support@visamalaysiaonline.com', 'Visa Malaysia Online');
        $mail2->addAddress('support@visamalaysiaonline.com', 'Visa Malaysia Online');
        $mail2->addReplyTo('support@visamalaysiaonline.com', 'Visa Malaysia Online');
        $mail->addAttachment("documents/$passportnme");         // Add attachments
        $mail->addAttachment("documents/$photonme", "documents/$ticketsnme");    // Optional name
        //Content
        $mail2->isHTML(true); // Set email format to HTML
        $mail2->Subject = 'Application for Visa Request';
        $mail2->Body = '<html><body><table rules="all" style="border-color: rgb(130,0,67);" cellpadding="10"><tr><td><strong>Name:</strong> </td><td>'.$name.'</td></tr><tr><td><strong>Email:</strong> </td><td>'.$email.'</td></tr><tr><td><strong>Phone:</strong> </td><td>'.$phone.'</td></tr></table></body></html>';
        $mail2->send();
        echo "<h3>admin mailsent</h3>";
    } catch (Exception $e) {
        echo "<h4>admin mail falied</h4>";
    }
}
?> <?php 
echo $success.'<br>';
echo $passporterror.'<br>';
echo $success2.'<br>';
echo $photoerror.'<br>';
echo $success3.'<br>';
echo $ticketserror.'<br>';
echo $passportnme.'<br>';
echo $photonme.'<br>';
echo $ticketsnme.'<br>';
?>

这是我的HTML代码

<form role="form" method="POST" action="#" method="post" class="contact-form" id="get-in-touch" enctype="multipart/form-data">
                <ul>
                    <li><label for="">Name: </label><input name="name" class="form-control input-box" value="" type="text" required> </li>
                    <li><label for="">Email: </label><input name="email" class="form-control input-box" value="" type="email" required> </li>
                    <li><label for="">Phone: </label><input name="phone" class="form-control input-box" value="" type="text" required> </li>
                    <li><label for="">Scanned colour copy of the first and the last page of your passport :</label>
                        <input name="passport" class="form-control input-box"  type="file" required></li>
                    <li><label for="">Scanned colour Passport-size Photographs taken against a white background :</label>
                        <input name="photo" class="form-control input-box"  type="file" required></li>
                    <li><label for="">Confirmed air tickets (onward & return) :</label>
                        <input name="tickets" class="form-control input-box"  type="file" required></li>
                </ul>
                <input type="submit" value="Send Message" name="submit">

            </form>

我不知道此代码中的错误是什么,我没有收到任何附件到我的管理员邮件

Out put i got

enter image description here

1 个答案:

答案 0 :(得分:0)

这部分出错了 我改变了

$mail->addAttachment("documents/$passportnme");         // Add attachments
    $mail->addAttachment("documents/$photonme", "documents/$ticketsnme");    // Optional name

到此

$mail2->addAttachment("documents/$passportnme");         // Add attachments
    $mail2->addAttachment("documents/$photonme", "documents/$ticketsnme");    // Optional name

$ MAIL-&GT;对于用户 $ mail2-&GT;为管理员 只是一个拼写错误