Search code examples
phpyiiyii-extensionsmandrillmailgun

How to apply mailgun or mandrill in YII Framework


I have searched but did not get the exact way to apply mailgun or mandrill in my project, Please guide me to start on this.

Thanks in advance.


Solution

  • Both mandrill and mailgun have an SMTP interface. First step is to get these values.

    Next, you must get an smtp extension for yii. smtp-mail turns out to be the top result in google.

    Next, in protected/config/main.php, you can configure the plugin:

    'components' => array(
      'Smtpmail' => array(
        'class'    => 'application.extensions.smtpmail.PHPMailer',
        'Host'     => "SMTP_HOST",
        'Username' => 'SMTP_USERNAME',
        'Password' => 'SMTP_PASSWORD',
        'Mailer'   => 'smtp',
        'Port'     => 587,
        'SMTPAuth' => true,
      ),
    ),
    

    Finally, you can create a helper function to actually send the email:

    public function mailsend($to, $from, $subject, $message){  
        $mail = Yii::app()->Smtpmail;
        $mail->SetFrom($from, 'From Name');
        $mail->AddAddress($to, '');
        $mail->Subject = $subject;
        $mail->MsgHTML($message);
        if (!$mail->Send()) {
            echo 'Mailer Error: ' . $mail->ErrorInfo;
            return false;
        } else {
            echo 'Message sent!';
            return true;
        }
    }