使用 PDF 发送邮件时的 PHP 邮件程序问题

php mailer problem while sending the mail with pdf

提问人:Aakash Mishra 提问时间:8/17/2023 最后编辑:OlivierAakash Mishra 更新时间:8/17/2023 访问量:31

问:

所以当有人发送邮件时,我想用PDF发送邮件。我的代码前段时间正在工作,但它突然停止工作,不知道到底发生了什么导致它这样做。请在此处附加代码,请告诉我到底需要更改什么。

以及为什么它突然停止工作。

<?php

use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
require 'vendor/autoload.php';
$mail = new PHPMailer(true);

if (isset($_POST['send'])) {
    print_r($_FILES);
    $email = $_POST['email'];
    $subject = $_POST['subject'];
    $message = $_POST['message'];

$fname = $_POST["fname"];
$lname = $_POST["lname"];
// $email = $_POST["email"];
$Position = $_POST["Position"];
$LinkedIn = $_POST["LinkedIn"];
$Contact = $_POST["Contact"];
$message = $_POST["message"];
$Subject = "Mail from" . $fname . " " . $lname . ". Position: " . $Position;

$Body = "";
$Body .= "First Name: ";
$Body .= $fname;
$Body .= "\n <br>";
$Body .= "\nLast Name: ";
$Body .= $lname;
$Body .= "\n <br> ";
$Body .= "\nEmail: ";
$Body .= $email;
$Body .= "\n <br>";
$Body .= "\nMessage: ";
$Body .= $message;
$Body .= "\n <br>";
$Body .= "\nPosition: ";
$Body .= $Position;
// $Body .= "\n <br>";
// $Body .= "\nResume: ";
// $Body .= $resume;
$Body .= "\n <br>";
$Body .= "\nPhone no: ";
$Body .= $Contact;

try {

    $mail->SMTPDebug = SMTP::DEBUG_SERVER;
    $mail->SMTPDebug = SMTP::DEBUG_OFF;
    $mail->isSMTP();
    $mail->Host = 'smtp.gmail.com';
    $mail->SMTPAuth = true;
    $mail->Username = 'mail id';
    $mail->Password = 'password';
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
    $mail->Port = 587;

    $mail->setFrom('[email protected]', 'aaka');

    $mail->addAddress($email);

    if ($_FILES['attachment']['name'] != null) {
        if (move_uploaded_file($_FILES['attachment']['tmp_name'], "uploads/{$_FILES['attachment']['name']}")) {
            // $mail->addAttachment("uploads/{$_FILES['attachment']['name']}");

            $mail->AddAttachment("uploads/{$_FILES['attachment']['name']}", "{$_FILES['attachment']['name']}");

        }

    }

    $mail->isHTML(true);
    $mail->Subject = $Subject;
    $mail->Body = $Body;
    // $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();
    // echo '<script>window.location.href="index.php?message_sent";</script>';
    // header("Location: sent.html");
    echo header("Location: sent.html");
} catch (Exception $e) {
    echo header("Location: not sent.html");
    }
}
PHP 电子邮件 phpmailer

评论

1赞 Kishan Bhensadadiya 8/17/2023
@Akash请附上您收到的确切错误
1赞 Ariful Islam 8/17/2023
启用 SMTP Debug for Mailer。获取正在发生的事情的确切问题。有问题的附加允许
0赞 droopsnoot 8/17/2023
当它停止工作时发生了什么变化?例如,Gmail 发送要求是否在同一天发生了变化?你的代码认为它已经发送了邮件吗?
0赞 ADyson 8/17/2023
如果它突然停止工作,但你没有更改代码,那么答案更有可能在调试日志中,而不是代码本身。看一看 github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
0赞 Lajos Arpad 8/20/2023
请提供您在日志中发现的错误。

答: 暂无答案