簡體   English   中英

使用 laravel 4.2 和 mailtrap 向電子郵件發送一條簡單的消息

[英]send a simple message to an email using laravel 4.2 and mailtrap

我正在嘗試發送電子郵件並為此使用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() ,只需將其替換為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指的是一個在 emails 文件夾中名為 example 的視圖,其中包含你的其余視圖。 array('a_value' => 'you_could_pass_through')就是這樣 - 一個將數據傳遞給視圖的數組。 如果您沒有要傳入的數據,那么只需使用array()因為它是Mail::send()的必需部分。

剩下的只是設置 to 和 cc 字段,from 信息已經來自您設置的mail.php文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM