Tag: development

Mailgun “On behalf of” issue

Development

We all know the feeling when we release a new project, we are overexcited until the first email goes out…
All of a sudden a client informs you, “Hey, the email I received has a strange “from” address. Can you fix it?“…

What is happening?

Even though we’ve registered our domain with Mailgun, made sure the correct DNS records are in place. And in our application, we’re using the correct API key combined with the domain and optionally the specific server that we prefer.

The “from” address in the email appears to be info=example.com@mg.example.com on behalf of info@example.com. which isn’t the address we wanted it to be, ideally it should be info@example.com

Mail::send(['html' => 'emails.template'], $data, function($message) use ($subject, $user){
    $message->to($user->email, $user->name);
    $message->subject("$subject");
    $message->from("info@example.com");
});

This issue is caused by the fact that we are using another domain, a subdomain, to configure Mailgun. This is advised, otherwise, we would endanger the normal mail functionality of the domain, if there are any in place, such as Google Workplace or Office 365.

It is the mismatch between the domain used to send our email (mg.example.com) and the domain indicated as the “from” address in our app (example.com).

Mailgun’s suggestion

Mailgun suggests a solution on its website. You can find it here.
The suggested solution may endanger the normal mail functionality as described above. Because we would overwrite the SPF record on the main domain. Which isn’t ideal.

A quick and easy solution

In Laravel, we can specify the sending domain in the email headers. For this, we can use the ->sender attribute.

Mail::send(['html' => 'emails.template'], $data, function($message) use ($subject, $user){
    $message->to($user->email, $user->name);
    $message->subject("$subject");
    $message->from("info@example.com");
    $message->sender("info@example.com");
});

By doing so we enforce the email sending address and the visible “from” address to be the same.

There, the job is done. Happy customer.

Share this post

Custom Commands & Scheduled Tasks in Laravel

Development

After building a quite large application that was all about automating several tasks during the day, week and month I thought why don’t I create a tutorial covering this subject.

In this tutorial we go over the the basics of a custom command and level up the game with scheduling this custom command into a task, that runs at certain moments in time.

Share this post

What if a scheduled task fails

Development

Sometimes scheduled tasks fail. It’s happening more often than we think.
By adding “emailOutputOnFailure” to your scheduled task you’ll be notified with the output when it fails.

Additionally I like to add “withoutOverlapping” to it, just in case the task takes longer than expected.

I’ve also tweeted about this:

Share this post

How to get started with Laravel Valet

Development

Tired of the localhost setup like MAMP, WAMP or XAMP?
Tired of the configuration, ‘etc/hosts’, and stuff like that.

We’ve been experimenting with Laravel Valet for the last months now and decided not to go back.
Its super easy, fast and reliable.

Installation

Before you start, make sure no other service is using port 80.

We’ll get started with installing Homebrew, the latest version of PHP (7.4 at this moment) and Composer

– You can update your Homebrew with “brew update”
– Installing the latest version of PHP using Homebrew : “brew install php”

We’re going to install Valet globally to our machine so we’ll use the following command.

composer global require laravel/valet

Once installed we can run valet install

Defining the working directory

We need to attach Valet to an directory where we’ll keep our projects.
When navigated to the directory of your choice we can initiate this directory as the Valet directory by using the Park Command.

valet park

Once run, we can create new application projects in this directory.
The directory name of the project will also be the url for Valet. So for example is I have a directory called “pizzawebsite” I can use the URL http://pizzawebsite.test it’s as easy as that…

Sharing projects

It’s even possible to share a project with your team of client.
Thanks to the valet share command a publicly accessible url will be created using Ngrok.

Only Laravel projects? No, many more.

For those of thought Laravel Valet is only for Laravel projects… Nope !
It can be used for Laravel, Lumen, October CMS, Drupal, Craft CMS, Magento, WordPress and many more.

Share this post

Laravel – Artisan – Cheat Sheet

Development

Here’s an overview of what you could use while developing a project with Laravel.
In order to create controllers, models, migrations, … we can use artisan.

Creating a Controller

php artisan make:controller MyController

When we want artisan to provide a controller with the “resource functions” (index, create, store, show, edit, update, destroy) predefined for us, we can simply add “–resource” to our command to provide these functions out of the box.

php artisan make:controller MyController --resource

Creating a Model

php artisan make:model

If you want artisan to provide a migration file related to our newly created model, you can just add “-m” to the command. Which gives us something like this:

php artisan make:model -m

Creating a Migration file

We’re also able to create our own migration file, for example is we want to add field to an existing table or create a pivot table, or …

php artisan make:migration the_name_of_your_migration

Just like the migration file that artisan provided for us when we used -m on the creation of a model, the migration file will be added to the /database/migrations folder.

Deploy the migration file(s) to the database

In order to get our migration file(s) migrated into the database, we need to tell artisan to migrate them. Like so:

php artisan migrate

Be aware that once a migration file has been migrated to the database, all changes to the file afterwards will be discarded. We’ll need to create a new migration file in order to add additional changes / migrations. In the database you can see the hierarchy of the migrations in the table migrations.

Share this post

Getting started with Grunt and Sass

Development

This year I’ve started working with some new tools, two of them are Grunt and Sass. Grunt is a commandline JavaScript Task Runner, and Sass is a Css preprocessor allowing you to write more powerful css, with the use of variables, mixins/functions and nesting!

Off we go !

First of all you’ll need to install a few things. Ruby to get Sass, and NodeJs to get npm, so we can work with Grunt.

Grunt

Step one: installing Grunt. If you have NodeJs installed properly you simply run: npm install -g grunt-cli

This will install Grunt globally. So you can use it wherever you want.

Now we need to create the “package.json” file there are several ways to do this. But I prefer to create a new file and input this.

{
  "name": "Project Name",
  "version": "0.0.1",
  "devDependencies": {
     //installed packages 
  }
}

And then save it as “package.json” in the root of my project.

After that we need to install our Dependencies. This can be done by running the following command: npm install <package> --save-dev

I want to use Grunt to compile my Sass when I save my project. To do this I need to install 2 dependencies.

grunt-contrib-sass: It is used to actually compile your scss files to css.

grunt-contrib-watch: It watches our folders for file changes.

Example

npm install grunt-contrib-sass --save-dev

Now my “package.json” looks like this:

"devDependencies": {
    "grunt": "~0.4.1",
    "grunt-contrib-sass": "~0.3.0",
    "grunt-contrib-watch": "~0.4.4"
}

Next is our “Gruntfile.js”. This file contains the tasks that grunt needs to do for you.

This is how my Gruntfile looks like:

module.exports = function(grunt) {
    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        sass: {
            dist: {
                files: {
                    'style/style.css' : 'sass/style.scss'
                }
            }
        },
        watch: {
            css: {
            files: '**/*.scss',
            tasks: ['sass']
            }
        }
    });
    grunt.loadNpmTasks('grunt-contrib-sass');
    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.registerTask('default',['watch']);
}

This file starts with a wrapper function and we pass in grunt so that we can access it..

Adding tasks with :

grunt.loadNpmTasks('*Name of the task*')

Register Tasks with :

grunt.registerTask('default',['*tasks to run*']);

the 'default' task is what gets running when you type grunt in the commandline.

The end !

When working on your project, just run the grunt command in your commandline in your project folder.

Hit ctrl+c to quit grunt when you’re done.

Share this post