2017-07-15 31 views
2

這是我在用戶註冊時發送電子郵件的代碼。每封郵件到達用戶電子郵件需要5分鐘或更長時間。我不知道爲什麼電子郵件需要花費太多時間才能到達。如果我正在發送具有相同功能的電子郵件sendEmail()不帶僅帶有簡單文本的驗證碼。現在需要1分鐘或1分半鐘來訪問用戶的電子郵件。使用Codeigniter發送電子郵件需要很多時間

有時甚至在鏈接末尾發送驗證碼時,它甚至不會發送任何電子郵件。

我不知道如何發送電子郵件與SMTP。我發現了一些例子,他們將他們的域名添加到smtp_host,電子郵件,密碼中,該電子郵件是使用域創建的。我做了同樣的事情,但沒有發生我的電子郵件發送。這與我是否使用SMTP幾乎一樣。

這是我的功能名稱sendEmail(),我創建了模型來發送電子郵件。我之所以在模型中創建這個功能,是因爲我也必須從其他控制器發送電子郵件。 我不會,如果它可能是發送電子郵件的問題

請看看這個功能,我做錯了。或者如果還有其他方法,請告訴我如何做到這一點。或者任何類型的建議對我都很有幫助。

控制器

function index() { 
     //my validation rules are here 
     if ($this->form_validation->run() == TRUE) { 

      $data = $this->fetch_data_from_post(); 
      $user_email = $data['email']; 
      $code = random_string('unique'); 
      $verification_link = base_url('Home/verify/').$code; 
      $subject = "Confirmation Message"; 
      $message = "Dear User,\n\n\nPlease click on Given below URL to verify your Email Address ".$verification_link." \n\n Once you click on the above link then your account will be verified and you will get an opportunity to login. See you soon,Thank You...!\n"; 
      $email_sent = $this->Perfect_mdl->sendEmail($user_email,$subject,$message); 

      if($email_sent != 1){ 
       $flash = '<div class="alert alert-danger">Opppssss Somthing went Wrong...</div>'; 
       $this->session->set_flashdata('user_registration',$flash); 
       redirect('Home/signup'); 

      }else{ 
       $this->Perfect_mdl->_insert($data); 
       $flash = '<div class="alert alert-success">You are Successfully Registered... Please Check Your Email <b>'.$user_email.'</b> For Verification</div>'; 
       $this->session->set_flashdata('user_registration',$flash); 
       redirect('Home/signup'); 
      } 
     } 

     $data['meta_title'] = "Signup"; 
     $data['services_name'] = $this->Perfect_mdl->getServices_home(); 
     $dat['flash'] = $this->session->flashdata('user_registration'); 
     $this->load->view('signup',$data); 
    } 

型號: -

function sendEmail($useremail,$subject,$message) { 


    $config = Array(
    'protocol' => 'smtp', 
    'smtp_host' => 'smtp.mydomainname.com', 
    'smtp_port' => 25, 
    'smtp_user' => '[email protected]', // change it to yours 
    'smtp_pass' => 'vishal123456', // change it to yours 
    'mailtype' => 'html', 
    'charset' => 'iso-8859-1', 
    'crlf' => "\r\n", 
    'newline' => "\r\n", 
    'wordwrap' => TRUE 
    ); 
    $this->load->library('email', $config); 
    $this->email->from('[email protected]', 'Company Name'); 
    $this->email->to($useremail); 


    $this->email->subject($subject); 
    $this->email->message($message); 
    if($this->email->send()){ 
     return "1"; 
    }else{ 
     return "0"; 
    } 
} 
+0

爲什麼你使用'SMTP'從不同的服務器發送代碼?如果它全部在本地主機上,那麼就不需要'SMTP' – ArtisticPhoenix

+0

我不知道smtp是如何工作的。我使用smtp儘可能快地發送到達用戶電子郵件的原因。不,我用這個服務器。 –

+0

刪除該smpt配置陣列..在現場主機不需要使用此smtp配置.. – Mahesh

回答

1

它不會是慢的類,它將是您試圖連接到的SMTP郵件服務器,它會發送使頁面滯後的電子郵件。 這裏是我的一些建議。

首先,創建一個自定義配置文件email.ph P內application/config 請確保此配置是autoloaded.Open您Autoload.phpapplication/config,寫$autoload['config'] = array('email');

在我的情況下,我通過網絡郵件ID發送電子郵件,所以這裏是我的email.php

$config = Array(
    'protocol' => 'smtp', 
    'smtp_host' => 'SMTP_HOST_NAME', 
    'smtp_port' => 25, 
    'smtp_user' => 'SMTP_USER_NAME', // change it to your user name 
    'smtp_pass' => 'SMTP_PASSWORD', // change it to your password 
    'mailtype' => 'html', 
    'charset' => 'iso-8859-1', 
    'wordwrap' => TRUE 
); 

使用父構造是這樣的:

function __construct() 
{ 
    parent::__construct();   
    $this->load->library('email', $config); 
} 

然後你就可以很容易的郵件僅僅是這樣的:

$this->email->from('[email protected]', 'Account'); 
$this->email->to('[email protected]'); 
$this->email->cc('[email protected]'); 
$this->email->bcc('[email protected]'); 
$this->email->subject('Account Confirmation'); 
$message = "any message body you want to send"; 
$this->email->message($message); 
$this->email->send(); 

如果遵循此過程那麼也許可以節省幾秒鐘。

+0

在我的結構'$ this-> load-> library('email',$ config); '爲什麼這會拋出一個錯誤:Undefined $ config,但我已經創建了一個文件名'email.php'到我的配置文件夾中,並且還添加了'$ autoload ['config'] = array('email'); '到我的'autoload.php'中。 –

+0

您是否創建了與我在答案中相同的內容? @vishalkumar –

+0

是的,我做了相同的所有步驟 –

1

嘗試像這樣在你的控制器....

公共職能sendResetEmail($ params)方法{

 $params['body'] = 'emails/password_reset'; 
     $params['title'] = 'Forgot Password'; 
     $params['subject'] = 'Mail From Admin - Reset Password '; 
     $params['reset_url'] = base_url() . 'login/reset/?key=' . $params['reset_key'] . '&email=' . $params['email_user']; 
     $params['mailtype'] = 'html'; 
     $this->email->set_mailtype("html"); 
     $this->email->from('[email protected]', 'admin'); 
     $this->email->to($params['email_user']); 
     $this->email->subject($params['subject']); 
     $this->email->message($this->load->view('emails/main', $params, true)); 
     $this->email->send(); 
} 
+0

有時電子郵件在2分鐘內到達。但如果它沒有達到,那就花了五分鐘。是否可以更快地發送電子郵件(少於幾分鐘)。 –

+0

我們只使用這個相同的功能..它在一分鐘內接收用戶... – Mahesh

相關問題