使用控制器/路由发送电子邮件

时间:2019-07-11 10:34:39

标签: codeigniter

如果我在测试页上使用此代码,则效果很好

控制器:

    function __construct(){

    parent::__construct();

    $this->load->config('emailer');
    $this->load->library(array('form_validation', 'Mymailer', 'upload', 'email'));
    $this->load->helper(array('form', 'url', 'file'));

public function index()
{
    $data = $formData = array();

      if ($this->form_validation->run() == TRUE){
        $name = $this->input->post('name', TRUE);
        $email = $this->input->post('email', TRUE); 
        $subject = $this->input->post('subject', TRUE); 
        $message = $this->input->post('message', TRUE);

//等....

        if (!$this->mymailer->send()) {
            $formData = array();
            $data['status'] = array(
                'type' => 'error',
                'msg' => 'Some problems occurred, please try again.'

            );
        } else {
            $data['status'] = array(
                'type' => 'success',
                'msg' => 'Email send successfully.' 
            );  
        }   

应用程序/视图/ .....提交时:

      <div class="form-group row">
    <div class="col-sm-10">
      <?=form_submit('sendemail', 'Send Email', array('class' => 'btn btn-primary')); ?>
      <?=form_reset('reset', 'Reset', array('onClick'=>"CKupdate()", 'class'=>'btn btn-primary')); ?>
    </div>
  </div>

libraires:Mymailer.php

    <?php
defined('BASEPATH') OR exit('No direct script access allowed');

use PHPMailer\PHPMailer\POP3;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require ( dirname(__dir__, 2).'/vendor/autoload.php' );

/**
* mymailer
*/
class Mymailer extends PHPMailer {
   function __construct(){
      parent::__construct();

      $CI =& get_instance();
   }       
}   

配置/路由

$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

我想在此方案中使用发送方法: 应用程序/视图/页面/ .. 索引,关于,联系

控制器Pages.php:

    class Pages extends CI_Controller
{
    public function __construct() {
        parent::__construct();

    }

    function view($page = 'index')
    {
        if (!file_exists('application/views/pages/' . $page . '.php')) {
            show_404();
        }

        $this->load->view('pages/' . $page);

    }

控制器Contact.php:

    class Send_email extends CI_Controller {

    function __construct(){
        parent::__construct();

        $this->load->config('emailer');
        $this->load->library(array('form_validation', 'Mymailer', 'upload', 'email'));
        $this->load->helper(array('form', 'url', 'file'));

    }

    public function contact()
    {
    /*  if ($this->input->post('sendemail') == 'sendemail') {
            $this->load->library('email');*/
            /*------------------------------------------------------*/
            $data = $formData = array();

            if ($this->form_validation->run() == TRUE) {
                $name = $this->input->post('name', TRUE);
                $email = $this->input->post('email', TRUE);
                $subject = $this->input->post('subject', TRUE);
                $message = $this->input->post('message', TRUE);
                $file = $this->input->post('file', TRUE);

                $data["name"] = $name;
                $data["message"] = $message;
                $xmsg = $this->load->view('email_body', $data, TRUE);

                /*------------------------------------------------------*/

                $config['upload_path'] = './uploads/';
                $config['allowed_types'] = 'gif|jpg|png|txt|doc|docs|xls|xlsm|dwg';
                $config['max_size'] = '1000000';
                $config['max_width'] = '1024';
                $config['max_height'] = '768';

                /*-------------------------------------------------------------------*/

                $this->mymailer->SMTPOptions = array('ssl' => array('verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true));
                $this->mymailer->isSMTP();
                //Enable SMTP debugging
                // 0 = off (for production use)
                // 1 = client messages
                // 2 = client and server messages
                $this->mymailer->isHTML(true);
                $this->mymailer->SMTPDebug = 2;//$this->config->item('SMTPDebug');
                $this->mymailer->SMTPSecure = $this->config->item('SMTPSecure');
                $this->mymailer->Host = $this->config->item('Host');
                $this->mymailer->Port = $this->config->item('Port');
                $this->mymailer->SMTPAuth = $this->config->item('SMTPAuth');
                $this->mymailer->Username = $this->config->item('Username');
                $this->mymailer->Password = $this->config->item('Password');
                $this->mymailer->setFrom($this->config->item('setFrom'), $this->config->item('Name'));
                $this->mymailer->addReplyTo($this->config->item('addReplyTo'), $this->config->item('Name'), $this->config->item('Email'));
                $this->mymailer->addAddress($email, $name);
                $this->mymailer->Subject = ($subject);
                $this->mymailer->msgHTML($xmsg);


                $this->load->library('upload', $config);
                $this->upload->do_upload('attachment');
                $upload_data = $this->upload->data();


                $this->mymailer->addAttachment($upload_data['full_path']);
                $this->mymailer->set_newline("\r\n");
                $this->mymailer->ste_crlf("\r\n");
                $this->mymailer->AltBody = 'This is a plain-text message body';


                if (!$this->mymailer->send()) {
                    $formData = array();
                    $data['status'] = array(
                        'type' => 'error',
                        'msg' => 'Some problems occurred, please try again.'

                    );
                } else {
                    $data['status'] = array(
                        'type' => 'success',
                        'msg' => 'Email send successfully.'
                    );

                    $this->load->view('pages/contact', $data);

                }

            }

      }

配置/路由:

    $route['(:any)'] = 'pages/view/$1';
    $route['default_controller'] = 'pages/view/';

    $route['send-email'] = 'contact controller';
    $route['email'] = 'contact/contact';

我没有犯错误,但是它甚至没有做应该做的事情 如果没有数据发送:错误 发送确认:没有结果

我该如何解决?

0 个答案:

没有答案