Page 1 of 1

smpt email, mail() or swiftmailer are not working using php

Posted: Wed Apr 21, 2021 5:48 pm
by rendyid
It's been 2 days struggling for email to get it to work.

I am using vestacp which is perfect in all cases but failed in sending email via PHP scripts. I have used traditional mail() function, SMTP and swift mailer too but nothing seems working.

I have setup a domain with Let's encrypt SSL in VestaCP and used A record in DNS for domain panel. And created [email protected] in mail section which is working fine in RoundCube by vestacp i.e., x.x.x.x/webmail

I don't get any error while using mail() function and SMTP (Email is not sending obviously) in ukuran kertas a4

Codeigniter Mail Function - (No errors, no email)

Code: Select all

$this->load->library('email');

$this->email->from('[email protected]', 'Web Application');
$this->email->to($email);
$this->email->subject('domain.com - Change Password Link');

$message = base_url().'auth/redirect';
$message .= ''.' '.'Click on link to reset password. Thank you';


$this->email->message($message);            
$this->email->set_mailtype("html");
$this->email->send();
I get Unable to connect with TLS encryption when using Swift mailer.

Swiftmailer - (Error - Unable to connect with TLS encryption, no email)

Code: Select all

try {
    $mail_host = 'ec2-x-x-xxx-xxx.ap-south-1.compute.amazonaws.com'; // Yup its aws
    $mail_port = '587'; // tried 25, 465
    $mail_user = '[email protected]';
    $mail_pass = 'xxx';
    // Create the Transport
    $transport = (new Swift_SmtpTransport($mail_host, $mail_port, 'tls'))
        ->setUsername($mail_user)
        ->setPassword($mail_pass);

    // Create the Mailer using your created Transport
    $mailer = new Swift_Mailer($transport);

    // Create a message
    $message = (new Swift_Message("Web App"))
        ->setFrom(array($mail_user => "Web app"))
        ->setTo([$to => ''])
        ->setSubject($subject)
        ->setBody($this->load->view("email/email_template", $data, TRUE), 'text/html');

    //Send the message
    $result = $mailer->send($message);
    if ($result) {
        return true;
    }
} catch (\Swift_TransportException $Ste) {
    $this->session->set_flashdata('error', $Ste->getMessage());
    return false;
} catch (\Swift_RfcComplianceException $Ste) {
    $this->session->set_flashdata('error', $Ste->getMessage());
    return false;
}