<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Sendgrid_Test extends CI_Controller {

    public function index()
    {
        $this->load->library('email');

        // Config for SendGrid
        $config = array(
            'protocol'  => 'smtp',
            'smtp_host' => 'relay.mailbaby.net',
            'smtp_port' => 587,
            'smtp_user' => 'mb75919', // This must be the word: apikey
            'smtp_pass' => 'esZewQWePymrRdpN3Zm6', // Replace with real SendGrid API key
            'mailtype'  => 'html',
            'charset'   => 'utf-8',
            'newline'   => "\r\n",
            'crlf'      => "\r\n",
            'smtp_crypto' => 'tls'
        );

        $this->email->initialize($config);
        $this->email->from('connect@evoting.io', 'Your Name'); // Use a verified sender email
        $this->email->to('ideasfromsunil@gmail.com');
        $this->email->subject('SendGrid SMTP Test');
        $this->email->message('<p>This is a <strong>test email</strong> via <b>SendGrid SMTP</b>.</p>');

        if (!$this->email->send()) {
            echo "<h3>❌ Failed to send email.</h3>";
            echo "<pre>" . $this->email->print_debugger(['headers']) . "</pre>";
        } else {
            echo "✅ Email sent successfully!";
        }
    }
}
