如何在发送前使用 Laravel 编辑电子邮件正文

how to edit email body before sending using Laravel

我已经生成了电子邮件功能,可以使用 laravel 向多人发送电子邮件。

现在我想生成一个编辑 window 以便我可以编写电子邮件正文,就像在 Gmail 中一样,如果我们要发送邮件,我们首先编辑正文并点击发送邮件。

所以,如果有人知道我该如何实现,请发表评论。

虽然我自己对 Laravel 还很陌生,但我可以尝试帮助您解决这个问题。首先,在 Routes.php 文件中设置你的路线。例如

Route::get('myapp/sendEmail', 'EmailController@returnComposeEmail');
Route::post('myapp/sendEmail', 'EmailController@sendEmail');

访问时的第一个路由应该return 向用户显示他可以在其中撰写电子邮件的视图。这基本上是一个表单,当用户单击 'Send' 按钮时,将通过 POST 方法提交。第二条路线是用于收集提交的数据并适当地使用它然后发送电子邮件的方法。

如果您按照我提供的路线进行,您应该有一个名为 EmailController.php 的控制器文件,其方法如下:

public function returnComposeEmail()
{
return view('pages.ComposeEmail');
}
public function sendEmail(Request $input)
{
    $input = $input->all();
    $dataArray = array();
    $dataArray['emailBody'] = $input['emailBody'];
    $to = $input['to'];
    $subject = $input['subject'];
    Mail::send('email.body', ['dataArray' => $dataArray], function ($instance) use ($to, $subject)
        {
            $instance->from(env('MAIL_USERNAME'), 'Your Name Here');
            $instance->to($to, 'Recipient Name');
            $instance->subject($subject);
            $instance->replyTo(env('MAIL_REPLY_TO', 'some@email.id'), 'Desired Name');
        });
}

您可以按原样或根据您的要求使用 email/body.blade.php 文件中的 $dataArray

如果我能帮上忙,请告诉我。 :-)

应该和

一样简单
Mail::send([], array('yourValue' => $yourValue), function($message) use ($yourValue) {
    $MailBody = 'Your Custom Body';
    $message->setBody($MailBody, 'text/html');
    $message->to('yourtoaddress@yourdomain.com');
    $message->subject('Your Custom Subject');
    });

控制器:

    public function showForm(Request $request )
    {
        //Get Content From The Form
        $name = $request->input('name');
        $email = Input::get('agree');
        $message = $request->input('message');

        //Make a Data Array
        $data = array(
            'name' => $name,
            'email' => $email,
            'message' => $message
        );

        //Convert the view into a string
        $emailView = View::make('contactemail')->with('data', $data);
        $contents = (string) $emailView;

        //Store the content on a file with .blad.php extension in the view/email folder
        $myfile = fopen("../resources/views/emails/email.blade.php", "w") or die("Unable to open file!");
        fwrite($myfile, $contents);
        fclose($myfile);

        //Use the create file as view for Mail function and send the email
        Mail::send('emails.email', $data, function($message) use ($data) {
            $message->to( $data['email'], 'Engage')->from('stifan@xyz.com')->subject('A Very Warm Welcome');
        });
//        return view();
    }

路线:

Route::post('contactform', 'ClientsController@showForm');
Route::get('/', 'ClientsController@profile');

视图contactemail有要发送的数据,视图email我们是通过邮件功能发送的。当用户将数据放入表单时,由于以下代码行,该数据将保存在 email.blade.php 中:

//Convert the view into a string
        $emailView = View::make('contactemail')->with('data', $data);
        $contents = (string) $emailView;

        //Store the content on a file with .blad.php extension in the view/email folder
        $myfile = fopen("../resources/views/emails/email.blade.php", "w") or die("Unable to open file!");
        fwrite($myfile, $contents);
        fclose($myfile);