How to install phpMailer
in a shared hosting environment? I need to use this for email verifications and password changing of the users.
Asked
Active
Viewed 1.8k times
7

Dumb Question
- 365
- 1
- 3
- 14
1 Answers
3
You can download it here: https://github.com/PHPMailer/PHPMailer
Upload the folder to your server and include the main file with this line:
<?php
require 'phpmailerfolder/PHPMailerAutoload.php';
?>
After that, you will need an external SMTP account, like gmail.com for example. Here is a working example of PHPMailer with GMAIL:
<?php
require 'phpmailerfolder/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->Debugoutput = 'html';
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "youremail@gmail.com";
$mail->Password = "yourpassword";
$mail->setFrom('youremail@gmail.com', 'Your Name');
$mail->addAddress('to@site.com', 'To');
$mail->Subject = "Subject";
$mail->Body = "Message";
if (!$mail->send()) {
echo "Error sending message";
} else {
echo "Message sent!";
}
?>
Make sure to enable "non secure apps" in this GMAIL account too: https://support.google.com/accounts/answer/6010255?hl=en

bruno
- 151
- 9
-
can I use the email account from the website? like `emailname@domainname.com` ? – Dumb Question Nov 26 '16 at 00:15
-
Yes, you can use any SMTP you want, just replace the smtp.gmail with the smtp and information from your server. – bruno Nov 26 '16 at 00:18
-
btw, the installation process there is like composer? json? should I just ignore it and follow what you said? – Dumb Question Nov 26 '16 at 00:18
-
The only installation process is to upload the files from github, there are nothing to do anymore after that. – bruno Nov 26 '16 at 00:20
-
Thanks, I will message you once I know the SMTP and the port(they're support is off at the moment) and can I test this in `localhost` ? – Dumb Question Nov 26 '16 at 00:28
-
Ok. I never tried in localhost but the PHPMailer uses a remote server to send messages, so, trying in localhost will probably work too. – bruno Nov 26 '16 at 01:24
-
Thanks, it worked but, it says that Gmail couldn't verify that website.com actually sent this message. – Dumb Question Nov 26 '16 at 23:21
-
and I used this code, https://github.com/PHPMailer/PHPMailer/wiki/Tutorial, because I do not know what will I type in `Username` & `Password` – Dumb Question Nov 26 '16 at 23:23
-
I tried your code for gmail, error sending message. – Dumb Question Nov 26 '16 at 23:30
-
Do you enabled your gmail account to allow less secure apps? https://support.google.com/accounts/answer/6010255?hl=en – bruno Nov 26 '16 at 23:31
-
Yes it worked now, but when I tried it with my domain address, I saw this every time, `gmail couldn't verify that website.com actually sent this message (and not a spammer)`. – Dumb Question Nov 26 '16 at 23:38
-
Oh, if you are going to use your domain, you will need to modify smtp.gmail to smtp.yourdomain, check with your host the configuration of: SMTP, SMTP Port (587 / 25 or other) authentication (yes or no), and inform all these in the script. – bruno Nov 26 '16 at 23:42
-
what do I type in `username` and `password`? my cPanel account? – Dumb Question Nov 27 '16 at 00:16
-
No, you will put youremail@yourdomain.com and the password is the password to access this e-mail. – bruno Nov 27 '16 at 00:22
-
It worked but, there is still the red question mark appearing on the mail. – Dumb Question Nov 27 '16 at 00:35
-
Have you changed the $mail->setFrom(''); to youremail@yourserver.com too? You will need to change this too, if yes, paste a print screen of imgur.com please. – bruno Nov 27 '16 at 00:37
-
Oh, I see. This looks like something related to DKIM, take a look here: https://productforums.google.com/forum/#!topic/gmail/pqx8Wq_Csxw , this happens because Google made some changes to combat spam. – bruno Nov 27 '16 at 02:05
-
You need to implement DKIM and SPF to validate your domain for Google and other services, you can find how to do it here: http://stackoverflow.com/questions/24463425/send-mail-in-phpmailer-using-dkim-keys – bruno Nov 27 '16 at 03:07
-
Where can I get a key? – Dumb Question Nov 27 '16 at 03:43
-
I don't know, I never used PHPMailer with a custom domain name, I use only with gmail. You will need to check the DKIM with your host, the SPF records you can see in the DNS zone or Cloudflare (if your domain is on Cloudflare). – bruno Nov 27 '16 at 17:01
-
Thanks @bruno, I will find it. – Dumb Question Nov 28 '16 at 00:07
-
2I can't find PHPMailerAutoload.php in the version 6.x zip download, I assume because this file is meant to be created by composer. However if I run composer with the composer.json present there are 7 dependancies in the require-dev section - is there an easier installation on a shared host available? – Nick W Mar 07 '18 at 10:58