Sending Text Messages With CakePHP | CakePHP blog

Have you ever wanted to spam your friend's phone with endless photos of cats, but felt like you didn’t have the time? Well, my devious friend, I’ll show you how to do that, and more, with the power of CakePHP and email!

Sending Text From Email

For those of you that don’t know, you can actually send text messages from your email! To do that you’ll just need 2 things:

  1. A phone number
  2. The provider for that phone number

If you have both of these, then refer to the list below to get the domain to send the email.

The email will be formatted as a phone number first, then provider extension. If my phone number was (555)555–5555 and my service provider was Google Fi, and you wanted to annoy me with endless quotes from the Bee Movie, then you would simply email me at 5555555555@msg.fi.google.com

Pretty neat, eh? But why stop there! We’re programmers after all, if we can automate a task as critical as annoying our friends then that will become a crucial task! CODE REVIEWS BE DAMNED we’re annoying our friends.

Sending Email With CakePHP

Before we jump into the code we’ll have to do some setup with our email account. I’ll assume you’re using a Gmail account. If you’re not using a Gmail account, then refer to the documentation on allowing apps to send emails from your account and the default host and port for configuration.

Giving Your App Permission to Send Emails Through Your Gmail

Giving permission to send emails to your app is a straight forward process all you have to do is:

  1. Allow less secure apps on your Gmail
  2. Get the SMTP configuration for Gmail:
host: ssl://smtp.gmail.com 
port: 465

After you do those 2 steps you’ll be able to send emails straight from whatever application you want! Now let’s set up CakePHP to send emails.

Setting Up Our CakePHP Config

Go ahead and run the composer script to set up your initial app. If you’re new to CakePHP there are plenty of great resources to get started

composer create-project --prefer-dist cakephp/app:4.* texting

Navigate to your config/app.php, and add your credentials.

'EmailTransport' => [
'default' => [
'className' => 'Smtp',
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'timeout' => 30,
'username' => 'username@domain.com',
'password' => 'w0ULDn\\'7-Y0U-LiK3-70-kN0W',
],
],

If you wanted to go one step further you could send text messages with your voice.

Now that we can send emails from our app all we need to do is actually send emails!

Sending Emails

You can either send emails through an API or web interface. Either way, you do it you’ll need to add the following code to your controller class. If it’s part of a larger application providing a full suite of tools to annoy people you can even add it to your components, or commands.

use Cake\\Mailer\\Email;
//...
Email::deliver('5555555555@msg.fi.google.com', 'Subject', 'Message', ['from' => 'username@domain.com']);

You can get creative and add custom email templates with variables, and complex layouts, but that those two lines of code are the only two you need to send emails quickly!

Conclusion

What did you think of the tutorial? If you liked it, or have any comments, questions, concerns then comment below!

Don’t forget to subscribe to the blog for new CakePHP content twice a week!

Originally published at https://cakephp.blog on November 3, 2020.

--

--