【问题标题】:send a simple message to an email using laravel 4.2 and mailtrap使用 laravel 4.2 和 mailtrap 向电子邮件发送简单的消息
【发布时间】:2015-12-24 00:47:21
【问题描述】:

我正在尝试发送电子邮件并为此使用 mailtrap。我试图发送的电子邮件只是一个简单的消息,让我们说,“您已收到电子邮件!”但我似乎无法使用 laravel 4.2 来实现,这是我的 mail.php

return array(
    'driver' => 'smtp',
    'host' => 'mailtrap.io',
    'port' => 2525,
    'from' => array('address' => 'example@gmail.com', 'name' => 'SSIMS'),
    'encryption' => 'ssl',
    'username' => 'sadasdadsadadsad', //not my real username
    'password' => '341ssfsdf2342', //not my real password
    'sendmail' => '/usr/sbin/sendmail -bs',
    'pretend' => false,
 );

刚刚从mailtrap.io 复制了这个然后我不知道如何使用 laravel 发送电子邮件。问题是我没有发送任何视图,我只是想发送一些简单的消息,所以在 4.2 的文档中我看到有这个 Mail::raw() 方法所以我像这样使用它

然后当我尝试它时,我得到一个错误提示

调用未定义的方法 Illuminate\Mail\Mailer::raw()

这是处理它的控制器(我在这里省略了其他功能)

<?php

class POrder extends \BaseController
{

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        //
        $title = "Purchase Order Page";
        $modules = prchorder::lists('ModuleName','ModuleID');
        return View::make('ssims.purchaseorder_index',compact('title','modules'));
    }

    public function chkInput()
    {
        session_start();
        $getsCapt = $_SESSION["captcha"];

        $rules = array(
            'ModuleInfo'      => 'required',
            'Quantity'        => 'required|numeric|digits_between:2,5',
            'SchoolEmail'     => 'required|min:10|max:254|email',
            'SupplierEmail'   => 'required|min:10|max:254|email',
            'capt'            => 'required|numeric'
        );

        $messages = array(
            'ModuleInfo.required'   => 'Please Select a Module.',
            //'SchoolEmail.email'     => 'Please Enter a Valid Email for the School Email.',
            //'SupplierEmail.email'   => 'Please Enter a Valid Email for the Supplier Email.',
            'capt.numeric'          => 'CAPTCHA code must be a number.',
            'SchoolEmail.same'      => 'School Email cannot be same with the Supplier Email.',
            'SupplierEmail.same'    => 'Supplier Email cannot be same with the School Email.',
        );

        $validator = Validator::make(Input::all(), $rules, $messages);
        $uCapt = Input::get('capt');

        // process the inputs given by the user
        if ($validator->fails()) { 
            return Redirect::to('purchase_order')
                ->withErrors($validator)
                ->withInput(Input::except('password'));
        } else {
            if ($uCapt == $getsCapt) {
                $sEmail    = Input::get('SupplierEmail');

                //return Redirect::to('purchase_order');
                Mail::raw('Text to e-mail', function($message) {
                    $message->from('sample@gmail.com', 'Laravel');
                    $message->to('sample1@gmail.com')->cc('sample2@yahoo.com');
                });
            } else {
                return Redirect::to('purchase_order')
                    ->withErrors('CAPTCHA code does not match')
                    ->withInput(Input::except('password'));
            }

        }
    }

}

关于我可能做错了什么的任何想法?或者我怎样才能让它工作?谢谢

【问题讨论】:

  • 处理这个问题的控制器是什么样的?
  • 我将其添加到问题中。谢谢
  • 我不是 Laravel 4.2 的专家,但您确定 Mail::raw() 在该版本中受支持吗?查看文档,它只在 Laravel 5.0 及更高版本中出现,laravel.com/docs/4.2/mail
  • 哦,是的。我的错。我现在遇到了更多问题,哈哈 :)
  • 添加了一个使用Mail::send() 代替Mail::raw() 的示例。

标签: php email laravel laravel-4


【解决方案1】:

鉴于 Laravel 4.2 不支持 Mail::raw(),只需将其替换为 Mail::send(),如下所示:

Mail::send('emails.example', array('a_value' => 'you_could_pass_through'), function($message)
{
    $message->to('sample1@gmail.com', 'John Smith')->cc('sample2@yahoo.com')->subject('Example!');
});

在此示例中,emails.example 指的是电子邮件文件夹中名为 example 的视图以及您的其余视图。 array('a_value' =&gt; 'you_could_pass_through') 就是这样 - 一个将数据传入查看的数组。如果您没有要传递的数据,则只需使用array(),因为它是Mail::send() 的必需部分。

剩下的只是设置 to 和 cc 字段,from 信息已经来自你设置的mail.php 文件。

【讨论】:

  • 如果我在emails.example 中的视图出现问题,我会遇到一些问题。说Unsupported operand types 这是我认为的表格 9
  • 您应该为此创建一个问题并提供您正在使用的导致错误的代码。
  • @James 如何在不使用 4.2 中的 Mail::pretend() 的情况下发送电子邮件以进行本地开发?因为我使用的是 Windows。它抛出 Swift_TransportException “系统找不到指定的路径。]”。我猜这是因为我的配置 'driver' => 'sendmail', 'host' => 'localhost', 'port' => 25, 'sendmail' => '/usr/sbin/sendmail -bs'。
  • @MithunJack 您需要发布自己的问题,因为上述问题与 OP 完全不同。另外,你还在用 4.2 做什么,我们现在升级到 Laravel 7.X,充分利用新功能和安全更新并更新你的应用程序!
  • @James 是的.. 我能理解。我们通常在 5.8 工作。我在 4.2 中什么也没做,我也不想这样做。是客户的建议。他们只是希望我们在 4.2 中修改他们的流程。而已。感谢回复
猜你喜欢
  • 2017-07-16
  • 2020-02-16
  • 2021-03-27
  • 2020-08-26
  • 2017-11-06
  • 1970-01-01
  • 2020-08-14
  • 1970-01-01
  • 2019-02-08
相关资源
最近更新 更多