Welcome Guest, Not a member yet? Register   Sign In
Test an email without 'Less Secure App Access'
#7

I suggest using the Mailtrap mail server. It's quite user-friendly. Here are CodeIgniter code snippets which connect to the server and send simple text mail.
Code:
<?php

class EmailController extends CI_Controller {

    public function __construct() {
        parent::__construct();
        // Load email library
        $this->load->library('email');
    }

    public function send_email() {
        // Only allow POST requests
        if ($this->input->server('REQUEST_METHOD') == 'POST') {
            // Configure your SMTP settings
            $config['protocol'] = 'smtp';
            $config['smtp_host'] = 'sandbox.smtp.mailtrap.io';
            $config['smtp_port'] = 587;
            $config['smtp_user'] = 'username';  // Insert your SMTP username
            $config['smtp_pass'] = 'password';  // Insert your SMTP password
            $config['crlf'] = "\r\n";
            $config['newline'] = "\r\n";

            //Initialize email library with the config array
            $this->email->initialize($config);

            // Set email parameters
            $this->email->from('[email protected]', 'Sender Name');  // Replace with sender's email and name
            $this->email->to('[email protected]');  // Replace with receiver's email
            $this->email->subject('New Form Submission');
            $this->email->message('This is the form submission content');

            // Send the email
            if ($this->email->send()) {
                echo "Email sent successfully!";
            } else {
                // For debugging purpose only, it will print any email sending error
                echo $this->email->print_debugger();
            }
        } else {
            // Show error or message for methods other than POST
            echo "This endpoint only accepts POST requests.";
        }
    }
}
Reply


Messages In This Thread
RE: Test an email without 'Less Secure App Access' - by martaperez - 03-26-2024, 04:50 AM



Theme © iAndrew 2016 - Forum software by © MyBB