0

I'm trying to use swiftmailer to send transactional emails but I always get the following error in Email.php, when I try to create swiftmailer's objects:

Warning: require_once(Swift/SmtpTransport.php): failed to open stream: No such file or directory in C:\xampp\htdocs\Tutorials\E-commerce\inc\autoload.php on line 13

Fatal error: require_once(): Failed opening required 'Swift/SmtpTransport.php' (include_path='C:\xampp\htdocs\Tutorials\E-commerce\classes;C:\xampp\htdocs\Tutorials\E-commerce\pages;C:\xampp\htdocs\Tutorials\E-commerce\mod;C:\xampp\htdocs\Tutorials\E-commerce\inc;C:\xampp\htdocs\Tutorials\E-commerce\template;C:\xampp\php\PEAR') in C:\xampp\htdocs\Tutorials\E-commerce\inc\autoload.php on line 13

The thing is, I included composer's and swiftmailer's autoloaders (separately), so it should find the classes. I have a custom autoloader but, if I include swiftmailer's autoloader directly, even if my autoloader doesn't find the classes, the include statement should be triggered and find them right? I can get it to work if I include each class individually but that's not very practical.

This is my dirs structure:

index.php
inc
    config.php
    autoload.php
classes
    SwiftMailer
         vendor
         composer.json
         composer.lock
    Url.php
    Core.php
    Email.php
    Login.php
    etc
pages
    login.php
    basket.php
    etc
etc

Email.php:

<?php

// I tried including them separately (also, php has no problem in finding them):
require_once 'SwiftMailer/vendor/autoload.php';
require_once 'SwiftMailer/vendor/swiftmailer/swiftmailer/lib/swift_required.php';

class Email {

    public function __construct() {
        $this->objTransport = Swift_SmtpTransport::newInstance();
        etc

autoload.php:

<?php
require_once('config.php');

function generic_autoloader($class_name) {
    $class = explode("_", $class_name); 
    $path = implode('/', $class) . '.php';
    require_once($path);

}

spl_autoload_register('generic_autoloader');

config.php:

It's a file where I define several constants (eg: classes directory, pages directory, root path..) and at the end I have this:

set_include_path(implode(PATH_SEPARATOR, array(
    realpath(ROOT_PATH.DS.CLASSES_DIR),
    realpath(ROOT_PATH.DS.PAGES_DIR),
    realpath(ROOT_PATH.DS.MOD_DIR),
    realpath(ROOT_PATH.DS.INC_DIR),
    realpath(ROOT_PATH.DS.TEMPLATE_DIR),
    get_include_path()
)));

I'm not sure if this has anything to do with the problem or not (I'm following a tutorial and I haven't quite understood this part of the code yet), so I'll leave it here:

index.php:

<?php
require_once('inc/autoload.php');

$core = new Core();
$core->run();

Core.php:

<?php

class Core {
    public function run() {
        ob_start();
        require_once(Url::getPage()); // Url::getPage just returns the path of the current page ($_GET['page'])
        ob_get_flush();
    }
}

Thanks in advance!

Acla
  • 141
  • 3
  • 12
  • Possible duplicate of [Failed to open stream : No such file or directory](http://stackoverflow.com/questions/36577020/failed-to-open-stream-no-such-file-or-directory) – Vic Seedoubleyew Jun 12 '16 at 09:54
  • Have you actually installed SwiftMailer ? it doesn't appear in your directory structure – Vic Seedoubleyew Jun 12 '16 at 10:02
  • Yes, I installed it using composer. The swiftmailer folder is inside my classes folder. It has 2 files there (composer.json and composer.lock), as well as the 'vendor' folder (which has two directories, composer and swiftmailer, and the autoload.php file). PHP showed no errors trying to include the files... – Acla Jun 12 '16 at 21:02
  • did my answer solve your problem ? – Vic Seedoubleyew Jun 15 '16 at 19:32
  • Sorry for the delay! Unfortunately no :/ There is indeed a SmtpTransport.php file inside a folder called 'Swift', so the file name can't be wrong. Also, initially I copied the file's path, tried including it manually and it worked, but then there was another 'wrong filename/missing directory' error regarding a different class. I was going to include that class' file manually too but I couldn't do the same for all the other files because it's not efficient... – Acla Jun 15 '16 at 21:00
  • I'm using PHPMailer at the moment because I was able to work with their autoloader, but I'm still curious as to why I'm having these problems with SwiftMailer.. ps: commenting twice because of the characters' limit – Acla Jun 15 '16 at 21:00

1 Answers1

0

If there is indeed an SmtpTransport.php file in the right folder, it seems that the error is precisely what it says it is : a wrong file name.

You should replace

$this->objTransport = Swift_SmtpTransport::newInstance();

by

$this->objTransport = SwiftMailer_SmtpTransport::newInstance();

You should also include composer's autoload file (the autoload file inside vendor)

Vic Seedoubleyew
  • 9,888
  • 6
  • 55
  • 76