PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, March 9, 2022

[FIXED] An Error Was Encountered Unable to send email using PHP mail(). Your server might not be configured to send mail using this method

 March 09, 2022     codeigniter, email, gmail, php, smtp     No comments   

Issue

I'm trying to sending an email using CodeIgniter via Gmail SMTP but the function is not working it is showing me this error on the localhost?

An Error Was Encountered Unable to send email using PHP mail(). 
Your server might not be configured to send mail using this method.

Here my email function

$config = Array(
        'protocol' => 'smtp',
        'smtp_host' => 'ssl://smtp.googlemail.com',
        'smtp_port' => 465,
        'smtp_user' => '******@gmail.com', // change it to yours
        'smtp_pass' => '**********', // change it to yours
        'mailtype' => 'html',
        'charset' => 'iso-8859-1',
        'wordwrap' => TRUE
    );

    $message = 'Test';
    $this->load->library('email', $config);
    $this->email->set_newline("\r\n");
    $this->email->from('*********@gmail.com'); // change it to yours
    $this->email->to('test@gmail.com');// change it to yours
    $this->email->subject('Resume from JobsBuddy for your Job posting');
    $this->email->message($message);
    if($this->email->send())
    {
        echo 'Email sent.';
    }
    else
    {
        show_error($this->email->print_debugger());
    }

Solution

Use PHP MAILER or SWIFT MAILER

  • https://github.com/PHPMailer/PHPMailer
  • https://github.com/swiftmailer/swiftmailer

For now here is PHPMAILER


try {
    //Server settings
    $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      //Enable verbose debug output
    $mail->isSMTP();                                            //Send using SMTP
    $mail->Host       = 'smtp.gmail.com';                     //Set the SMTP server to send through
    //$mail->Host = gethostbyname('smtp.gmail.com');
    $mail->SMTPAuth   = true;                                   //Enable SMTP authentication
    $mail->Username   = 'sender@gmail.com';                     //SMTP username
    $mail->Password   = 'sender-password';                               //SMTP password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         //Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged                                   //TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
    $mail->Port       = 587;                                    //TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above

    //Recipients
    $mail->setFrom('receiver1@gmail.com', 'Mailer');
    $mail->addAddress('receiver2@gmail.com', 'Dylan');     

    //Content
    $mail->isHTML(true);                                  //Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $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}";
}

You can install these amazing libarries with composer



Answered By - Ryan The Ghost
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing