/** * Try to send example mail. * * @return void */ public function actionDefault() { $recipients = array('*****@*****.**'); $subject = 'Send emails from Nette'; $body = 'SMTP client working!'; //enable SMTP debug messages Nette\Mail\SMTP\SMTPClient::$debugMode = TRUE; $mailer = $this->getContext()->getService('nette.mailer'); $message = $this->getContext()->createNette__mail(); $undelivered = array(); foreach ($recipients as $email) { try { if (!preg_match('/^\\s*$/', $email)) { $message->addTo($email); } } catch (InvalidArgumentException $e) { $undelivered[] = $email; } } $message->setSubject($subject); $message->setBody($body); ob_start(); try { $message->send(); $undelivered = array_merge($undelivered, $mailer->getUndeliveredRecipients()); if (count($undelivered) > 0) { $this->result = $undelivered; } else { $this->result = TRUE; } } catch (Nette\InvalidStateException $e) { $this->result = FALSE; } catch (Nette\IOException $e) { $this->result = FALSE; } $this->smtpLog = ob_get_clean(); }
<?php /* * This is an example of use of the nSMTPMailer alone (without Nette) * Just substitute username, password a from address and you can send mails! */ header('Content-Type: text/html; charset=utf-8'); require_once 'nSMTPMailer/SMTPClient.php'; $client = new Nette\Mail\SMTP\SMTPClient(); $client->setConnectionInfo('smtp.gmail.com', 587, 'tcp', 300); $client->setLoginInfo('username', 'password'); $client->setFrom('*****@*****.**'); $recipients = array('*****@*****.**', '*****@*****.**'); $client->setRecipients($recipients); // do not forget to include an empty line between headers and message body!!! $client->setBody(<<<END Subject: Sending mails using nSMTPMailer This mail contains very useful information. END ); try { $client->send(); $undelivered = $client->getUndeliveredRecipients(); if (empty($undelivered)) { echo 'Email successfully sent'; } else { echo 'Email failed to send to the listed addresses: ' . implode(',', $undelivered); } } catch (Nette\InvalidStateException $e) { echo 'Email has not been sent due to an error';