如何使用 PHP Mailer 通过电子邮件发送文件?

问题描述 投票:0回答:1

我一段时间以来一直在尝试在 php 中编写代码以从表单发送文件。

该表单有效,它将信息和文件上传到服务器。

我使用 AJAX 提交表单。

问题:

当我进行电子邮件测试时,我只收到消息......没有附件,或者它甚至没有到达Google电子邮件。当我删除附件的代码行时,我也会在 google mail 上收到该消息。
简而言之,如果我删除添加附件的代码行,代码将完美运行。

有人可以帮助我吗?

我是 PHP 初学者。

    require __DIR__ . '/../../vendor/autoload.php';


    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;
    use PHPMailer\PHPMailer\SMTP;


    $mail = new PHPMailer(true);


    $mail->CharSet = 'UTF-8';
    $mail->Encoding = 'base64';


    $mail->isSMTP();
    $mail->SMTPAuth = true;


    $mail->Host = Config::SMTP_HOST;
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
    $mail->Port = Config::SMTP_PORT;


    $mail->Username = Config::SMTP_USERNAME;
    $mail->Password = Config::SMTP_PASSWORD;


    $mail->addCustomHeader('MIME-Version', '1.0');
    $mail->addCustomHeader('Content-type', 'text/html; charset=UTF-8');
    $mail->addCustomHeader('Viewport', 'width=device-width, initial-scale=1.0');
    $mail->ContentType = 'text/html; charset=UTF-8';
    

    $mail->setFrom($customerEmail, $customerName);
    $mail->addAddress($sellerEmail, $sellerName);
    $mail->addAttachment($_FILES['file']['tmp_name'], $_FILES['file']['name']);
   
    $mail->IsHTML(true);
    $mail->Subject = $subjectForSeller;
    $mail->Body = $messageForSeller;

    $mail->send();
php email phpmailer
1个回答
0
投票

代码良好且干净。检查文件的大小(小于 25 meg)和 mime 类型(不是 EXE、APK 等)并将其上传到您的主机上。然后在 $mail->addAttachment 中使用主机上的文件路径和一个干净的名称(只是带有扩展名的数字/字母)。

如果您需要更多帮助或遇到任何错误,请告诉我。

© www.soinside.com 2019 - 2024. All rights reserved.