phpmailer using gmail smtp server can be easily set up with this quick guide from our experts.
At bobcares, we offer solutions for every query, large or small, as part of our server management services.
let’s see how our support team is ready to help customers with phpmailer using gmail smtp server.
how to send email with phpmailer using gmail smtp server
phpmailer is more popular than plain mail as it offers many extra features like attachments, encryption, authentication, etc. In addition, it simplifies the process of sending html emails.
Also, we don’t need to use a local mail server since it contains an integrated smtp client. it’s useful for platforms that don’t have a local mail server, like pantheon.
Although there are other alternative options, our support technicians recommend phpmailer over them. today we are going to take a look at how we can use phpmailer to send emails via gmail smtp.
Before we continue, here are some of the limitations of gmail smtp servers and how to deal with them:
- the number of recipients in a single email and the number of emails that can be sent each day are set to 500 emails and 500 recipients respectively in gmail. there is no way to exceed this restriction. however, we may send more than these limits by using a third party email delivery platform.
- If we exceed the above threshold, we will not be able to send messages for the next 24 hours. we may resume sending emails after the counter resets after the temporary suspension period ends.
- Third party apps/codes cannot send emails using our gmail account by default. Fortunately, we can alter this by changing some settings as seen in the next section.
how to enable third party apps/send code email in gmail
- Begin by going to google account security settings.
- Next, make sure two-step verification is turned off.
- Next, change the setting from less secure application to enable.
- After that, create an app password for the app or device. Google may ask us to take this extra step as part of the sign-in process for security reasons.
- finally, allow access to our google account through the new device/application.
how to install phpmailer
our support technicians offer two ways to install phpmailer:
- via composer:
In this method, add the following code to composer.json:
here, run the following command to install phpmailer:
Alternatively, we can also download phpmailer from this link and copy the contents of the folder to one of the include_path directories in the php configuration.
after that, add the following code to the php file to manually load each class file:
sample php code to send email via gmail smtp
here is the code we need to add to the php file through which we want to send the email. our support technicians would like to point out that we have to change the values based on our domain and usernames.
require ‘provider/autoload.php’;
$mail = new phpmailer(true); $mail->issmtp(); $mail->sender = “smtp”;
try { $mail->smtpdebug = 1; $mail->smtpauth = true; $mail->smtpsecure = “tls”; $mail->port = 587; $mail->host = “smtp.gmail.com”; $email->username = “[email protected]”; $mail->password = “your-gmail-password”;
//recipients $mail->ishtml(true); $mail->address(“recipient@domain”, “recipient-name”); $mail->setfrom(“[email protected]”, “name-from”); $mail->addreplyto(“reply-to@domain”, “reply-to-name”); $mail->addcc(“cc-recipient@domain”, “cc-recipient-name”); $mail->subject = “test email sent through gmail smtp server using php mailer”; $content = “<b>this is a test email sent through the gmail smtp server using the php</b> mail class.”;
//attachments $mail->addattachment(‘/var/tmp/file.tar.gz’); //add attachments $mail->addattachment(‘/tmp/image.jpg’, ‘new.jpg’); //optional name
//content $mail->msghtml($content); if(!$mail->send()) { echo “Error sending email.”; var_dump($mail); } else { echo “email sent successfully”; } }
With these new changes, we will be able to successfully send an email through gmail’s smtp server using phpmailer.
[need help with another query? we are available 24/7.]
conclusion
In short, our expert support engineers at bobcares demonstrated how to send an email with phpmailer using the gmail smtp server