使用laravel 4.2和mailtrap向电子邮件发送简单消息

时间:2015-09-26 07:40:00

标签: php email laravel laravel-4

我正在尝试发送电子邮件,即时通讯使用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'));
        }

    }
}

}

关于我可能做错的任何想法?或者我怎样才能使它发挥作用?感谢

1 个答案:

答案 0 :(得分: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' => 'you_could_pass_through')就是这样 - 一个将数据传递给视图的数组。如果您没有要传入的数据,请使用array(),因为它是Mail::send()的必需部分。

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