Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.8 kB
4
Indexable
Never
'transport' => [
                'class' => 'Swift_SmtpTransport',
                'host' => getenv('MAIL_HOST'),
                'username' => getenv('MAIL_USERNAME'),
                'password' => getenv('MAIL_PASSWORD'),
                'port' => getenv('MAIL_PORT'),
                'encryption' => getenv('MAIL_ENCRYPTION'),
            ],


.env
MAIL_DRIVER=smtp
MAIL_HOST=srv162.niagahoster.com
MAIL_PORT=465
MAIL_USERNAME=noreply@daftarmana.com
MAIL_PASSWORD=*
MAIL_ENCRYPTION=ssl

send email
 public function sendEmail($to, $subject, $view, $params = [], $from = null, $cc = null, $bcc = null, $replyTo = null)
    {
        // Membuat instance dari mailer
        $mailer = Yii::$app->mailer;

        // Membuat instance dari message
        $message = $mailer->compose($view, $params);
        // Set alamat email pengirim jika diisi, jika tidak maka akan menggunakan pengaturan dari konfigurasi mailer
        if ($from !== null) {
            $message->setFrom($from);
        }

        // Set alamat email cc jika diisi
        if ($cc !== null) {
            $message->setCc($cc);
        }

        // Set alamat email bcc jika diisi
        if ($bcc !== null) {
            $message->setBcc($bcc);
        }

        // Set alamat email reply-to jika diisi
        if ($replyTo !== null) {
            $message->setReplyTo($replyTo);
        }

        // Set alamat email tujuan dan subject email
        $message->setTo($to)
            ->setSubject($subject);

        // Mengirim email
        try {
            $message->send();
            return true;
        } catch (InvalidArgumentException $e) {
            // Jika email tidak berhasil dikirim, tampilkan error message
            Yii::error('Email sending failed: ' . $e->getMessage());
            return false;
        }
    }