/** * * @param $sender * @param $senderPass * @param $receiver * @param $subject * @param $message * @return unknown_type */ public function email($sender, $senderPass, $receiver, $subject, $message, $replyTo = '*****@*****.**') { try { $smtp = new Swift_Connection_SMTP("smtp.gmail.com", 465, Swift_Connection_SMTP::ENC_SSL); $smtp->setTimeout(10); $smtp->setUsername($sender); $smtp->setPassword($senderPass); $smtp->attachAuthenticator(new Swift_Authenticator_LOGIN()); $swift = new Swift($smtp, 'exambuff.co.uk'); } catch (Exception $e) { $error = $e->getMessage(); } $msgSubject = $subject; $msgBody = $message; $swfMessage = new Swift_Message($msgSubject, $msgBody); try { $swift->send($swfMessage, $receiver, $replyTo); return true; } catch (Exception $e) { if (!@$error) { $error = ''; } $error .= $e->getMessage(); log_message('error', $error); } return @$error; }
public function testAuthExtensionsWithAsteriskAsNameAreRunAlways() { $auth = new MockAuthenticator(); $auth->setReturnValue("isAuthenticated", true); $auth->setReturnValue("getAuthExtensionName", "*foo"); $auth->expectOnce("isAuthenticated"); $conn = new Swift_Connection_SMTP(); $conn->setExtension("AUTH", array("not-an-asterisk")); $conn->attachAuthenticator($auth); $conn->setUsername("xxx"); $conn->setPassword("yyyy"); $conn->postConnect(new Swift($conn, "xxx", Swift::NO_START)); }
/** * Creates a SwiftMailer instance. * * @param string DSN connection string * @return object Swift object */ public static function connect($config = NULL) { if (!class_exists('Swift', FALSE)) { // Load SwiftMailer require Kohana::find_file('vendor', 'swift/Swift'); // Register the Swift ClassLoader as an autoload spl_autoload_register(array('Swift_ClassLoader', 'load')); } // Load default configuration $config === NULL and $config = Kohana::config('email'); switch ($config['driver']) { case 'smtp': // Set port $port = empty($config['options']['port']) ? NULL : (int) $config['options']['port']; if (empty($config['options']['encryption'])) { // No encryption $encryption = Swift_Connection_SMTP::ENC_OFF; } else { // Set encryption switch (strtolower($config['options']['encryption'])) { case 'tls': $encryption = Swift_Connection_SMTP::ENC_TLS; break; case 'ssl': $encryption = Swift_Connection_SMTP::ENC_SSL; break; } } // Create a SMTP connection $connection = new Swift_Connection_SMTP($config['options']['hostname'], $port, $encryption); // Do authentication, if part of the DSN empty($config['options']['username']) or $connection->setUsername($config['options']['username']); empty($config['options']['password']) or $connection->setPassword($config['options']['password']); if (!empty($config['options']['auth'])) { // Get the class name and params list($class, $params) = arr::callback_string($config['options']['auth']); if ($class === 'PopB4Smtp') { // Load the PopB4Smtp class manually, due to its odd filename require Kohana::find_file('vendor', 'swift/Swift/Authenticator/$PopB4Smtp$'); } // Prepare the class name for auto-loading $class = 'Swift_Authenticator_' . $class; // Attach the authenticator $connection->attachAuthenticator($params === NULL ? new $class() : new $class($params[0])); } // Set the timeout to 5 seconds $connection->setTimeout(empty($config['options']['timeout']) ? 5 : (int) $config['options']['timeout']); break; case 'sendmail': // Create a sendmail connection $connection = new Swift_Connection_Sendmail(empty($config['options']) ? Swift_Connection_Sendmail::AUTO_DETECT : $config['options']); // Set the timeout to 5 seconds $connection->setTimeout(5); break; default: // Use the native connection $connection = new Swift_Connection_NativeMail($config['options']); break; } // Create the SwiftMailer instance return email::$mail = new Swift($connection); }
/** * Private function that emails the user with their activation key * * @param String $email * @param String $firstName * @todo add language lines for emails */ private function emailActivation($email, $firstName, $key) { require_once $this->config->item('swift'); require_once $this->config->item('swift_smtp'); require_once $this->config->item('swift_auth'); try { $smtp = new Swift_Connection_SMTP("smtp.gmail.com", 465, Swift_Connection_SMTP::ENC_SSL); $smtp->setTimeout(10); $smtp->setUsername("*****@*****.**"); $smtp->setPassword("44naughty555"); $smtp->attachAuthenticator(new Swift_Authenticator_LOGIN()); $swift = new Swift($smtp, 'exambuff.co.uk'); } catch (Exception $e) { $msg = $e->getMessage(); log_message('error', 'Error setting up activation email ' . $msg); } $viewData['name'] = $firstName; $viewData['activationCode'] = $key; $msgSubject = "Activate your Exambuff account"; $msgBody = $this->load->view('email/activation_request', $viewData, TRUE); $swfMessage = new Swift_Message($msgSubject, $msgBody); try { $swift->send($swfMessage, $email, '*****@*****.**'); } catch (Exception $e) { $msg = $e->getMessage(); log_message('error', 'Error while sending activation email ' . $msg); } }
/** * Private function that emails the user with their activation key * * @param String $email * @param String $firstName * @todo add language lines for emails */ private function emailActivation($email, $firstName) { require_once $this->config->item('swift'); require_once $this->config->item('swift_smtp'); require_once $this->config->item('swift_auth'); $this->load->model('activator'); $this->activator->generateKey(); $this->activator->set('email', $email); $this->activator->create(); try { $smtp = new Swift_Connection_SMTP("smtp.gmail.com", 465, Swift_Connection_SMTP::ENC_SSL); $smtp->setTimeout(10); $smtp->setUsername("*****@*****.**"); $smtp->setPassword("44naughty555"); $smtp->attachAuthenticator(new Swift_Authenticator_LOGIN()); $swift = new Swift($smtp, 'exambuff.co.uk'); } catch (Exception $e) { $msg = $e->getMessage(); log_message('error', $msg); } $message = new Swift_Message("test", "Dear {$firstName},\n Please go to " . app_base() . 'signup/activate/' . $this->activator->get('activationKey') . ' to finish your activation'); $swift->send($message, $email, '*****@*****.**'); }
/** * Email the user a receipt for their payment * * @param $email * @param $items * @param $time * @param $transactionID * @param $method * @return unknown_type */ private function _emailReceipt($recipientEmail) { require_once $this->config->item('swift'); require_once $this->config->item('swift_smtp'); require_once $this->config->item('swift_auth'); try { $smtp = new Swift_Connection_SMTP("smtp.gmail.com", 465, Swift_Connection_SMTP::ENC_SSL); $smtp->setTimeout(10); $smtp->setUsername("*****@*****.**"); $smtp->setPassword("gim3th3c@ash"); $smtp->attachAuthenticator(new Swift_Authenticator_LOGIN()); $swift = new Swift($smtp, 'exambuff.co.uk'); } catch (Exception $e) { $msg = $e->getMessage(); log_message('error', "Email to {$recipientEmail} failed due to {$msg}"); return false; } $this->load->model('user'); $user = new User(); $user->retrieve($recipientEmail); $nameChunks = explode(' ', $user->get('name'), 2); $firstName = $nameChunks[0]; $viewData['firstName'] = $firstName; $viewData['question'] = $this->scriptToPay->get('question'); $msgSubject = "Payment receipt from Exambuff"; $msgBody = $this->load->view('email/receipt', $viewData, TRUE); $swfMessage = new Swift_Message($msgSubject, $msgBody); try { if ($swift->send($swfMessage, $recipientEmail, '*****@*****.**')) { return true; } return false; } catch (Exception $e) { $msg = $e->getMessage(); log_message('error', "Email to {$recipientEmail} failed due to {$msg}"); return false; } }