-
demyr Senior Member
   
-
Posts: 331
Threads: 17
Joined: Sep 2018
Reputation:
7
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
-
pippuccio76 Senior Member
   
-
Posts: 547
Threads: 231
Joined: Jun 2017
Reputation:
2
(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 .... 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
-
demyr Senior Member
   
-
Posts: 331
Threads: 17
Joined: Sep 2018
Reputation:
7
01-22-2021, 01:57 PM
(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
-
iRedds Senior Member
   
-
Posts: 662
Threads: 36
Joined: Apr 2019
Reputation:
45
01-22-2021, 02:00 PM
(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();
-
pippuccio76 Senior Member
   
-
Posts: 547
Threads: 231
Joined: Jun 2017
Reputation:
2
(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?
-
Corsari Member
  
-
Posts: 108
Threads: 26
Joined: Jun 2017
Reputation:
4
(01-22-2021, 02:00 PM)iRedds Wrote: oh, GOD.
|