如何将我放在文件夹中的 PDF 文件添加到此 SMTP 邮件中?

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

我想将附有消息的 PDF 发送给用户。

我的网站文件位于服务器的 Public_htm 文件夹中。我在那里创建了名为“wp-regpdf”的文件夹。当用户注册时,会在此文件夹中创建 PDF。现在我想将 PDF 作为附件发送到他们的电子邮件中。我尝试附加它,但无法获取,仅显示正文消息,没有附件发送。

<?php
session_start();
if ($_SESSION['email'] == '' || $_SESSION['Uniqueid'] == '') {
    session_destroy();
    echo '<script>window.location.href="wp-destroy.php"</script>';
}
?>
<?php
$Uniqueid =$_SESSION['Uniqueid'];  //this the file name iam getting from session
$path = 'wp-regpdf/' . $_SESSION['Uniqueid'] . '.pdf'; // this is the complete path with file name i need
$email = $_SESSION['email'];
include ('smtp/PHPMailerAutoload.php');
echo smtp_mailer($email, 'Testing Mail', 'Please find your payslip attached');
function smtp_mailer($to, $subject, $msg) {
    $mail = new PHPMailer();
    $mail->IsSMTP();
    $mail->SMTPAuth = true;
    $mail->SMTPSecure = 'tls';
    $mail->Host = "smtppro.zoho.in";
    $mail->Port = 587;
    $mail->IsHTML(true);
    $mail->CharSet = 'UTF-8';
    //$mail->SMTPDebug = 2;
    $mail->Username = "";
    $mail->Password = "";
    $mail->SetFrom("[email protected]");
    $mail->addCC('[email protected]');
    $mail->Subject = $subject;
    $mail->Body = $msg;
    $mail->AddAttachment('' . $path . ''); //need help here?
    $mail->AddAddress($to);
    $mail->SMTPOptions = array('ssl' => array('verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => false));
    if ($mail->Send()) {
        echo '<script>window.location.href="thankyou.php"</script>';
    } else {
        echo $mail->ErrorInfo;
    }
}
?>
email smtp phpmailer attachment email-attachments
1个回答
0
投票

最有可能的问题是文件的绝对路径错误。您正在这样设置路径:

$path = 'wp-regpdf/' . $_SESSION['Uniqueid'] . '.pdf';

但是这是一个相对路径,它相对的地方可能不是你想象的地方。最好将其解析为绝对路径,例如使其相对于脚本本身所在的位置:

$path = __DIR__ . '/wp-regpdf/' . $_SESSION['Uniqueid'] . '.pdf';

当你调试这样的东西时,总是质疑你的假设,所以添加一个

echo $path;
来确认它是正确的路径。现在您可以尝试附加它:

if (!$mail->addAttachment($path)) {
    die('Attaching file failed');
}

如果运行脚本的用户没有读取文件的所有权或权限,则路径可能会失败。

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