/** * This function will return SMTP connection. It will try to load options from * config and if it fails it will use settings from php.ini * * @param void * @return Swift */ static function getMailer() { $mail_transport_config = config_option('mail_transport', self::MAIL_TRANSPORT_MAIL); // Emulate mail() - use NativeMail if ($mail_transport_config == self::MAIL_TRANSPORT_MAIL) { $mailer = new Swift(new Swift_Connection_NativeMail()); return $mailer->isConnected() ? $mailer : null; // Use SMTP server } elseif ($mail_transport_config == self::MAIL_TRANSPORT_SMTP) { // Load SMTP config $smtp_server = config_option('smtp_server'); $smtp_port = config_option('smtp_port', 25); $smtp_secure_connection = config_option('smtp_secure_connection', self::SMTP_SECURE_CONNECTION_NO); $smtp_authenticate = config_option('smtp_authenticate', false); if ($smtp_authenticate) { $smtp_username = config_option('smtp_username'); $smtp_password = config_option('smtp_password'); } // if switch ($smtp_secure_connection) { case self::SMTP_SECURE_CONNECTION_SSL: $transport = SWIFT_SSL; break; case self::SMTP_SECURE_CONNECTION_TLS: $transport = SWIFT_TLS; break; default: $transport = SWIFT_OPEN; } // switch $mailer = new Swift(new Swift_Connection_SMTP($smtp_server, $smtp_port, $transport)); if (!$mailer->isConnected()) { return null; } // if $mailer->setCharset('UTF-8'); if ($smtp_authenticate) { if ($mailer->authenticate($smtp_username, $smtp_password)) { return $mailer; } else { return null; } // if } else { return $mailer; } // if // Somethings wrong here... } else { return null; } // if }
function getContent($smtp_server, $smtp_port, $transport, $smtp_username, $smtp_password, $body, $attachments) { //Load in the files we'll need Env::useLibrary('swift'); switch ($transport) { case 'ssl': $transport = SWIFT_SSL; break; case 'tls': $transport = SWIFT_TLS; break; default: $transport = 0; break; } //Start Swift $mailer = new Swift(new Swift_Connection_SMTP($smtp_server, $smtp_port, $transport)); if (!$mailer->isConnected()) { return false; } // if $mailer->setCharset('UTF-8'); if ($smtp_username != null) { if (!$mailer->authenticate($smtp_username, self::ENCRYPT_DECRYPT($smtp_password))) { return false; } } if (!$mailer->isConnected()) { return false; } // add attachments $mailer->addPart($body); // real body if (is_array($attachments) && count($attachments) > 0) { foreach ($attachments as $att) { $mailer->addAttachment($att["data"], $att["name"], $att["type"]); } } $content = $mailer->getFullContent(false); $mailer->close(); return $content; }