【问题标题】:PHPMailer Sending just One FilePHPMailer 只发送一个文件
【发布时间】:2018-10-27 00:43:36
【问题描述】:

我正在尝试使用 PHPMailer 类发送带有多个附件的电子邮件。多个文件在目录中成功上传,但它只在电子邮件中发送一个文件。以及如何使用引导程序格式化我的电子邮件正文?您的建议将不胜感激。我正在使用 PHPMailer 6.0.5

这是我的 PHP 代码:

<?php 

    $msg = '';
    use PHPMailer\PHPMailer\PHPMailer;
    include_once 'PHPMailer/PHPMailer.php';
    include_once 'PHPMailer/Exception.php';
    // include_once 'PHPMailer/SMTP.php';

    if (isset($_POST['submit'])) {

        $inputZip = $_POST['inputZip'];
        $selectService = $_POST['selectService'];
        $lawnMovingService = $_POST['lawnMovingService'];
        $leafRemovalService = $_POST['leafRemovalService'];
        $snowPlowingService = $_POST['snowPlowingService'];
        $handymanService = $_POST['handymanService'];
        $inputName = $_POST['inputName'];
        $inputEmail = $_POST['inputEmail'];
        $inputPhone = $_POST['inputPhone'];
        $inputMessage = $_POST['inputMessage'];

        if (isset($_FILES['images']['name']) && $_FILES['images']['name'] != '') {
            $destination = "attachment/";
            foreach ($_FILES["images"]["tmp_name"] as $key => $value) {
                $tmp_name = $_FILES["images"]["tmp_name"][$key];
                $name = $destination . basename($_FILES["images"]["name"][$key]);
                move_uploaded_file($tmp_name, $name);
            }
        } else {
            $name = '';
        }

        $mail = new PHPMailer;

        //For SMTP 
        //$mail->Host = "smtp.gmail.com";
        //$mail->isSMTP(); // This line may cause problem
        //$mail->SMTPAuth = true;
        //$mail->Username = "example@gmail.com";
        //$mail->Password = "examplePassword";
        //$mail->SMTPSecure = "ssl"; //OR TLS
        //$mail->Port = 465; //TLS : 587

        $mail->addAddress('milan.uptech@gmail.com');
        $mail->setFrom($inputEmail);
        $mail->Subject = 'Service Booking from Website';
        $mail->isHTML(true);
        $mail->Body = $inputMessage;
        $mail->addAttachment($name);

        if ($mail->send()) {
            header('Location: index.php');
        } else {
            header('Location: contact.php');
        }

        // if(sendemail('milan.uptech@gmail.com', $email, $name, $body, $file)) {
        //      $msg = 'Email Sent!';
        //      sendemail($inputEmail, 'milan.uptech@gmail.com', $inputName, 'We have received your email');
        //  }
    }

【问题讨论】:

  • 您需要为每个要附加的文件调用 addAttachment(),所以在循环中
  • @smith 如何循环 addAttachment() 你有什么好的例子吗?
  • 我马上补充答案,
  • 基本上,只需将您的 foreach 循环向下移动到您正在执行的位置 addAttachment 并在其中移动 addAttachment 调用。
  • @smith 我希望我可以将您的答案标记为完美解决方案。 ;)

标签: php email phpmailer email-attachments


【解决方案1】:

我将类创建移到文件文件处理循环之前,并将附件代码移到循环中

<?php 

    $msg = '';
    use PHPMailer\PHPMailer\PHPMailer;
    include_once 'PHPMailer/PHPMailer.php';
    include_once 'PHPMailer/Exception.php';
    // include_once 'PHPMailer/SMTP.php';

    if (isset($_POST['submit'])) {

        $inputZip = $_POST['inputZip'];
        $selectService = $_POST['selectService'];
        $lawnMovingService = $_POST['lawnMovingService'];
        $leafRemovalService = $_POST['leafRemovalService'];
        $snowPlowingService = $_POST['snowPlowingService'];
        $handymanService = $_POST['handymanService'];
        $inputName = $_POST['inputName'];
        $inputEmail = $_POST['inputEmail'];
        $inputPhone = $_POST['inputPhone'];
        $inputMessage = $_POST['inputMessage'];

        $mail = new PHPMailer; //moved here

        if (isset($_FILES['images']['name']) && $_FILES['images']['name'] != '') {
            $destination = "attachment/";
            foreach ($_FILES["images"]["tmp_name"] as $key => $value) {
                $tmp_name = $_FILES["images"]["tmp_name"][$key];
                $name = $destination . basename($_FILES["images"]["name"][$key]);
                move_uploaded_file($tmp_name, $name);
                $mail->addAttachment($name); //attache here
            }
        } else {
            $name = '';
        }



        //For SMTP 
        //$mail->Host = "smtp.gmail.com";
        //$mail->isSMTP(); // This line may cause problem
        //$mail->SMTPAuth = true;
        //$mail->Username = "example@gmail.com";
        //$mail->Password = "examplePassword";
        //$mail->SMTPSecure = "ssl"; //OR TLS
        //$mail->Port = 465; //TLS : 587

        $mail->addAddress('milan.uptech@gmail.com');
        $mail->setFrom($inputEmail);
        $mail->Subject = 'Service Booking from Website';
        $mail->isHTML(true);
        $mail->Body = $inputMessage;


        if ($mail->send()) {
            header('Location: index.php');
        } else {
            header('Location: contact.php');
        }

        // if(sendemail('milan.uptech@gmail.com', $email, $name, $body, $file)) {
        //      $msg = 'Email Sent!';
        //      sendemail($inputEmail, 'milan.uptech@gmail.com', $inputName, 'We have received your email');
        //  }
    }

【讨论】:

  • 谢谢!完美运行。另一个问题是如何使用引导程序或纯 css 格式化电子邮件正文?是否可以通过自定义消息让发件人知道“我们已收到您的消息”?
  • @MilanUptech $inputMessage 是您的正文,您可以添加任何您喜欢的 html,但电子邮件 html 和网页 html 不一样,如果您想要所有电子邮件客户端,您不想做任何花哨的事情支持它。目前成功重定向到'index.php'你可以改变它
  • 这是不安全的——你没有检查move_uploaded_file的返回值,只是假设它有效。使用用户提供的文件名也是不安全的。看看the PHPMailer example that does the same thing 如何避免这两个问题。
  • @smith 哦。谢谢。我正在尝试将其重定向到带有金额值的贝宝付款页面。所以我只是想把 index.php 重定向。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-28
  • 2012-12-10
  • 1970-01-01
  • 1970-01-01
  • 2012-09-01
  • 2012-09-15
相关资源
最近更新 更多