PHP Swift Mailer Setup

2019-06-08 00:01发布

问题:

I'm trying to implement swift mailer into my website so I can send password recovery emails to registered users. I'm using dreamweaver. What I have done:

1) Downloaded Swift-4.2.2.tar
2) Extracted it
3) Uploaded to my host in /Domain/classes/lib
4) Included it in Recovery.php

Here's my file structure

Main Folder
     |classes
          |Swift
     |Private
          |Recovery.php

And here's the code for Recovery.php:

require_once '../classes/lib/swift_required.php';
// Create the Transport
$email_to = $_POST["email"];
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465)
  ->setUsername('Username')
  ->setPassword('Password')
  ;

// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);

// Create a message
$message = Swift_Message::newInstance()

  // Give the message a subject
  ->setSubject('Password Recovery')

  // Set the From address with an associative array
  ->setFrom(array('Username@domain.com' => 'Username'))

  // Set the To addresses with an associative array
  ->setTo($email_to)

  // Give it a body
  ->setBody('Below is your temporary password. Please make sure to login in immediately and change it to a password of your choice.\n'.$password);

// Send the message
$result = $mailer->send($message);

But this is not including the libraries because if i do Swift_SmtpTransport:: then dreamweaver should popup an intellisense thing that gives me options of methods or variables, meaning it can see the class and all of it's member items. But since it doesn't popup the options, i get the feeling it cannot see what swift class is. And when I tried to run it, the console network tab said it was pending for a while before it returned a 500 Internal Server Error. So it's not including the libraries properly. Any idea what I'm doing wrong?

Edit: Error reporting output:

Warning: require(/*/*/*/Main Folder/classes/lib/classes/Swift.php): failed to open stream: No such file or directory in /*/*/*/Main Folder/classes/lib/swift_required.php on line 22 
Fatal error: require(): Failed opening required '/*/*/*/Main Folder/classes/lib/classes/Swift.php' (include_path='.:/usr/local/lib/php') in /*/*/*/Main Folder/classes/lib/swift_required.php on line 22

回答1:

Open swift_required.php file. There's a require() on line 22. Adjust it's path to match lib/classes/Swift.php



回答2:

Maybe it helps when You check paths in swift_required.php, they must be correct from point of called out file



回答3:

try using absolute path instead of relative path

for example in linux

require_once '/var/www/project/mailer/lib/swift_required.php' ;

alternatively try replacing above line with

require_once '/path/to/mailer/lib/swift_init.php';

swift mailer uses its own class autoloader, do you already have any other autoloader?