【发布时间】:2025-12-24 00:20:23
【问题描述】:
您好,我正在使用 swiftmailer 发送带有附件的电子邮件。当我发送电子邮件时,我收到一个错误。我知道文件正在上传并且没有
->attach(Swift_Attachment::fromPath('$target_file'));
电子邮件发送没有问题。我目前正在发送一个小图像文件,但幸运地尝试了不同的文件类型。
php代码返回的错误如下:
致命错误:在 /home/ggraphic/public_html/swiftmailer/lib/classes/Swift/DependencyContainer.php:309 中未捕获异常“ReflectionException”和消息“Swift_Mime_ContentEncoder_Base64ContentEncoder 类不存在”堆栈跟踪:#0 /home/ggraphic /public_html/swiftmailer/lib/classes/Swift/DependencyContainer.php(309): ReflectionClass->__construct('Swift_Mime_Cont...') #1 /home/ggraphic/public_html/swiftmailer/lib/classes/Swift/DependencyContainer.php (323): Swift_DependencyContainer->_createNewInstance('mime.base64cont...') #2 /home/ggraphic/public_html/swiftmailer/lib/classes/Swift/DependencyContainer.php(114): Swift_DependencyContainer->_createSharedInstance('mime. base64cont...') #3 /home/ggraphic/public_html/swiftmailer/lib/classes/Swift/DependencyContainer.php(371): Swift_DependencyContainer->lookup('mime.base64cont...') #4 /home/ggraphic /public_html/swiftmailer/lib/classes/Swift/DependencyContainer.php(348): Swift_DependencyContainer->_lookupRecursive('mime.base64cont... ') #5 /home/ggraphic/public_html/swiftma 在 /home/ggraphic/public_html/swiftmailer/lib/classes/Swift/DependencyContainer.php 第 309 行
我的完整 php 代码在这里说明:
<?php
require_once 'swiftmailer/lib/swift_required.php';
Swift_Preferences::getInstance()->setCacheType('disk')->setTempDir('/tmp');
$to = "test.email@address.com";
$subject = $_POST["Subject"];
$txt = $_POST["Body"];
$sender_email = $_POST["Email"];
$sender_name = $_POST["Name"];
$attachment = $_FILES["Attach"];
$target_file = basename($_FILES["Attach"]["name"]);
echo $target_file;
move_uploaded_file($_FILES["Attach"]["tmp_name"], $target_file)
$transport = Swift_SmtpTransport::newInstance('mail.testadress.com', 25)
->setUsername('user@testwebsite.com')
->setPassword('Password') ;
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance($subject)
->setFrom(array('user@testwebsite.com' => $sender_name))
->setTo(array($to => 'Dummy Name'))
->setBody("html><body><b>" . $txt . "</b></body></html>")
->addPart('All MY HTML CODE', 'text/html');
->attach(Swift_Attachment::fromPath('myfile.jpg'));
?>
如果你能引导我朝着正确的方向前进,我会很高兴的!
【问题讨论】:
-
ahem =>
$attachment = $_POST["Attach"];- 这里是$_FILES,就像其他人一样,确保你也有一个有效的enctype和一个POST方法它与文件类型输入具有相同的名称属性。 -
嗨,谢谢。所以你的意思是我必须将 $_POST 更改为 $_FILES?但是如何在哪里添加 enctype 和 POST 方法。你能发布一个解决方案吗? @Fred-ii-
-
是的,然后阅读php.net/manual/en/features.file-upload.post-method.php 的答案;-)
-
而你的
move_uploaded_file()让你失望了。如果您打算将其存储在文件夹中,请阅读它的手册php.net/manual/en/function.move-uploaded-file.php
标签: php email attachment email-attachments swiftmailer