gpt4 book ai didi

php - PHP Mailer 中的简单 PDF 文件附件

转载 作者:行者123 更新时间:2023-11-28 00:53:33 26 4
gpt4 key购买 nike

我正在尝试使用 PHP 邮件程序以最简单的方式发送带有附件的电子邮件。

我试过用这个

   $mail ->addAttachment("path_to_pdf", "pdf_name);

但它不起作用,因为“邮件已发送”但未发送“PDF 文件附件”。

请帮我解决我的问题,我想在发送给收件人的电子邮件中附加一个pdf文件。谢谢!

这是我用来发送带文件附件的电子邮件的文件。

index.html

        <html>
<head>
</head>
<body>
<form method="post" action="send_mail.php" enctype="multipart/form-data">
To : <input type="text" name="mail_to"> <br/>
Subject : <input type="text" name="mail_sub">
<br/>
Message <input type="text" name="mail_msg">
<br/>
File: <input type="file" name="file" >
<br/>
<input type="submit" value="Send Email">

</form>
</body>
</html>

发送邮件.php

      <?php

$mailto = $_POST['mail_to'];
$mailSub = $_POST['mail_sub'];
$mailMsg = $_POST['mail_msg'];
require 'PHPMailer-master/PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail ->IsSmtp();
$mail ->SMTPDebug = 0;
$mail ->SMTPAuth = true;
$mail ->SMTPSecure = 'ssl';
$mail ->Host = "smtp.gmail.com";
$mail ->Port = 465; // or 587
$mail ->IsHTML(true);
$mail ->Username = "acc.ldevera@gmail.com";
$mail ->Password = "accountsamplepassword";
$mail ->SetFrom("acc.sample@gmail.com");
$mail ->Subject = $mailSub;
$mail ->Body = $mailMsg;
$mail ->AddAddress($mailto);
$mail->AddAttachment('pdf_files/', 'reservation.pdf');


if(!$mail->Send())
{
echo "Mail Not Sent";
}
else
{
echo "Mail Sent";
}

?>

请帮我解决我的问题,谢谢!

最佳答案

这一行没有达到你的预期

$mail->AddAttachment('pdf_files/', 'reservation.pdf');

它试图找到名为“pdf_files/”的文件并想要添加它。但是,正如您现在可能想象的那样,这不是一个正确的文件名。 AddAttachment 的第一个参数是文件的路径(即包括文件的文件名),第二个参数是文件名,在电子邮件中显示 ,文件应该如何被调用/命名,因此您可以不同地调用它,而无需重命名原始文件。

所以上面这行应该是:

$mail->AddAttachment('pdf_files/reservation.pdf', 'reservation.pdf');

关于php - PHP Mailer 中的简单 PDF 文件附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45996378/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com