Welcome Guest, Not a member yet? Register   Sign In
Test an email without 'Less Secure App Access'
#1
Lightbulb 
(This post was last modified: 01-28-2024, 11:40 AM by ozornick.)

Im trying to send an email through the CodeIgniter backend. Many suggest using Gmail SMTP server, but Im concerned about enabling 'Less Secure App Access' which I'd prefer to avoid. Is there an easy way to test email sending without having to use that feature?
Reply
#2

You can also use your internet providers SMTP server and send them to yourself.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

Totally understand your concern about "Less Secure App Access". Here are a few options to test email sending in CodeIgniter without enabling it:

1. Local Testing with Fake SMTP Server:

Use a library like "PHPMailer" with a local fake SMTP server like "MailCatcher" or "FakeSMTP". This simulates sending emails without actually sending them anywhere. Great for initial testing and development.
2. Use a Dedicated SMTP Service:

Consider signing up for a free or paid service like SendGrid, Mailgun, or Amazon SES. They offer secure SMTP access without needing "Less Secure App Access". They usually have simple integrations for CodeIgniter too.
3. Implement Email Templates (No Sending):

Focus on building well-formatted email templates in your CodeIgniter backend without attempting to send them. This tests the content and structure without involving external servers.
4. Test with a Different Email Provider:

If you have another email provider (e.g., Yahoo, Outlook), check if they offer SMTP access without requiring similar security compromises. Test cautiously with a dedicated email address for this purpose.
Remember, using "Less Secure App Access" isn't ideal, and these alternatives let you test and develop your email functionality safely and securely. Good luck!
Reply
#4

This is not easy, but if you want to use Gmail, you can use PHPMailer and XOAuth2 SMTP authentication.
See https://github.com/PHPMailer/PHPMailer/w...API-Client
Reply
#5

If your running on Windows you can use this tool, I have used it for years for testing emails.


ToolHeap - Test Mail Server Tool
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#6

If you use Docker, devkit provides MailHog:
See https://github.com/codeigniter4/devkit/t...er#mailhog
Reply
#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




Theme © iAndrew 2016 - Forum software by © MyBB