laravel發送email


 在經過一段時間的使用后,發現在項目中很多地方需要用到用戶驗證,以短信驗證和郵箱驗證為主流趨勢,小麥總結了如果在Laravel框架中實現發送郵件功能,以后會陸續更上如何實現短信驗證.....  

      

      

在.env文件下

 1.配置Laravel文件

     MAIL_DRIVER=smtp  //建議使用smtp方式

     MAIL_HOST=smtp.163.com  //建議使用163郵箱 QQ郵箱會有報錯

     MAIL_PORT=25        //smtp 默認為25   

     MAIL_USERNAME=null   //自己的163帳號

     MAIL_PASSWORD=null //客戶端密碼

     MAIL_ENCRYPTION=null

.

2.修改config/email.php文件中的 

            'from' => ['address' => null, 'name' => null],   //手冊上未提示,但實際應用中  如果為addredd=>null則報錯,需要天寫自己的163地址

 

3. 注冊163的郵箱並進行郵箱帳號設置  POP3/SMTP/IMAP都要開啟   並開啟授權碼並進行手機驗證

        

 

4.參照Laravel手冊里的郵件發送

                必須注意的是  在控制器引用郵件發送時  必須首先引用use  Mail

    ​    ​    ​    ​發送郵件測試

    ​    ​    ​    ​在路由里設置 

      

    ​    ​    ​    ​在控制器中寫入方法

                

    ​    ​    ​    ​    ​    ​    ​    ​    ​    ​    ​    ​其中

    ​    ​    ​    ​    ​    ​    ​    ​    ​    ​    ​    ​    ​1:Mail::raw  是發送原生數據,其他的內容發送方式在手冊里都有提供;

    ​    ​    ​    ​    ​    ​    ​    ​    ​    ​    ​    ​    ​2.$message->subjuet('');是文件的標題

    ​    ​    ​    ​    ​    ​    ​    ​    ​    ​    ​    ​    ​3.$message->to();發送給誰

    ​    ​    ​

這是一份在 $message 消息生成器實例中可以使用的方法清單:

$message->from($address, $name = null); $message->sender($address, $name = null); $message->to($address, $name = null); $message->cc($address, $name = null);//抄送 $message->bcc($address, $name = null); $message->replyTo($address, $name = null); $message->subject($subject); $message->priority($level); $message->attach($pathToFile, array $options = []);  // 以原始 $data 字符串附加一個文件... $message->attachData($data, $name, array $options = []);  // 獲取底層的 SwiftMailer 消息實例... $message->getSwiftMessage();
public function send(){ 
    $image = Storage::get('images/obama.jpg'); //本地文件
    //$image = 'http://www.baidu.com/sousuo/pic/sdaadar24545ssqq22.jpg';//網上圖片
    Mail::send('emails.test',['image'=>$image],function($message){ 
        $to = '123456789@qq.com';
        $message->to($to)->subject('圖片測試'); 
    }); 
    if(count(Mail::failures()) < 1){
     echo '發送郵件成功,請查收!'; 
    }else{
     echo '發送郵件失敗,請重試!';
    } 
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM