zoukankan      html  css  js  c++  java
  • Laravel邮件发送问题小解

      在Laravel中已经有内置了发送邮件的功能,通过 Mail::send 可发送邮件,但要使用这个函数必须先进行相关配置。

      在 /app/config/mail.php 中设置你的邮件参数,如下:

      1   1 <?php
      2   2 return array(
      3   3 
      4   4     /*
      5   5     |--------------------------------------------------------------------------
      6   6     | Mail Driver
      7   7     |--------------------------------------------------------------------------
      8   8     |
      9   9     | Laravel supports both SMTP and PHP's "mail" function as drivers for the
     10  10     | sending of e-mail. You may specify which one you're using throughout
     11  11     | your application here. By default, Laravel is setup for SMTP mail.
     12  12     |
     13  13     | Supported: "smtp", "mail", "sendmail", "mailgun", "mandrill", "log"
     14  14     |
     15  15     */
     16  16 
     17  17     'driver' => 'smtp',
     18  18 
     19  19     /*
     20  20     |--------------------------------------------------------------------------
     21  21     | SMTP Host Address
     22  22     |--------------------------------------------------------------------------
     23  23     |
     24  24     | Here you may provide the host address of the SMTP server used by your
     25  25     | applications. A default option is provided that is compatible with
     26  26     | the Mailgun mail service which will provide reliable deliveries.
     27  27     |
     28  28     */
     29  29 
     30  30     'host' => 'smtp.163.com',
     31  31 
     32  32     /*
     33  33     |--------------------------------------------------------------------------
     34  34     | SMTP Host Port
     35  35     |--------------------------------------------------------------------------
     36  36     |
     37  37     | This is the SMTP port used by your application to deliver e-mails to
     38  38     | users of the application. Like the host we have set this value to
     39  39     | stay compatible with the Mailgun e-mail application by default.
     40  40     |
     41  41     */
     42  42 
     43  43     'port' => 25,
     44  44 
     45  45     /*
     46  46     |--------------------------------------------------------------------------
     47  47     | Global "From" Address
     48  48     |--------------------------------------------------------------------------
     49  49     |
     50  50     | You may wish for all e-mails sent by your application to be sent from
     51  51     | the same address. Here, you may specify a name and address that is
     52  52     | used globally for all e-mails that are sent by your application.
     53  53     |
     54  54     */
     55  55 
     56  56     'from' => array (
     57  57                 'address' => 'xxx@163.com',
     58  58                 'name' => 'xxx@163.com'
     59  59         ),
     60  60 
     61  61     /*
     62  62     |--------------------------------------------------------------------------
     63  63     | E-Mail Encryption Protocol
     64  64     |--------------------------------------------------------------------------
     65  65     |
     66  66     | Here you may specify the encryption protocol that should be used when
     67  67     | the application send e-mail messages. A sensible default using the
     68  68     | transport layer security protocol should provide great security.
     69  69     |
     70  70     */
     71  71 
     72  72     'encryption' => 'tls',
     73  73 
     74  74     /*
     75  75     |--------------------------------------------------------------------------
     76  76     | SMTP Server Username
     77  77     |--------------------------------------------------------------------------
     78  78     |
     79  79     | If your SMTP server requires a username for authentication, you should
     80  80     | set it here. This will get used to authenticate with your server on
     81  81     | connection. You may also set the "password" value below this one.
     82  82     |
     83  83     */
     84  84 
     85  85     'username' => 'xxx@163.com',
     86  86 
     87  87     /*
     88  88     |--------------------------------------------------------------------------
     89  89     | SMTP Server Password
     90  90     |--------------------------------------------------------------------------
     91  91     |
     92  92     | Here you may set the password required by your SMTP server to send out
     93  93     | messages from your application. This will be given to the server on
     94  94     | connection so that the application will be able to send messages.
     95  95     |
     96  96     */
     97  97 
     98  98     'password' => 'xxx',
     99  99 
    100 100     /*
    101 101     |--------------------------------------------------------------------------
    102 102     | Sendmail System Path
    103 103     |--------------------------------------------------------------------------
    104 104     |
    105 105     | When using the "sendmail" driver to send e-mails, we will need to know
    106 106     | the path to where Sendmail lives on this server. A default path has
    107 107     | been provided here, which will work well on most of your systems.
    108 108     |
    109 109     */
    110 110 
    111 111     'sendmail' => '/usr/sbin/sendmail -bs',
    112 112 
    113 113     /*
    114 114     |--------------------------------------------------------------------------
    115 115     | Mail "Pretend"
    116 116     |--------------------------------------------------------------------------
    117 117     |
    118 118     | When this option is enabled, e-mail will not actually be sent over the
    119 119     | web and will instead be written to your application's logs files so
    120 120     | you may inspect the message. This is great for local development.
    121 121     |
    122 122     */
    123 123 
    124 124     'pretend' => false 
    125 125 );
    View Code

      其中的 driver、host、port 和 encryption 等自己可以根据相应邮件服务器的属性来设置。

      在 Mail::send 中,需要注意其中的参数,如下:

     1 if (!$this->postUsernameCheck() && !$this->postEmailCheck())
     2         {
     3             $pwd = User::where('username', Input::get('username'))->where('email', Input::get('email'))->get();
     4             $msg = Input::get('username') . " , your password is : " . Hash::make($pwd[0]->password);
     5             $messageData = array('msgnext' => $msg);
     6             $emails = 'Passdd';
     7         
     8             Mail::send('emails.view', $messageData, function ($message) use ($pwd,$emails) {
     9                 $message->to($pwd[0]->email);
    10                 $message->subject($emails);
    11             });
    12 
    13             return Redirect::to('main')->with('user' , Input::get('username'));
    14         }
    15         else 
    16             return Redirect::back();

      send 的第一个参数是view的页面,你可以在view文件夹里添加blade文件,输入相应的代码来显示的数据,我的页面是 {{ $msgnext }};

      第二个参数是一维数组,不可是其他多维的;

      第三个参数是匿名函数,变量名不可与第二个参数一样,可用 use 来连接函数外部的变量。

      这样你的邮件就想发就发了!

  • 相关阅读:
    4.父类私有属性和方法
    3.单继承和方法的重写
    2.Python封装练习及私有属性
    1.Python面向对象基础
    6.类与对象、封装、构造方法
    5.数组
    4.方法
    3.流程控制语句
    结构型模式和物理视图,模型管理试图的总结
    创建型模式和关于状态视图、活动视图、交互视图的总结
  • 原文地址:https://www.cnblogs.com/linguoguo/p/4032616.html
Copyright © 2011-2022 走看看