Welcome Guest, Not a member yet? Register   Sign In
SMTP problem ( same as codeigniter 3)
#1

Hi ,sorry for english , thi is my function to send email to recover password with smtp :

PHP Code:
  public function send_new_password_by_mail($email_to,$newpassword)
  {
      $email = \Config\Services::email();

      $configuration = new \Config\Common();


      $config_smtp['protocol'] = 'smtp';
      $config_smtp['SMTPHost'] = 'authsmtp.securemail.pro';
      $config_smtp['SMTPUser'] = 'xxxxxxxxxx';
      $config_smtp['SMTPPass'] = 'xxxxxxxxxx';
      $config_smtp['SMTPPort'] = 465;
      $config_smtp['SMTPCrypto'] = 'ssl';

      $email->initialize($config_smtp);

      
      $email
->setMailType('html');
      $email->setFrom($configuration->siteName$configuration->siteEmail);
      $email->setTo($email_to);

      //$email->cc('[email protected]');
      //$email->bcc('[email protected]');


      $email->setSubject('Reset Password ' $configuration->siteName);

      $message '
      <html>
      <head>
          <title></title>
      </head>
      <body>

          <p>Ricevi questa mail perche` hai chiesto un reset della password sul sito '
.$configuration->siteName.'<p>
          
          <p>La tua nuova password e\' : '
.$newpassword.'<p>
          
          
          <p>Esegui il login cliccando  <a href="'
base_url().'/login">qui</a> <p>


          
          <p>Se hai ricevuto questa mail per sbaglio non considerarla<p>
          
          <a href="'
.$configuration->siteUrl .'">'.$configuration->siteUrl.' <p>


      </body>
      </html>'
;

      $email->setMessage($message);

      if ($email->send()) {


          return TRUE;
          
      
}else{

          return $email->printDebugger();
      

The method return TRUE but no mail are send . In codeigniter 3 i must use phpmailer (with same credential work fine) . Must i use phpmailer in codeigniter 4 too ?
Reply
#2

Si, puoi usarlo anche con il CI 4.*  

Please follow this thread and the ways our friends showed how to use Phpmailer with CI4. It's pretty easy. Be careful with the app/config/autoload.php part.

Saluti!

* you can also use it with CI4
Reply
#3

(01-22-2021, 09:45 AM)demyr Wrote: Si, puoi usarlo anche con il CI 4.*  

Please follow this thread and the ways our friends showed how to use Phpmailer with CI4. It's pretty easy. Be careful with the app/config/autoload.php part.

Saluti!

* you can also use it with CI4
Ciao demyr  Wink  .... I install the phpmailer library in ThirdParty , change the namespace as 

namespace App\ThirdParty\phpmailer\src\PHPMailer\PHPMailer;

in PHPMailer,SMTP,EXCEPTION,OAuth

in controller where i must use :

use App\ThirdParty\phpmailer\src\PHPMailer\PHPMailer\PHPMailer;
use App\ThirdParty\phpmailer\src\PHPMailer\PHPMailer\Exception;
use App\ThirdParty\phpmailer\src\PHPMailer\PHPMailer\SMTP;

but i have this error :

Class 'App\ThirdParty\phpmailer\src\PHPMailer\PHPMailer\PHPMailer' not found
Reply
#4

(This post was last modified: 01-22-2021, 02:00 PM by demyr.)

Ciao di nuovo,

Upload the PHP Mailer folder at the same level with app-public-system etc folders.

Autoload PHP:

PHP Code:
public $psr4 = [
        
APP_NAMESPACE => APPPATH// For custom app namespace
        
'Config'      => APPPATH 'Config',
        
'App' => APPPATH// To ensure filters, etc still found
                'Forms' => ROOTPATH.'forms'
                'PHPMailer\\PHPMailer'=> ROOTPATH.'PHPMailer/src'
    
]; 

Create a Controller with a name, per esempio, Sendmail.php

PHP Code:
<?php namespace App\Controllers;

use 
CodeIgniter\Controller;
use 
PHPMailer\PHPMailer\PHPMailer;
use 
PHPMailer\PHPMailer\OAuth;
use 
PHPMailer\PHPMailer\Exception;
use 
PHPMailer\PHPMailer\SMTP;
//use \App\Andy\CheckSpam;
//use CodeIgniter\I18n\Time;

class Sendmail extends BaseController
{

public function 
send(){


$mail = new PHPMailer();
//above is where you actually instantiate PHPMailer class and i can do this due to declaration above and location of PHPMailer directory
$mail->isSMTP();
$mail->Timeout 20;
$mail->SMTPDebug 0;
//Set the hostname of the mail server
$mail->Host 'your smtpe mail address like smtp.yoursite.com or your server name';
$mail->Port 587;
$mail->SMTPSecure 'tls';
$mail->SMTPAuth true;
$mail->Username "your_email@your_website.com";
$mail->Password "Password Please";

$mail->setFrom('your_email@your_website.com''A name ');
$mail->addAddress('to_which_email@you_are_sending.com''');
//Set the subject line

//retrieve the data from your form
$name $this->request->getVar('name');
$email $this->request->getVar('email');
$phone_number $this->request->getVar('phone_number');
$msg_subject $this->request->getVar('msg_subject');
$message $this->request->getVar('message');

//below is up to you
$combined_name 'From : ' .$name;
$combined_email 'Email : ' .$email;
$combined_phone 'Phone Number : ' .$phone_number;
$combined_subject 'The subject is : ' .$msg_subject;
$combined_message 'The message : ' .$message;

//below is a must
$mail->isHTML(true);                                  
$mail
->Subject 'You have got a mail my friend!';            
$mail->Body    "$combined_name<br />$combined_email<br />$combined_phone<br />$combined_subject<br />$combined_message";        
$mail->CharSet 'utf-8';

if(
$mail->send()){
    
$msg 'Success. Thanks for writing us';
return 
redirect()->to($_SERVER['HTTP_REFERER'])->with('msg'$msg); 
    }else{
    echo 
'There is an error';
    }
}
//public ends

}
?>

To show the success message within your view:

PHP Code:
<?php if (session('msg')) : ?>
        <div class="alert alert-info alert-dismissible">
            <?= session('msg'?>
            <button type="button" class="close" data-dismiss="alert"><span>×</span></button>
        </div>
    <?php endif ?>

The message box is made with Bootstrap, you can change it easily.

Hope it works
Cordiali Saluti
Reply
#5

(This post was last modified: 01-22-2021, 02:01 PM by iRedds.)

oh, GOD.

app/Config/Autoload.php 
property $psr4
add a pair (namespace => path)
PHP Code:
'PHPMailer\PHPMailer' => APPPATH 'ThirdParty/phpmailer/src' 

In a controller

PHP Code:
use PHPMailer\PHPMailer\PHPMailer

$mailer 
= new PHPMailer(); 
Reply
#6

(01-22-2021, 02:00 PM)iRedds Wrote: oh, GOD.

app/Config/Autoload.php 
property $psr4
add a pair (namespace => path)
PHP Code:
'PHPMailer\PHPMailer' => APPPATH 'ThirdParty/phpmailer/src' 

In a controller

PHP Code:
use PHPMailer\PHPMailer\PHPMailer

$mailer 
= new PHPMailer(); 

I dont want autoload phpmailer but use or only where i want use why the class aren't round?
Reply
#7

(01-22-2021, 02:03 PM)pippuccio76 Wrote: I dont want autoload phpmailer but use or only where i want use why the class aren't round?

You just define a namespace. There is no autoload.
Reply
#8

The Email class in CodeIgniter 4 is a barely-changed port from version 3. There is a complete rewrite in the works but for now it is likely any problems you had using the CI3 version will persist in CI4.
That said I use the Email class daily without trouble, so you might just need to give it some more effort.
Reply
#9

(This post was last modified: 01-25-2021, 08:10 AM by pippuccio76.)

(01-25-2021, 06:11 AM)MGatner Wrote: The Email class in CodeIgniter 4 is a barely-changed port from version 3. There is a complete rewrite in the works but for now it is likely any problems you had using the CI3 version will persist in CI4.
That said I use the Email class daily without trouble, so you might just need to give it some more effort.
I found the problem , It s a server problem about sender if i use a link as name the class ( or the server) dont send the mail
Reply
#10

(01-22-2021, 02:00 PM)iRedds Wrote: oh, GOD.

    Smile
Reply




Theme © iAndrew 2016 - Forum software by © MyBB