Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Custom email configurations in CakePHP 3.x

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 400
    Comment on it

    Welcome to FindNerd. We are going to discuss the Email settings in CakePHP 3.x. We have already discussed the different methods available in Email class in our previous blog. Click here  to check the blog. We have also mentioned examples how we can use these methods for different purposes. If you check the file config/app.default.php then you can see email transport settings in it. Please have a look.

     

    'EmailTransport' => [
            'default' => [
                'className' => 'Mail',
                // The following keys are used in SMTP transports
                'host' => 'localhost',
                'port' => 25,
                'timeout' => 30,
                'username' => 'user',
                'password' => 'secret',
                'client' => null,
                'tls' => null,
                'url' => env('EMAIL_TRANSPORT_DEFAULT_URL', null),
            ],
        ],

     

    It is not necessary to add these settings in default config file. You can use the email methods by loading the config array. If you want to use the above set configurations then you need to use the constructor of Email class or method named profile() to load the predefined settings. Please have a look.

     

    // Or in constructor
    $findnerd_email = new Email('default');
    
    // with profile method
     $findnerd_email = new Email();
    $findnerd_email->profile('default');

     

    You can see in above code. We have loaded the settings using constructor as well as profile method.

    You can set your custom email settings in config/app.default.php file, if you want to set your own settings. Please have a look. 

     

    $findnerd_email = new Email();
    $findnerd_email->profile(['from' => 'support@findnerd.com', 'transport' => 'custom_email']);
    // Or in constructor
    $findnerd_email = new Email(['from' => 'support@findnerd.com', 'transport' => 'custom_email']);

     

    We created a custom settings named custom_email and loaded using profile/constructor methods. In version 3.1 you do not need to mention the default string at the time of loading the settings. It will load the default settings automatically if you don't pass anything.

    If you want to set the email transport settings outside the app.default.php file then you can set the settings like this

     

    use Cake\Mailer\Email;
    // Sample Mail configuration
    Email::configTransport('default', [
    'className' => 'Mail'
    ]);
    // Sample smtp configuration.
    Email::configTransport('gmail', [
    'host' => 'ssl://smtp.gmail.com',
    'port' => 465,
    'username' => 'admin@findnerd.com',
    'password' => 'findnerd',
    'className' => 'Smtp'
    ]);

     

    In above code we have mentioned both types of settings. one for simple mail configuration and other is SMTP settings. Here we are using Gmail SMTP so you need to enable the less secure app  by login in your Gmail account.

    Thank you for being with us!

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: