2016-07-25 125 views
0

Im phalcon中的新功能需要一个phpmailer的工作示例,该示例从我的站点发送电子邮件。目前我只是想这样。我只是按照这个链接:github.com/asyamedya/phalcon-phpmailer。如何在phalcon中使用phpmailer发送邮件php

[config.php中]

$config = new Phalcon\Config(array(
"app"    => array(
"controllersDir"=> "app/controllers/", 
"modelsDir"  => "app/models/", 
"viewsDir"  => "app/views/", 
"pluginsDir" => "app/plugins/", 
"libraryDir" => "app/library/", 
"helpersDir" => "app/helpers/", 
"formsDir"  => "app/forms/", 
"cacheDir"  => "cache/volt/", 
"logsDir"  => "cache/logs/", 
"encryptKey" => "", 
"baseUri"  => "/demo/", 
"StaticBaseUri" => "http://localhost/demo/", 
"debug"   => '0', 
), 
"mail"    => array(
    "driver"  => "smtp", 
    "host"   => "smtp.gmail.co",       
    "username"  => "[email protected]",    
    "password"  => "*****",       
    "security"  => "tls", //ssl:465, tls:587      
    "port"   => 587, 
    "charset"  => "UTF-8", 
    "email"   => "[email protected]", 
    "name"   => "webmaster", 
), 
)); 

[loader.php]

use Phalcon\Loader; 
$loader = new Loader(); 
$loader->registerDirs(
    array(
     APP_PATH . $config->app->controllersDir, 
     APP_PATH . $config->app->modelsDir, 
     APP_PATH . $config->app->pluginsDir, 
     APP_PATH . $config->app->libraryDir, 
     APP_PATH . $config->app->helpersDir, 
     APP_PATH . $config->app->formsDir, 
     APP_PATH . $config->app->cacheDir, 
    ) 
)->register(); 

[service.php]

use Phalcon\DI\FactoryDefault; 
use Phalcon\Mvc\View; 
use Phalcon\Mvc\Dispatcher; 
use Phalcon\Mvc\Url as UrlResolver; 
use Phalcon\Mvc\Router; 
use Phalcon\Security; 
use \PHPMailer as PHPMailer; 
use Phalcon\Mvc\View\Engine\Volt; 
use Phalcon\Events\Manager as EventsManager; 
use Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter; 
use Phalcon\Flash\Session as FlashSession; 
use Phalcon\Logger\Adapter\File as Logger; 
use Phalcon\Mvc\Model\Metadata\Apc as ApcMetaData; 
use Phalcon\Session\Adapter\Files as SessionAdapter; 

    require(APP_PATH . $config->app->libraryDir . 'PHPMailer/PHPMailerAutoload.php'); 
    $di->set('pmailer', function() use ($config){ 
    $pmailer = new PHPMailer(); 
    //$mail->setLanguage('fr', '/optional/path/to/language/directory/'); 
    $pmailer->isSMTP(true); 
    $pmailer->SMTPDebug = 2; // Enable verbose debug output 
    $pmailer->SMTPOptions = array(
    'ssl' => array(
    'verify_peer' => false, 
    'verify_peer_name' => false, 
    'allow_self_signed' => true 
    ) 
    ); 
    $pmailer->Charset = $config->mail->charset; 
    $pmailer->Host = $config->mail->host; 
    $pmailer->SMTPAuth = true; 
    $pmailer->Username = $config->mail->username; 
    $pmailer->Password = $config->mail->password; 
    $pmailer->SMTPSecure = $config->mail->security; 
    $pmailer->Port = $config->mail->port; 
    $pmailer->addAddress($config->mail->email,$config->mail->name); 
    $pmailer->isHTML(true); 
    return $pmailer; 
}); 

[控制器指数]

public function SendmailAction() 
{ 
    $this->pmailer->From = "[email protected]"; 
    $this->pmailer->FromName = "user"; 
    $this->pmailer->addReplyTo("[email protected]", "user"); 
    $this->pmailer->addAddress('[email protected]'); 
    $this->pmailer->Subject = "email test !"; 
    $this->pmailer->Body = "success!"; 
    $this->pmailer->WordWrap = 70; 
    if(!$this->pmailer->send()){echo 'Message could not be sent.'; 
    echo 'Mailer Error: ' . $this->pmailer->ErrorInfo; 
    } else { 
    echo 'Message has been sent'; 
    } 
} 

在我的图书馆我有这个文件。

[库/ PHPMailer的/]

class.phpmailer.php 
class.pop3.php 
class.smtp.php 
PHPMailerAutoload.php 
class.phpmaileroauthgoogle.php 
+1

[有你甚至试着用搜索引擎这个(https://github.com/asyamedya/phalcon-phpmailer)?你有什么尝试?你得到什么样的错误? – Timothy

+2

你应该尝试一些,如果你失败了,然后问我们。 StackOverflow不是我们为您编写代码的服务。请多加努力。 –

+0

Niki Mihaylov&Timothy!我更新了我的问题。并无法发送邮件。请!告诉我的错是什么?我得到致命错误:在第177行的C:\ xampp \ htdocs \ demo \ app \ config \ services.php中找不到类“Phalcon \ PHPMailer” –

回答

0

我不知道,不过你,我觉得这是不对您的service.php

use Phalcon\PHPMailer;

更改它到

use \PHPMailer as PHPMailer;

+0

获取此:警告:具有非复合名称' PHPMailer'在第11行中的C:\ xampp \ htdocs \ demo \ app \ config \ services.php中没有任何作用 致命错误:未在C:\ xampp \ htdocs \ demo \ app \ config \在线177 –

+0

更新,错过了\ – Borjante

+0

仍然同样的问题!有没有人可以举出一个有效的例子?这也经过测试。 –