简体   繁体   中英

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

Im trying to send an email and im using mailtrap for this. the email im trying to send is just a plain messgae let's say , "You have been emailed!" but i can't seem to make it using laravel 4.2 here is my 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,
 );

just copied this from mailtrap.io then i don't know how to send the email using laravel. The thing is im not sending any views im just trying to send some simple message so in the documentation of 4.2 i saw that there is this Mail::raw() method so i used it like this

then when i tried it i got an error saying

Call to undefined method Illuminate\\Mail\\Mailer::raw()

here is the controller that handles it (i omitted the other functions here)

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

        }
    }

}

Any ideas on what i may be doing wrong? or how can i make it work? thanks

Given that Laravel 4.2 doesn't support Mail::raw() , simply replace it with Mail::send() like so:

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

In this example, emails.example refers to a view called example in an emails folder with the rest of your views. array('a_value' => 'you_could_pass_through') is simply that - an array that passes data in to view. If you have no data you want to pass in then simply use array() as it is a required part of Mail::send() .

The rest is just setting the to and cc fields, the from information will already come form your mail.php file which you have set up.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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