如何使用 Laravel 制作发送电子邮件表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47028726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:53:14  来源:igfitidea点击:

How to make a send email form using laravel

phplaravelemailsmtp

提问by anonymous

Route:

路线:

Route::get('sendemail', function () {

    $data = array(
        'name' => "Learning Laravel",
    );

    Mail::send('AltHr/Portal/welcome', $data, function ($message) {

        $message->from('[email protected]', 'John Doe');

        $message->to('[email protected]')->subject('Alt Support');

    });

    return "Your email has been sent successfully";

});

Welcome.blade.php:

欢迎.blade.php:

<html>
<head>
    <meta charset="utf-8">
</head>
<body>

<div>
    Hello Idris test test
</div>

</body>
</html>

Hey guys so i have set up my config/mail.php file and my .env file and i am using sendgrid smtp to send email.

嘿伙计们,我已经设置了我的 config/mail.php 文件和我的 .env 文件,我正在使用 sendgrid smtp 发送电子邮件。

The code above is a simple code that i manage to so when i open my browser to : localhost/sendemail it sends an email directly to my email stated in the code.

上面的代码是我设法使用的简单代码,因此当我打开浏览器到:localhost/sendemail 时,它会直接向代码中指定的我的电子邮件发送一封电子邮件。

Now im trying to create a form to send that email basically a contact me form like this

现在我正在尝试创建一个表格来发送该电子邮件,基本上是一个像这样的联系我表格

<form role="form" class="m-t-15">
  <div class="form-group form-group-default">
    <label>Full Name*</label>
    <input type="text" placeholder="As per IC" class="form-control" required>
  </div>
  <div class="form-group form-group-default">
    <label>Company*</label>
    <input type="text" placeholder="Company name" class="form-control" required>
  </div>
  <div class="form-group form-group-default">
    <label>Email*</label>
    <input type="email" placeholder="Company email preferred" class="form-control" required>
  </div>
      <div class="form-group form-group-default">
                  <label>Category</label>
                  <select class="full-width form-control">
                    <option value=""></option>
      <option value="1">Sign Up</option>
      <option value="2">Onboarding</option>
                  </select>
              </div>
  <div class="form-group form-group-default">
    <label>Message*</label>
    <textarea placeholder="Please type your message here" style="height:100px" class="form-control" required></textarea>
  </div>
  <div class="form-group form-group-default">
    <label>Attachment</label>
    <input type="file" name="pic" accept="file_extension|image/*|media_type">
  </div>

  <div class="sm-p-t-10 clearfix">
    <p class="pull-left small hint-text m-t-5 font-arial">*indicates required field</p>
    <button class="btn btn-primary font-montserrat all-caps fs-12 pull-right xs-pull-left">Submit</button>
  </div>
  <div class="clearfix"></div>
</form>

So i have created the form now i want to know how i can i do so that the details will be dynamic and not hardcoded in the route file

所以我现在已经创建了表单我想知道我该怎么做才能使详细信息是动态的而不是硬编码在路由文件中

How can i do that?

我怎样才能做到这一点?

回答by madalinivascu

Change your route to post

更改您的发布路线

Route::post('sendemail', function (Request $request) {

    $data = array(
        'name' => $request->name,
         'mail'=>$request->mail,
          'message'=>$request->message,
         'category'=>$request->category,
         'company'=>$request->company
    );

    Mail::send('AltHr/Portal/welcome', $data, function ($message) use($request) {

        $message->from($request->mail,$request->name);

        $message->to('[email protected]')->subject('Alt Support');

    });

    return "Your email has been sent successfully";

});

change form to:

将表格更改为:

<form role="form" action={{route('sendemail')}} method="post" class="m-t-15">
  <div class="form-group form-group-default">
    <label>Full Name*</label>
    <input type="text" name="name" placeholder="As per IC" class="form-control" required>
  </div>
  <div class="form-group form-group-default">
    <label>Company*</label>
    <input type="text" name="company" placeholder="Company name" class="form-control" required>
  </div>
  <div class="form-group form-group-default">
    <label>Email*</label>
    <input type="email" name="mail" placeholder="Company email preferred" class="form-control" required>
  </div>
      <div class="form-group form-group-default">
                  <label>Category</label>
                  <select name="category" class="full-width form-control">
                    <option value=""></option>
      <option value="1">Sign Up</option>
      <option value="2">Onboarding</option>
                  </select>
              </div>
  <div class="form-group form-group-default">
    <label>Message*</label>
    <textarea name="message" placeholder="Please type your message here" style="height:100px" class="form-control" required></textarea>
  </div>
  <div class="form-group form-group-default">
    <label>Attachment</label>
    <input type="file" name="pic" accept="file_extension|image/*|media_type">
  </div>

  <div class="sm-p-t-10 clearfix">
    <p class="pull-left small hint-text m-t-5 font-arial">*indicates required field</p>
    <button class="btn btn-primary font-montserrat all-caps fs-12 pull-right xs-pull-left">Submit</button>
  </div>
  <div class="clearfix"></div>
</form>

your email blade

你的电子邮件刀片

<html>
<head>
    <meta charset="utf-8">
</head>
<body>

<div>
  {{name}}
{{mail}}
{{message}}
{{category}}
{{company}}
</div>

</body>
</html>

To attach the file use $message->attach($pathToFile);

附加文件使用 $message->attach($pathToFile);

more info at:https://laravel.com/docs/5.1/mail

更多信息请访问:https: //laravel.com/docs/5.1/mail