Have you ever wanted to spam your friends 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:
- A phone number
- 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.
- AT&T: [email protected] (SMS), [email protected] (MMS)
- T-Mobile: [email protected] (SMS & MMS)
- Verizon: [email protected] (SMS), [email protected] (MMS)
- Sprint: [email protected] (SMS), [email protected] (MMS)
- Xfinity Mobile: [email protected] (SMS), [email protected] (MMS)
- Virgin Mobile: [email protected] (SMS), [email protected] (MMS)
- Tracfone: [email protected] (MMS)
- Simple Mobile: [email protected] (SMS)
- Mint Mobile: [email protected] (SMS)
- Red Pocket: [email protected] (SMS)
- Metro PCS: [email protected] (SMS & MMS)
- Boost Mobile: [email protected] (SMS), [email protected] (MMS)
- Cricket: [email protected] (SMS), [email protected] (MMS)
- Republic Wireless: [email protected] (SMS)
- Google Fi (Project Fi): [email protected] (SMS & MMS)
- U.S. Cellular: [email protected] (SMS), [email protected] (MMS)
- Ting: [email protected]
- Consumer Cellular: [email protected]
- C-Spire: [email protected]
- Page Plus: [email protected]
The email will be formatted as 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 [email protected]
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:
- Allow less secure apps on your Gmail
- 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 setup 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' => '[email protected]',
'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('[email protected]', 'Subject', 'Message', ['from' => '[email protected]']);
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!