问题
Hi I am using swiftmailer to send a email with attachment. When I send the email, I get an error. I know the file is being uploaded and without
->attach(Swift_Attachment::fromPath('$target_file'));
the email sends without a problem. I am sending a small image file at the moment, but have tried different file types with on luck.
The error that the php code returns is as follows:
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
My full php code is stated here:
<?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'));
?>
Please if you can steer me in the right direction I would be greatfull!
回答1:
Found the solution. The copy of swiftmailer that I had was corrupted to an extent. Git Files Of Correct Swiftmailer
After uploading the new files, everything worked!
来源:https://stackoverflow.com/questions/37444737/swiftmailer-email-message-returns-error-when-attaching-file