Laravel。如何点击一个按钮发送电子邮件

时间:2017-09-19 14:54:13

标签: php laravel email sendgrid-api-v3

所以我使用的是sendgrid,它有一些很好的文档。我没有使用laravels Mail帮助器,因为它是用于smtp,我想使用他们的Web API。

如果有人最近通过sendgrid我正在使用他们的示例代码(略微适应)来发送电子邮件。

// using SendGrid's PHP Library
// https://github.com/sendgrid/sendgrid-php
// If you are using Composer (recommended)
require 'vendor/autoload.php';

// If you are not using Composer
// require("path/to/sendgrid-php/sendgrid-php.php");

$view = View::make('emails.tenant')->render();
$from = new SendGrid\Email("Pearlai", "pearlai@pearlai.com");
$subject = "Sending with SendGrid is Fun";
$to = new SendGrid\Email("Daniel Cull", "daniel@pearlai.com");
$content = new SendGrid\Content("text/html", $view);
$mail = new SendGrid\Mail($from, $subject, $to, $content);
$apiKey = env('SENDGRID_API_KEY');
$sg = new \SendGrid($apiKey);
$response = $sg->client->mail()->send()->post($mail);
Log::info($response->statusCode());
Log::info($response->headers());
Log::info($response->body());

我不熟悉PHP而不仅仅是Laravel:D我的第一个问题是如何在控制器中正确“使用”sendgrid-api类?就像包含Log facade一样,我会说“使用Log;”

我的目标是拥有一段每周运行一次的代码,当它收集数据时,会生成包含数据的电子邮件并将其发送给收件人。我需要研究一下laravel事件处理程序吗?

编辑:单击按钮时发生错误:

(1/1) FatalThrowableError
Parse error: syntax error, unexpected end of file, expecting function 
(T_FUNCTION) or const (T_CONST)
in EmailController.php (line 87)
at Composer\Autoload\includeFile('C:\\Users\\danie\\Documents\\Pearlai\\CRM_Dashboard\\vendor\\composer/../../app\\Http\\Controllers\\Emails\\EmailController.php')
in ClassLoader.php (line 322)
at ClassLoader->loadClass('App\\Http\\Controllers\\Emails\\EmailController')
at spl_autoload_call('App\\Http\\Controllers\\Emails\\EmailController')
at class_exists('App\\Http\\Controllers\\Emails\\EmailController')
in IlluminateRouteCollector.php (line 63)
at IlluminateRouteCollector->getRouteInformation(object(Route))
in IlluminateRouteCollector.php (line 37)
at IlluminateRouteCollector->collect()
in LaravelDebugbar.php (line 769)
at LaravelDebugbar->collect()
in DebugBar.php (line 246)
at DebugBar->getData()
in JavascriptRenderer.php (line 872)
at JavascriptRenderer->render()
in LaravelDebugbar.php (line 805)
at LaravelDebugbar->injectDebugbar(object(Response))
in LaravelDebugbar.php (line 697)
at LaravelDebugbar->modifyResponse(object(Request), object(Response))
in Debugbar.php (line 60)
at Debugbar->handle(object(Request), object(Closure))
in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in TrustProxies.php (line 56)
at TrustProxies->handle(object(Request), object(Closure))
in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in TransformsRequest.php (line 30)
at TransformsRequest->handle(object(Request), object(Closure))
in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in TransformsRequest.php (line 30)
at TransformsRequest->handle(object(Request), object(Closure))
in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in ValidatePostSize.php (line 27)
at ValidatePostSize->handle(object(Request), object(Closure))
in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in CheckForMaintenanceMode.php (line 46)
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
in Pipeline.php (line 148)
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php (line 53)
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in Pipeline.php (line 102)
at Pipeline->then(object(Closure))
in Kernel.php (line 151)
at Kernel->sendRequestThroughRouter(object(Request))
in Kernel.php (line 116)
at Kernel->handle(object(Request))
in index.php (line 53)
at require_once('C:\\Users\\danie\\Documents\\Pearlai\\CRM_Dashboard\\public\\index.php')
in server.php (line 21)

目前仍然没有正确加载sendgrid类

1 个答案:

答案 0 :(得分:0)

在你的控制器中,你可能错过了一个结束括号。