我想在注冊后發送電子郵件通知,當用戶注冊時他們會收到電子郵件通知,請幫助我如何發送電子郵件?
用戶控制器
public function store(Request $request)
{
$data = [
'first_name' => $request->first_name,
'last_name' => $request->last_name,
'name' => $request->first_name . ' ' . $request->last_name,
'email' => $request->email,
'password' => bcrypt($request->password),
'country_id' => $request->country,
'user_type' => 'customer',
'active_status' => '1',
'activation_code' => str_random(30)
];
$user_create = User::create($data);
Mail::to($request->email)->send(new UserSignUpMail);
}
用戶注冊郵箱
<?php
class UserSignUpMail extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->markdown('emails.userSignUp');
}
}
uj5u.com熱心網友回復:
Laravel 8 發送郵件示例;在本教程中,您將學習如何在 laravel 8 中使用 Mailgun、Postmark、Amazon SES 和 sendmail 等 SMTP 驅動程式發送電子郵件。
注冊后你可以呼叫你的函式
此鏈接將幫助您發送電子郵件
uj5u.com熱心網友回復:
你使用什么電子郵件服務?在 config/mail.php 中向該服務添加憑據,或者如果它的 sendgrid 我們應該使用它們提供的庫
<?php
namespace App\Traits;
trait SendMail
{
protected function SendEmail($from_name, $to_email, $to_name, $subject, $body, $template)
{
try {
$from_email = '[email protected]';
$email = new \SendGrid\Mail\Mail();
$email->setFrom($from_email, $from_name);
$email->setSubject($subject);
$email->addTo($to_email, $to_name);
$email->addContent("text/plain", $body);
$email->addContent("text/html", $template);
$sendgrid = new \SendGrid('SENDGRID_API_KEY');
$response = $sendgrid->send($email);
return $response->statusCode();
} catch (Exception $e) {
return 'Error: ' . $e->getMessage();
}
}
//example
//$this->SendEmailWithAttachment("from_name", "[email protected]", "name", "Subject", "Body", "Template string", "URL, "file.mp3");
protected function SendEmailWithAttachment($from_name, $to_email, $to_name, $subject, $body, $template, $attachment_file_url, $file_name)
{
try {
$file_encoded = base64_encode(file_get_contents($attachment_file_url));
$from_email = '[email protected]';
$email = new \SendGrid\Mail\Mail();
$email->setFrom($from_email, $from_name);
$email->setSubject($subject);
$email->addTo($to_email, $to_name);
$email->addContent("text/plain", $body);
$email->addContent("text/html", $template);
$email->addAttachment(
$file_encoded,
null,
$file_name,
null
);
$sendgrid = new \SendGrid(getenv("SENDGRID_API_KEY"));
$response = $sendgrid->send($email);
return $response->statusCode();
} catch (Exception $e) {
return 'Error: ' . $e->getMessage();
}
}
}
uj5u.com熱心網友回復:
自定義發送電子郵件app/mail.php或.env檔案。您可以使用https://mailtrap.io/
uj5u.com熱心網友回復:
首先,您已經在 .env 檔案中設定了正確的 Mailsettings。
MAIL_DRIVER=smtp
MAIL_HOST=your.server.com
MAIL_PORT=25
MAIL_USERNAME=1234
MAIL_PASSWORD=password
MAIL_ENCRYPTION=null
[email protected]
您可以像這樣從控制器發送郵件:
return \Mail::to("[email protected]")
->send(new UserSignUpMail());
或通過路線:
Route::get(“/send-email”, function() {
return new UserSignUpMail();
});
在這兩種情況下,您都需要 Laravel Mail Facade。
use Illuminate\Support\Facades\Mail;
在userSignUp類中的build() 函式中,然后替換:
return $this->markdown('emails.userSignUp');
和 :
$this->view('emails.userSignUp');
你發送“真實”的郵件。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/326509.html
標籤:拉拉维尔
