2

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!

Simon Erasmus
  • 134
  • 12
  • *ahem* => `$attachment = $_POST["Attach"];` - it's `$_FILES` here, just like the others and make sure you've a valid enctype for it also and a POST method and that it has the same name attribute for the file type input. – Funk Forty Niner May 25 '16 at 18:16
  • possible duplicate of [PHP: “Notice: Undefined variable” and “Notice: Undefined index”](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Funk Forty Niner May 25 '16 at 18:19
  • Hi, thanks. So you mean I must change the $_POST to $_FILES? But how where do I add enctype and the POST method. Could you post a solution maybe? @Fred-ii- – Simon Erasmus May 25 '16 at 18:19
  • yes and read http://php.net/manual/en/features.file-upload.post-method.php answer's in there ;-) – Funk Forty Niner May 25 '16 at 18:19
  • and your `move_uploaded_file()` is failing you. If you intend on storing it in a folder, then read the manual for it http://php.net/manual/en/function.move-uploaded-file.php – Funk Forty Niner May 25 '16 at 18:24
  • I changed $_POST to $_FILES but still get the same error – Simon Erasmus May 25 '16 at 18:29
  • For now I am only trying to send a image that is already on the server as the updated post shows – Simon Erasmus May 25 '16 at 18:30

1 Answers1

0

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!

Simon Erasmus
  • 134
  • 12