Welcome Guest, Not a member yet? Register   Sign In
email example for CI4
#1

(This post was last modified: 06-27-2020, 06:45 AM by carlkyo.)

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
#5

(This post was last modified: 07-17-2020, 07:12 AM by mlurie.)

Tom,

I am also relatively new to CodeIgniter.  In the past I have always used PHPMailer too, but CI4's mail class is just as good and easier to implement within the framework. 

The first thing you should do is set your mail server's configuration in App\Config\Email.php using $protocol, $SMTPHost, $SMTPUser, $SMTPPass, $SMTPPort, and $SMTPCrypto.

Alternatively, you can simply add the following to your .env file:

Code:
#--------------------------------------------------------------------
# E-Mail
#--------------------------------------------------------------------

email.protocol = 'smtp'
email.SMTPHost = 'mail.mailserver.com'
email.SMTPUser = '[email protected]'
email.SMTPPass = 'password'
email.SMTPPort = '465'
email.SMTPCrypto = 'ssl'

Use email.SMTPPort = '587' and email.SMTPCrypto = 'tls' if your mail server uses TLS instead of SSL.

Here is a simple code snippet that takes POST data from a mail form and sends it as an e-mail to the specified recipient:

PHP Code:
//Expecting $_POST['Name', 'Email', 'Phone', 'Message']
public function send() {

    
helper('form');

    
//Check for POST data
    
if($this->request->getMethod() == 'post') {
        
        
//Define validation rules
        
$rules = [
            
//POST data validation rules here
        
];

        
//Validate POST data
        
if(!$this->validate($rules)) {
            
$data['validation'] = $this->validator;
            return 
view('view_name'$data);  //Specify view name of mail form
        
} else {
            
            
//Send mail form data
            
$email Services::email();
            
$email->setFrom($this->request->getPost('Email'), $this->request->getPost('Name'));
            
$email->setTo('[email protected]');
            
$email->setSubject('Your Subject Here...');

            
$message  'Name: ' $this->request->getPost('Name') . "\n";
            
$message .= 'E-Mail: ' $this->request->getPost('Email') . "\n";
            
$message .= 'Phone: ' $this->request->getPost('Phone') . "\n";
            
$message .= 'Message: ' $this->request->getPost('Message') . "\n";
            
$email->setMessage($message);

            if(
$email->send()) {
                
//Handle success here...
            


            
//Output errors for debugging if necessary
            //echo $email->printDebugger();
            //exit;
        
}
    }
    
//Handle any errors here...


I hope this helps you get started.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB