Hi, In this blog post, we will send an email from a business email using PHP script. We will use phpmailer library to send emails. We will send HTML document that contains text, images, buttons, and links as email content. We will also send an attachment with our email. Send emails from a Gmail account using any third-party app is now restricted by Google. But you can use Gmail cloud API for that.
Add PHPmailer to project
Here, we will use phpmailer library that helps us in sending emails safely from the web server. Download the library from GitHub and add it to your project. You can also use the CMD command composer require phpmailer/phpmailer and download the library using composer. This method is recommended, convenient, and reliable. Here, we are downloading from the composer. After downloading, the following files will be added to our project folder.
Start with sample code
Start PHP code from the given sample on the GitHub page. Create a PHP file and add the sample code to that. Now, we need to make some changes to the code for server settings, adding content, recipients, and attachments. First, discover the server settings for your email on cPanel.
Get email config settings from Cpanel
Open the cPanel of your web server. Go to Email Accounts in the Email tab. Select an email if you already created it or create a new one. Click on the Connect Devices button, then given below box will be shown with your email settings.
Make changes to sample code
Now, we will do some changes to the sample code to make it useful for us.
In the server settings, first, we will add the outgoing hostname of the email. Here, any.yourdomain.com is the outgoing hostname. Add it as a host in the PHP code. The username is the email and the password is the email's password that is set during email creation. You can change the email's password at any time by clicking the Manage button on the email page of cPanel. Also, add the SMTP port of the email given above.
Pass two parameters to the setFrom() function, one for the email address of the sender also used as the username above, and another one for the sender name. Provide the receiver's email address and name to the addAddress() function as parameters.
Add email content
Now, add the email subject and content that you want to send through email. You can pass HTML content to the msgHTML() function or create a separate file for the email template and fetch content using file_get_contents() function. It's a recommended way. Also, add non-HTML content as AltBody, for clients that don't have support for HTML emails. You can create an email template from unlayer.
We can also attach files with our email using addAttachment() function. Make sure, the file is available on the server. Give the complete path of the file to the function and you can also provide an optional file name.
Sending emails using a web server is very important for a web developer. It is mostly used for communicating with clients, giving confirmation on forms submitted, and also for OTP verification. If you don't have a professional business email, then you can use your Gmail account as the sender using Gamil Cloud API. Because we cannot send emails from the Gmail account by using just a username and password after Google update on May 30, 2022. Get the complete source code given below, and comment, if you have any issue or suggestion.