How to integrate Swiftmailer into Angular2

since I couldn't find an answer for three days, I try to ask it myself.

I'm new to Angular, started learning it 2 weeks ago. I created a project with angular-cli. So far so good, everything is working as I imagined, except for my contact form.

What I want I want to integrate Swiftmailer into an Angular project so I can send email locally (Linux Mint) to myself via a contact form.

What I have I already have a simple bootstrap form (name, email, message) and I added Swiftmailer via composer. I created a file called mail.php (because Angular cannot do this) in my resources folder:

<?php
require_once __DIR__.'../vendor/autoload.php';

$request = json_decode(file_get_contents('php://input'));

// Create the Transport
$transport = (new Swift_SmtpTransport('smtp.actual-server.de', 25))
->setUsername('actually-filled-in-working-username')
->setPassword('actually-filled-in-working-password')
;

// Create the Mailer using your created Transport
$mailer = new Swift_Mailer($transport);
$mailer->registerPlugin(new Swift_Plugins_AntiFloodPlugin(100, 30));

// Create a message
$message = (new Swift_Message('Test Subject'))
->setFrom([$request->email => $request->name])
->setTo(['suzu.amaranthine@gmail.com' => 'Suzu'])
->setBody($request->message)
;

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

      

In my template, I have set an action to this file. When I try to obey, nothing happens. My idea was that I need to do something in my contact.component.ts, but when I try to import "Swiftmailer / Message.php" I am failing.

Does anyone have an idea what I need to do to make it work together?

Versions:

PHP 7.0.18-0ubuntu0.16.04.1 (cli) ( NTS )

"swiftmailer/swiftmailer": "^6.0"

@angular/cli: 1.2.0
node: 6.11.1
os: linux x64
@angular/animations: 4.3.0
@angular/cdk: 2.0.0-beta.8
@angular/common: 4.3.0
@angular/compiler: 4.3.0
@angular/core: 4.3.0
@angular/forms: 4.3.0
@angular/http: 4.3.0
@angular/material: 2.0.0-beta.8
@angular/platform-browser: 4.3.0
@angular/platform-browser-dynamic: 4.3.0
@angular/router: 4.3.0
@angular/cli: 1.2.0
@angular/compiler-cli: 4.3.0
@angular/language-service: 4.3.0

      

+3


source to share


1 answer


I solved it finally. I was missing the "/" when loading vendor / autoload.php, so it didn't work.



<?php
require_once __DIR__ . '/../../vendor/autoload.php';
require_once __DIR__ . '/../../vendor/swiftmailer/swiftmailer/lib/swift_required.php';

      

+2


source







All Articles