繁体   English   中英

phpmailer我没有任何附件,但是我收到邮件

[英]Phpmailer I am not getting any attachment, but i am receiving mail

这是申请签证的表格。 代码中的所有内容都可以正常工作。 文件也上传了,同时也获得了用户和管理员的邮件。 但对于管理员邮件文件则不附加在邮件中。

下面是我的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>

我不知道这段代码有什么错误,我没有收到我的管理邮件的任何附件

放了我

在此处输入图片说明

我在这部分更改了一个错误

$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->对于用户$ mail2->对于管理员只是拼写错误

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM