简体   繁体   English

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

[英]how to edit email body before sending using Laravel

I have generated email function, that sends email to multiple people using laravel. 我已经生成了电子邮件功能,可以使用laravel将电子邮件发送给多个人。

Now I want to generate an edit window so that i can write the body of email,like in Gmail, if we are sending mail, we first edit body and hit send mail. 现在,我想生成一个编辑窗口,以便我可以像在Gmail中那样编写电子邮件的正文,如果我们要发送邮件,我们首先要编辑正文,然后单击“发送邮件”。

So, if anyone know how can I implement this, leave a comment. 因此,如果有人知道我该如何实施,请发表评论。

It should be as simple as 它应该像

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');
    });

Though I am fairly new to Laravel myself, I could try to help you out with this. 尽管我自己对Laravel并不陌生,但我可以尝试帮助您。 Firstly, set up your routes in the Routes.php file. 首先,在Routes.php文件中设置您的路线。 For eg 例如

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

The first route when visited should return a view to the user where he can compose his email. 访问时的第一条路线应将视图返回给用户,以便用户撰写电子邮件。 This basically is a form which will be submitted by the POST method when the user clicks the 'Send' button. 这基本上是一种表单,当用户单击“发送”按钮时将由POST方法提交。 The second route is for that method which would collect the submitted data and use it appropriately then to send the email. 第二种途径是该方法,该方法将收集提交的数据并适当地使用它然后发送电子邮件。

If you go by the routes I have provided, you should have a controller file named EmailController.php with the following methods: 如果按照我提供的路线进行操作,则应该使用以下方法创建一个名为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');
        });
}

You may use use the $dataArray in the email/body.blade.php file as is or as per your requirement. 您可以按原样或根据需要使用email/body.blade.php文件中的$dataArray

Do let me know if I could be of help. 让我知道我是否可以帮忙。 :-) :-)

Controller: 控制器:

    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();
    }

Routes: 路线:

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

The view contactemail has the data to be sent, and the view email we are sending through mail function. 视图contactemail具有要发送的数据,以及我们正在通过邮件功能发送的视图电子邮件 When the user puts data in the form, that data will get saved in email.blade.php because of these lines of code: 当用户将数据放入表单时,由于以下行代码,该数据将保存在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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM