• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
email example for CI4

#1
Hi All, I am Tom and I am a newbie to any framework but I love to learning Codeigniter.
I followed the tutorial below but not work, I had tried gmail and and other mail server

https://www.tutsmake.com/send-email-in-c...with-smtp/
Is there any mail example or PHPMailer example for CI4
many thanks and hope you everything is well

I am trying to make PHPMailer as a helper
PHP Code:
<?php 
use PHPMailer\PHPMailer\PHPMailer
use 
PHPMailer\PHPMailer\Exception

function 
send_mail($send_from NULL$send_to NULL$send_name NULL$send_subj NULL$send_body NULL){
require 
APPPATH.'ThirdParty/PHPMailer/Exception.php';
require 
APPPATH.'ThirdParty/PHPMailer/PHPMailer.php';
require 
APPPATH.'ThirdParty/PHPMailer/SMTP.php';


    $mail = new PHPMailer(true);                              // Passing `true` enables exceptions 
    try {
        //Server settings
        $mail->SMTPDebug SMTP::DEBUG_SERVER;                      // Enable verbose debug output
        $mail->isSMTP();                                            // Send using SMTP
        $mail->Host       'smtp1.example.com';                    // Set the SMTP server to send through
        $mail->SMTPAuth   true;                                   // Enable SMTP authentication
        $mail->Username   '[email protected]';                     // SMTP username
        $mail->Password   'secret';                               // SMTP password
        $mail->SMTPSecure PHPMailer::ENCRYPTION_STARTTLS;         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
        $mail->Port       587;                                    // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above

        //Recipients
        $mail->setFrom($send_from'Mailer');
        $mail->addAddress($send_to$send_name);     // Add a recipient
        #$mail->addAddress('[email protected]');               // Name is optional
        #$mail->addReplyTo('[email protected]', 'Information');
        #$mail->addCC('[email protected]');
        #$mail->addBCC('[email protected]');

        // Attachments
        #$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
        #$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

        // Content
        $mail->isHTML(true);                                  // Set email format to HTML
        $mail->Subject $send_subj;
        $mail->Body    $send_body;
        #$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}";
    }


Reply

#2
What problem are you having?

Your question only says it doesn't work.

Have you tried to make it work without it being a helper? Just direct from within the Controller?
Reply

#3
In the article how to use CodeIgniter's Email class is described. You are showing an attempt for using PHPMailer. Why are you trying to load its classes manually? Maybe you would need to read PHPMaier's documentation?
Reply

#4
I want some tutorial about phpmailer on CI 4. I had searched a lot and I didn't find it.
Laravel is popular and lots of resources
but now I just have done with the code below
thanks all
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.