0

I have created a HTML5 contact form that I'm currently testing on a local host (XAMPP) using Swiftmailer, and after clicking the submit button I get the following error:

Warning: require_once(/swift.php): failed to open stream: No such file or directory in /Applications/XAMPP/xamppfiles/htdocs/testsite/sendmessage.php on line 10

Line 10 refers, obviously, to the require_once line in my PHP, however in my file it is encased in HTML tags.

I believe that this error is telling me that the file does not exist, but it does and I can browse there with no problems.

Both my HTML file and the 'swift.php' file itself is in /Applications/XAMPP/xamppfiles/htdocs/testsite/ so I'm unsure why I'm getting this error. I have tried the following:

require_once(realpath(dirname(__FILE__).'swift.php')); without success.

I have also tried a number of permutations of present the file path (ie things like ../, and including the full file path, also with no success.

My HTML (form.html) is:

<form id="contactform" name="contact" method="post" action="sendmessage.php">
  <label for="Name">Name: *</label><br>
  <input type="text" name="Name" id="Name"><br>

  <label for="Email">Email: *</label><br>
  <input type="Email" name="Email" id="Email" class="txt"><br>

  <label for="Message">Message: *</label><br>
  <textarea name="Message" rows="20" cols="20" id="Message" class="txtarea"></textarea><br>

  <button type="submit" id="submit-button">Send E-mail</button>
</form>

My PHP ('sendmessage.php') is (again, not encased in HTML and BODY tags here):

require_once("/swift.php");
echo 'Mail sent <br />';

// Grab the post data
$name = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
$usermail = filter_var($_POST['usermail'], FILTER_SANITIZE_EMAIL);
$content = filter_var($_POST['content'], FILTER_SANITIZE_STRING);

// Construct the body of the email
$data = "Name: " . $name . "<br />" . "Email: " . $usermail . "<br />" . "Message: " . $content;

// Create the transport
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 587, 'tls')
    ->setUsername('foobar@googlemail.com')
    ->setPassword('GENERATED PASSWORD');
echo 'line 26 <br />';

// Create the mailer using the created transport
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('This is the subject')
    ->setFrom(array('foobar@googlemail.com' => 'Feedback Received From User'))
    ->setTo(array('somemail@gmail.com', 'foobar@googlemail.com' => 'Lead Recipients'))
    ->setSubject('Here is your Feedback subject')
    ->setBody($data, 'text/html');
echo 'line 34 <br />';

// Send the message
$result = $mailer -> send($message);
echo $result;
echo 'line 39<br />';
?>

The 'GENERATED PASSWORD' is the app specific one required when you use Google's Two Step Verification, but this has of course been removed here.

I have looked at the following questions here, here, and here but I appear to have carried out the advice given.

I have tried amending 'require_once("/swift.php");' to 'require_once("swift.php");', however this gives me the following error:

Warning: require(/Applications/XAMPP/xamppfiles/htdocs/testsite/classes/Swift.php): failed to open stream: No such file or directory in /Applications/XAMPP/xamppfiles/htdocs/testsite/swift.php on line 20

The difference is the addition of a 'classes' subfolder now in the path. Putting 'swift.php' in this folder gives me the original error.

Line 20 for reference is:

$data = "Name: " . $name . "<br />" . "Email: " . $usermail . "<br />" . "Message: " . $content;

I have looked to check the following code -

In sendmessage.php on line 10:

require_once(dirname(__FILE__)."/swift.php");

In swift.php on line 20:

require(dirname(__FILE__)."/classes/Swift.php");

and these are present and correct. The error following checking and re-running is:

Warning: require(/Applications/XAMPP/xamppfiles/htdocs/testsite/classes/classes/Swift.php): failed to open stream: No such file or directory in /Applications/XAMPP/xamppfiles/htdocs/testsite/classes/Swift.php on line 20

It appears to be looking for an extra 'classes' folder every time I add one of those folders.

Community
  • 1
  • 1
tomdot
  • 551
  • 6
  • 20
  • Where are both files located within your project directory? – the_pete Jul 27 '15 at 20:15
  • I did state that near the top, however the full path is /Applications/XAMPP/xamppfiles/htdocs/testsite/ – tomdot Jul 27 '15 at 20:16
  • 1
    Before you `require_once` run `echo getcwd();` just to verify which directory your script is actually running in. Then try to include/require_once the file relative to that directory. – TheHvidsten Jul 27 '15 at 20:16

3 Answers3

1

try to change,

require_once("/swift.php");

to

require_once("swift.php");
1

The error message says:

Warning: require_once(/swift.php): failed to open stream: No such file or directory

You claim:

I believe that this error is telling me that the file does not exisit, but it does and I can browse there with no problems.

And your alleged proof is:

Both my HTML file and the 'swift.php' file itself is in /Applications/XAMPP/xamppfiles/htdocs/testsite/

But that does not make sense.
/swift.php is an absolute path and has nothing to do with the folder you're looking in.

Take out the / if you wanted a relative path:

require_once("/swift.php");
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • I commented elsewhere that I have done this and gives me exactly the same error, but for line 20 instead of line 10. – tomdot Jul 27 '15 at 20:19
  • 1
    @tomdot: Then fix it also on line 20... though it bears mentioning that, since there are no further `require_once` calls in this code file, that is _impossible_. Are you sure you are reading the error carefully enough? Regardless, there is no cause shown in the question. So, for all intents and purposes, your original question has been answered and solved and that's that. Spend some time on this new problem and post a new question later (with a decent [testcase](http://stackoverflow.com/help/mcve) if you're still stuck. – Lightness Races in Orbit Jul 27 '15 at 20:20
  • I have updated my question to show how this has affected the error - the jist is that the original error remains. I did edit my question to show that the new error is slightly different, however it is still a 'failed to open stream' error so the question is still valid. – tomdot Jul 27 '15 at 20:27
1

To summarize it:

/Applications/XAMPP/xamppfiles/htdocs/testsite/sendmessage.php
/Applications/XAMPP/xamppfiles/htdocs/testsite/swift.php
/Applications/XAMPP/xamppfiles/htdocs/testsite/classes/Swift.php

In sendmessage.php on line 10:

require_once(dirname(__FILE__)."/swift.php");

In swift.php on line 20:

require(dirname(__FILE__)."/classes/Swift.php");
hellcode
  • 2,678
  • 1
  • 17
  • 21
  • The line for swift.php is the default in the file, and the sendmessage.php line I have tried with no success. – tomdot Jul 27 '15 at 20:37