Swiftmailer 电子邮件 returns 附加文件时出错

Swiftmailer email message returns error when attaching file

您好,我正在使用 swiftmailer 发送带附件的电子邮件。当我发送电子邮件时,出现错误。我知道文件正在上传但没有

->attach(Swift_Attachment::fromPath('$target_file'));

邮件发送没有问题。我现在正在发送一个小图像文件,但幸运地尝试了不同的文件类型。

php代码returns报错如下:

Fatal error: Uncaught exception 'ReflectionException' with message 'Class Swift_Mime_ContentEncoder_Base64ContentEncoder does not exist' in /home/ggraphic/public_html/swiftmailer/lib/classes/Swift/DependencyContainer.php:309 Stack trace: #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 in /home/ggraphic/public_html/swiftmailer/lib/classes/Swift/DependencyContainer.php on line 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'));

?>

如果你能引导我朝着正确的方向前进,我会很高兴!

找到解决方案。我拥有的 swiftmailer 副本在一定程度上已损坏。 Git Files Of Correct Swiftmailer

上传新文件后,一切正常!