2017-07-21 63 views
3

因爲我現在找不到三天的答案,所以我試着自己去問。如何將Swiftmailer集成到Angular2中

我對Angular有點新鮮,2周前開始學習它。我用angular-cli建立了一個項目。到目前爲止,一切都很好,除了我的聯繫表格之外,我想象的都是。

我想 我想Swiftmailer融入我的角度的項目,所以我其實可以通過聯繫表格發送電子郵件本地(Linux Mint的)我自己。

我有什麼 我已經有了一個簡單的引導形式(姓名,電子郵件,消息)和我通過作曲家添加Swiftmailer。我創建了一個名爲mail.php文件(因爲角度不能做到這一點),到我的資產文件夾:

<?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(['[email protected]' => 'Suzu']) 
->setBody($request->message) 
; 

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

在我的模板,我設置操作這個文件。當我嘗試提交時,沒有任何反應。我的想法是,我需要在我的contact.component.ts中做些事情,但是當我嘗試導入「Swiftmailer/Message.php」時,我失敗了。

有沒有人有一個想法,我需要做什麼使它一起工作?

版本:

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 
+1

在學習像Angular這樣複雜的前端框架之前,您似乎缺乏基本的表單/ POST知識。你應該區分前端和後端。 –

回答

2

我解決了它,終於。加載vendor/autoload.php時我錯過了「/」,這就是爲什麼它不起作用。

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