gpt4 book ai didi

email - 使用 php mailer 发送带附件的电子邮件

转载 作者:行者123 更新时间:2023-12-01 12:51:52 25 4
gpt4 key购买 nike

谁能告诉我用 phpMailer 发送带附件的电子邮件的步骤。我已经准备好所有代码来发送电子邮件本身,它工作正常但我不明白如何发送附件。我有表格,如果我从 POST 上的文件输入中获取值,它只会给我文件名而不是完整路径。我认为为了添加附件,我需要获取文件的完整路径,对吗?我不必将文件存储在服务器上,只需要通过电子邮件发送即可。

最佳答案

我找到了解决方案:首先,在我的表单中我必须包含 enctype="multipart/form-data"来发送文件,所以它必须是这样的:

<form method="POST"  action="send.php" enctype="multipart/form-data">
inputs...
</form>

之后我以这种方式获取文件:

if(is_uploaded_file($_FILES['myfile']['tmp_name'])) {
$file = $_FILES['myfile'];
}

然后我准备此文件的路径,该文件现在位于临时目录中并确定新文件名:

$attachment_path = $this->file['tmp_name'];

//If need to give new name for the file:
//$newfilename = pathinfo(basename($this->file['name']));
//$attachment_name = "attachment_new_name.".$newfilename['extension'];

$attachment_name = basename($this->file['name']);

$mail->AddAttachment($attachment_path, $attachment_name);

完成!现在附上文件。

关于email - 使用 php mailer 发送带附件的电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11816101/

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