Search code examples
phpcodeigniteremailamazon-ec2smtp

Email not sending on ec2 instance running Ubuntu


I have the following code that is used to send an email. This works fine on MAMP; but not on LAMP.

$config = [
    'crlf' => "\r\n",
    'mailtype' => 'html',
    'newline' => "\r\n",
    'protocol' => 'smtp',
    'smtp_host' => 'ssl://smtpout.secureserver.net',
    'smtp_pass' => 'password',
    'smtp_port' => 465,
    'smtp_user' => 'username',
    'wordwrap' => true,
];
$this->load->library('email', $config);

// Send the email
$this->email->from('email@domain.com', 'Email Name');
$this->email->to('myemail@gmail.com');
$this->email->subject('Subject');
$this->email->message('Message');
$this->email->set_newline("\r\n");
var_dump($this->email->send());

false is returned from the var_dump call. OpenSSL is installed. I checked my security group settings for this instance and all outbound traffic is enabled.


Solution

  • As I suggest early, I've added an example for Using PHPmailer on Amazon EC2.(Worked/Tested well)

    When you use normal mail(aws) you have to pay for it.Read More about Amazon Simple Email Service and How to setup SMTP on EC2

    Controller

    require_once('./phpmailer/class.phpmailer.php'); # Files attached below
    require_once('./phpmailer/class.smtp.php');
    
    $mail = new PHPMailer();
    
    $mail->IsSMTP();
    $mail->SMTPAuth   = true;
    $mail->SMTPSecure = "tls";
    $mail->Host       = "smtp.gmail.com"; 
    $mail->Port       = 587;  
    $mail->Username   = "someMailAddress@gmail.com";
    $mail->Password   = "password";
    
    $to = ('to@mail.com');
    $subject = 'This is and test mail from PHPMailer';
    
    $message = 'Hi';
    $message .= 'this is an mail body';
    
    $mail->SetFrom( 'info@stackoverflow.com' , 'Stack OverFlow' );
    $mail->AddAddress( $to , 'Abdulla Nilam' );
    $mail->Subject = $subject;
    $mail->MsgHTML( $message );
    $sendEmail = $mail->Send();
    
    if( $sendEmail == true )
    {
        $this->session->set_flashdata('success', 'It was sent ....');
        redirect('contact');
    }
    else
    {
        $this->session->set_flashdata('error', 'Nah :/ something wrong');
        redirect('contact');
    }
    

    Files of PHPMailer