• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to send emails using CI 4

#11
captain-sensible,many thanks for your help
I add this and finally works,
PHP Code:
    public $psr4 = [
        'PHPMailer\PHPMailer' => ROOTPATH 'PHPMailer/src']; 

Code:
2020-06-29 11:24:15 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP y26sm32398306pff.26 - gsmtp
2020-06-29 11:24:15 CLIENT -> SERVER: EHLO web2.test
2020-06-29 11:24:16 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [113.254.203.93]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2020-06-29 11:24:16 CLIENT -> SERVER: STARTTLS
2020-06-29 11:24:16 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
2020-06-29 11:24:16 CLIENT -> SERVER: EHLO web2.test
2020-06-29 11:24:16 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [113.254.203.93]250-SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2020-06-29 11:24:16 CLIENT -> SERVER: AUTH LOGIN
2020-06-29 11:24:16 SERVER -> CLIENT: 334 VXNlcm5hbWU6
2020-06-29 11:24:16 CLIENT -> SERVER: [credentials hidden]
2020-06-29 11:24:16 SERVER -> CLIENT: 334 UGFzc3dvcmQ6
2020-06-29 11:24:16 CLIENT -> SERVER: [credentials hidden]
2020-06-29 11:24:16 SERVER -> CLIENT: 535-5.7.8 Username and Password not accepted. Learn more at535 5.7.8 https://support.google.com/mail/?p=BadCredentials y26sm32398306pff.26 - gsmtp
2020-06-29 11:24:16 SMTP ERROR: Password command failed: 535-5.7.8 Username and Password not accepted. Learn more at535 5.7.8 https://support.google.com/mail/?p=BadCredentials y26sm32398306pff.26 - gsmtp
SMTP Error: Could not authenticate.
2020-06-29 11:24:16 CLIENT -> SERVER: QUIT
2020-06-29 11:24:17 SERVER -> CLIENT: 221 2.0.0 closing connection y26sm32398306pff.26 - gsmtp
SMTP Error: Could not authenticate.
Message could not be sent. Mailer Error: SMTP Error: Could not authenticate.
Reply

#12
ok now its just a case of playing with it to get "SMTP Error: Could not authenticate" working. That will be your smtp host set up and password.

When you sign in to say mail.google.com you use , your "normal" password that won't work , so you need to generate an
app password:

Create & use App Passwords

Go to your Google Account.
On the left navigation panel, choose Security.
On the "Signing in to Google" panel, choose App Passwords. ...
At the bottom, choose Select app and choose the app you're using.
Choose Select device and choose the device you're using.
Choose Generate.

you will see a 16 or so digit string , generated. Use your normal email account creds eg

[email protected] but use that generated 16 digit as password in phpmailer.


Also when emails do get sent you get a blank page, so you need to add logic code and view. The return , is one or true if mail gets sent . i can't remember so covered myself using :


$logic2 = $mail->send();

if (($logic2==1) OR ($logic2=="true"))
{
$data = ['title' => 'mail',
'info' => 'your message was sent ',
'date'=>$date


];

echo view ('header',$data);
echo view ('info',$data);
echo view ('footer',$data);
}

//i use a "info" view a lot and simply pass in , a string of text as needed relevant to the purpose.

The other thing to mention , is people used to quote their emails in web pages and got bombarded , with spam so now you see things like Joe 'at' doe 'period' webdomain.com

Also with a form on the internet you will not get so much spam but you will get some. To filter a lot of spammers out , before the text box string is sent to PHPmailer I have a class , that the text runs through.
Also for the string that a user types into the message text box i use :


$message= $this->request->getVar('message');
$cleanMessage = htmlentities($message,ENT_QUOTES);


i don't know how effective that is, but it can't do any harm


The last point i just remembered is that the main use of using your gmail account for smtp host is that you can do development on say apache with CI4 and you will find emails get sent, so you can fine tune on localhost before having to use live internet sever . At that point when everything more or less works as expected shift to your web hosting smtp ; you might have to adjust ports etc in PHPMailer code
Reply

#13
Hi Captain,
I added this below and it works now.
but it shows Class 'PHPMailer\PHPMailer\PHPMailer' not found when I moved it to web station.
thanks
Quote:$mail->SMTPOptions = array(
            'ssl' => array(
                'verify_peer' => false,
                'verify_peer_name' => false,
                'allow_self_signed' => true
            )
        );

Hi Captain
here is my solution
I tested it works well on local and web station
no need to set psr4
thanks and wish you stay safe
PHP Code:
use PHPMailer\PHPMailer\Exception;
use 
PHPMailer\PHPMailer\PHPMailer;
use 
PHPMailer\PHPMailer\SMTP;
public function 
send($users) {    
    
require ROOTPATH.'/PHPMailer/src/Exception.php';
require 
ROOTPATH.'/PHPMailer/src/PHPMailer.php';
require 
ROOTPATH.'/PHPMailer/src/SMTP.php';
    
        $mail 
= new PHPMailer(true);
        $mail->SMTPOptions = array(
            'ssl' => array(
                'verify_peer' => false,
                'verify_peer_name' => false,
                'allow_self_signed' => true
            
)
        );
        try {
            //Server settings
            #$mail->SMTPDebug = SMTP::DEBUG_SERVER;                      // Enable verbose debug output
            $mail->isSMTP();                                            // Send using SMTP
            $mail->Host '192.168.1.219';                    // Set the SMTP server to send through
            $mail->SMTPAuth true;                                   // Enable SMTP authentication
            $mail->Username '[email protected]';                     // SMTP username
            $mail->Password 'PxAkdV%4MW';                               // SMTP password
            $mail->SMTPSecure PHPMailer::ENCRYPTION_STARTTLS;         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
            #$mail->SMTPSecure = 'tls'; 
            $mail->Port 587;                                    // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
            //Recipients
            $mail->setFrom('[email protected]''Mailer');
            $mail->Subject "Thanks for your registration";
            foreach ($users as $user) {
                $mail->addAddress($user['email'], $user['name']);
                $mail->Body "<h2>Hello, {$user['name']}!</h2> <p>Thanks for your registeration,the application is being processed</p>";
                $mail->AltBody "Hello, {$user['name']}! \n Thanks for your registeration,the application is being processed";

                try {
                    $mail->send();
                    //echo "Message sent to: ({$user['email']}) {$mail->ErrorInfo}\n";
                } catch (Exception $e) {
                    //echo "Mailer Error ({$user['email']}) {$mail->ErrorInfo}\n";
                }

                $mail->clearAddresses();
            }
            $mail->smtpClose();
        } catch (Exception $e) {
            #echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
        }
    
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.