Ivan Golubar
asked on
How to use PhpMailer?
I am trying to use PhpMailer to send email.
From GetHub i did download all package. For minimal installation i am using only class.phpmailer.php and class.SMTP.php. And also PHPMailerAutoload.php. Where i have next function :
The first error that I am getting is :
Connection failed. Error #2: stream_socket_client() [<a href='function.stream-sock et-client' >function. stream-soc ket-client </a>]: php_network_getaddresses: getaddrinfo failed: Name or service not known
I have all files in same directory as main.php.
In my main.php i did change only lines with comments.
What do I have to change to get rid of before mentioned error?
main.php:
From GetHub i did download all package. For minimal installation i am using only class.phpmailer.php and class.SMTP.php. And also PHPMailerAutoload.php. Where i have next function :
function PHPMailerAutoload($classname)
{
//Can't use __DIR__ as it's only in PHP 5.3+
$filename = dirname(__FILE__).DIRECTORY_SEPARATOR.'class.'.strtolower($classname).'.php';
if (is_readable($filename)) {
require $filename;
}
}
The first error that I am getting is :
Connection failed. Error #2: stream_socket_client() [<a href='function.stream-sock
I have all files in same directory as main.php.
In my main.php i did change only lines with comments.
What do I have to change to get rid of before mentioned error?
main.php:
<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
//use PHPMailer\PHPMailer\PHPMailer;
//use PHPMailer\PHPMailer\Exception;
//Load Composer's autoloader
require 'PHPMailerAutoload.php';
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = 4; //ERRORS INFO
$mail->isSMTP();
$mail->Host = 'smtp1.gmail.com' // SMTP servers
$mail->SMTPAuth = true;
$mail->Username = 'user@example.com';
$mail->Password = 'secret';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
//Recipients
$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('to.me@gmail.com'); // I AM SENDING TO ME
$mail->addAddress('ellen@example.com');
$mail->addReplyTo('info@myotheremail.com'); //MY OTHER EMAIL
// $mail->addCC('cc@example.com');
//$mail->addBCC('bcc@example.com');
//Content
$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
?>
Pretty sure the gmail server address is : smtp.gmail.com.
Have a look at the gmail examples on the PHPMailer site - https://github.com/whera/P HPMailer/b lob/master /examples/ gmail.phps
It also seems that you're using an older version of PHPMailer, which is no longer supported, so you might want to consider upgrading to version 6.
Have a look at the gmail examples on the PHPMailer site - https://github.com/whera/P
It also seems that you're using an older version of PHPMailer, which is no longer supported, so you might want to consider upgrading to version 6.
ASKER
Thank you for: smtp.gmail.com.
I am getting now this error.
Message could not be sent. Mailer Error: SMTP Error: Could not authenticate.
And Gmail sent me an notification message that they did block somebody (me in this case) trying to reach my acount.
I will try upgrade to higher version and let you know.
But can i use my server instead of gmail?
As next?
I am getting now this error.
Message could not be sent. Mailer Error: SMTP Error: Could not authenticate.
And Gmail sent me an notification message that they did block somebody (me in this case) trying to reach my acount.
I will try upgrade to higher version and let you know.
But can i use my server instead of gmail?
As next?
//Server settings
$mail->SMTPDebug = 4; // Enable verbose debug output
$mail->isSMTP();
$mail->Host = 'smtp.myserver.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'me@myserver.com'; // SMTP username 'info@3eyeobjects.com';
$mail->Password = 'myserverpass'; // SMTP password 55ena48s
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I will go on with smtp.gmail.com for now.
What must i use for
When i use my actual gmail password
I get next error : Password command failed
And the link for error solving is sending me to https://myaccount.google.com/
What must i use for
$mail->Password = '???????' // SMTP password
?When i use my actual gmail password
I get next error : Password command failed
And the link for error solving is sending me to https://myaccount.google.com/
Don't know why it would give you that error. In order to send email through the Gmail server, you need to authenticate yourself. You usually do this with you email address and password.
So if you're going to use GMail for SMTP, then it gets a little complicated. Basically, Google prefers OAuth2 authentication instead of the more straightforward username/password. You CAN change the setting in Gmail to tell it to allow "less-secure" apps to send mail through your account in order to use the standard username/password, but there are various downsides to that.
The PHPMailer documentation does a good job of covering this. First read the Gmail, OAuth2 and "Allow less secure apps" section here (about halfway down the page):
https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Then you can read PHPMailer's documentation page specifically on Gmail/XOAuth2 that gives you step-by-step instructions:
https://github.com/PHPMailer/PHPMailer/wiki/Using-Gmail-with-XOAUTH2
Also be forewarned that I believe GMail still has various limits on sending, so make sure you're not trying to set this all up just to realize that you can't send as many emails as you'd like:
https://support.google.com/a/answer/166852?hl=en
The PHPMailer documentation does a good job of covering this. First read the Gmail, OAuth2 and "Allow less secure apps" section here (about halfway down the page):
https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Then you can read PHPMailer's documentation page specifically on Gmail/XOAuth2 that gives you step-by-step instructions:
https://github.com/PHPMailer/PHPMailer/wiki/Using-Gmail-with-XOAUTH2
Also be forewarned that I believe GMail still has various limits on sending, so make sure you're not trying to set this all up just to realize that you can't send as many emails as you'd like:
https://support.google.com/a/answer/166852?hl=en
ASKER
If I want to upgrade to master or version 6.0, I have to use a composer. But before I learn about composer , can I use then composer to install packages on my server directly ?
You can install an upgrade without using Composer - it's just easier with Composer.
If you want to install packages onto your server, then you would install Composer on your server. Otherwise, install it locally, install the packages locally, and then push your whole app to the server.
If you want to install packages onto your server, then you would install Composer on your server. Otherwise, install it locally, install the packages locally, and then push your whole app to the server.
ASKER
I used composer and to install composer I had to install xapp .
Then I copied all on server.
My project tree is like next ilustration (PHPmailer.php and exception.php are inside of src folder).
How must I change my path in sendEmailPHP.php
to find files?
Then I copied all on server.
My project tree is like next ilustration (PHPmailer.php and exception.php are inside of src folder).
How must I change my path in sendEmailPHP.php
to find files?
<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
//use PHPMailer\PHPMailer\PHPMailer;
//use PHPMailer\PHPMailer\Exception;
use vendor\PHPMailer\PHPMailer\src\PHPMailer;
use vendor\PHPMailer\PHPMailer\src\Exception;
//Load Composer's autoloader
require 'vendor/autoload.php';
ASKER
Next also isn't loading the page.
use PHPMailer\PHPMailer\src\PHPMailer;
use PHPMailer\PHPMailer\src\Exception;
require 'vendor/autoload.php';
Hi Ivan,
You need a slightly different path to load the autoloader. Use this:
You need a slightly different path to load the autoloader. Use this:
require __DIR__ . '/vendor/autoload.php';
Actually - I think your include path was correct. It looks like your use statements are wrong:
use PHPMailer\PHPMailer\PHPMai ler;
use PHPMailer\PHPMailer\Except ion;
To help whilst debugging, it make sense to turn on error reporting. At the very top of your script, add the following:
error_reporting(E_ALL);
ini_set('display_errors', 1);
use PHPMailer\PHPMailer\PHPMai
use PHPMailer\PHPMailer\Except
To help whilst debugging, it make sense to turn on error reporting. At the very top of your script, add the following:
error_reporting(E_ALL);
ini_set('display_errors', 1);
ASKER
Thank you. I am trying with: require __DIR__ . '/vendor/autoload.php';
But no positive result yet.
Because of error log:
PHP Fatal error: Class 'PHPMailer\PHPMailer\src\P HPMailer' not found
I did change use PHPMailer\PHPMailer\src\PH PMailer; to use PHPMailer\PHPMailer\PHPMai ler; (as it is in example on GitHub)
and then I got in error log next:
PHP Parse error: syntax error, unexpected '[' in .......................... ......../v endor/phpm ailer/phpm ailer/src/ PHPMailer. php on line 288
And this is line 288 in PHPMailer.php : public $SMTPOptions = [];
I am hoping to not misleading You now.
But no positive result yet.
Because of error log:
PHP Fatal error: Class 'PHPMailer\PHPMailer\src\P
I did change use PHPMailer\PHPMailer\src\PH
and then I got in error log next:
PHP Parse error: syntax error, unexpected '[' in ..........................
And this is line 288 in PHPMailer.php : public $SMTPOptions = [];
I am hoping to not misleading You now.
Hey Ivan,
That error on Line 288 tells me that you're using a really old version of PHP. The shorthand syntax for creating an array:
$SMTPOptions = [];
was introduced in PHP Version 5.4. We're now up to version 7.2.
Technically, if you're using an older version of PHP, you could use an older version of PHPMailer, but that's not supported anymore (neither is the older versions of PHP), so I would strongly suggest you upgrade your PHP version to at least version 7. How you do that will depend on your current setup.
You can easily find out which version of PHP you're running by creating a simple file with the following content:
That error on Line 288 tells me that you're using a really old version of PHP. The shorthand syntax for creating an array:
$SMTPOptions = [];
was introduced in PHP Version 5.4. We're now up to version 7.2.
Technically, if you're using an older version of PHP, you could use an older version of PHPMailer, but that's not supported anymore (neither is the older versions of PHP), so I would strongly suggest you upgrade your PHP version to at least version 7. How you do that will depend on your current setup.
You can easily find out which version of PHP you're running by creating a simple file with the following content:
<?php
phpinfo();
Load this up in your browser and it will give you a lot of information about your setup, including the version of PHP you're running.
ASKER
In PHPMailer.php I could see that it is PHP Version 5.5.
But for PHPMailer installation I was following actual instructions on PHPMailer page, which did direct me to next command in Command prompt: composer require phpmailer/phpmailer
But for PHPMailer installation I was following actual instructions on PHPMailer page, which did direct me to next command in Command prompt: composer require phpmailer/phpmailer
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ok. I have PHP 7.2.7 now.
I will not use Gmail and my host support has given me info for:
I can send emails now.
I will not use Gmail and my host support has given me info for:
$mail->Host ='';
I can send emails now.
Excellent. Glad you got it working
To resolve it, look for any typos in the domains you're using. If the domains are not public domains (e.g. internal / private domains), you might need to add an entry in your hosts file for that domain.