【问题标题】:send html mail using codeigniter使用 codeigniter 发送 html 邮件
【发布时间】:2012-02-24 02:46:41
【问题描述】:

在 codeigniter 中使用 SMTP 的邮件内容出错 实际上,我的邮件是用HTML 标签发送的,它显示的HTML 标签不正确。

$config = Array(
'protocol' => 'smtp',
        'smtp_host' => 'ssl://smtp.googlemail.com',
        'smtp_port' => 465,
        'smtp_user' => 'user@gmail.com',
        'smtp_pass' => '',
        'mailtype'  => 'html', 
        'charset' => 'utf-8',
        'wordwrap' => TRUE

    );
    $this->load->library('email', $config);
    $this->email->set_newline("\r\n");
    $email_body ="<div>hello world</div>";
    $this->email->from('user@gmail.com', 'ddd');

    $list = array('user@gmail.com');
    $this->email->to($list);
    $this->email->subject('Testing Email');
    $this->email->message($email_body);

    $this->email->send();
    echo $this->email->print_debugger();

如果我在不使用 SMTP 的情况下发送邮件,它可以正常工作。我的错误是什么?

【问题讨论】:

标签: html email codeigniter


【解决方案1】:

你可以试试这行代码,它将邮件类型设置为HTML

 $this->email->set_mailtype("html");

【讨论】:

    【解决方案2】:

    截至CodeIgniter 3.x。添加了许多功能。此示例与早期版本几乎相同,但您可以做的更多。

    点击文档链接。

    // load email library
    $this->load->library('email');
    
    // prepare email
    $this->email
        ->from('info@example.com', 'Example Inc.')
        ->to('to@example.com')
        ->subject('Hello from Example Inc.')
        ->message('Hello, We are <strong>Example Inc.</strong>')
        ->set_mailtype('html');
    
    // send email
    $this->email->send();
    

    如果您有模板设计。你也可以像这样在message 方法中包含模板...

    ->message($this->load->view('email_template', $data, true))
    

    这里,第一个参数是你的views目录下的email_template.php,第二个参数是要发送到email模板的数据,你可以设置为''或者array()或者[]如果不传递任何动态数据和最后一个参数true 确保,你抓取的是模板数据而不是输出。

    希望这有帮助。

    【讨论】:

    • 我已经尝试过这种方式。电子邮件发送成功。但问题是我的 html 正文中的图像不可见。应用的所有文本和样式。但没有图像。有什么问题?
    【解决方案3】:

    将邮件类型设置为HTML 对我有用:

    $email_setting  = array('mailtype'=>'html');
    $this->email->initialize($email_setting);
    

    【讨论】:

      【解决方案4】:

      尝试手动设置内容类型标题:

      $this->email->set_header('Content-Type', 'text/html');
      

      这为我解决了问题。

      【讨论】:

        【解决方案5】:

        要发送 HTML 电子邮件,您首先必须在变量中编写消息,然后将该变量传递给 codeigniter 的“$this->email->message()”方法,如下所示,

         $this->load->library('email');
        
         $message = "
             <html>
               <head>
                 <title>your title</title>
               </head>
               <body>
                 <p>Hello Sir,</p>
                 <p>Your message</p>
               </body>
             </html>";
        
           $this->email->from('email id', 'name');
           $this->email->to('email id');
        
           $this->email->subject('email subject');
           $this->email->message($message);
        
           if ($this->email->send()) {
             print "success";
           } else {
             print "Could not send email, please try again later";
           }
        

        希望它会有所帮助。

        享受!!

        【讨论】:

          【解决方案6】:

          你能试试这个代码吗,B'z我可以用这个代码发送HTML电子邮件。

          $configemail = Array(
                  'protocol' => 'smtp',
                  'smtp_host' => 'ssl://smtp.gmail.com', //mail.webconsort.com
                  'smtp_port' => 465, //5074
                  'smtp_user' => 'XXXXXXXX@gmail.com', //tororide@webconsort.com
                  'smtp_pass' => 'XXXXXXXX', //'T0r0r1d3'
                  'mailtype'  => 'html', 
                  'charset'   => 'iso-8859-1'
              );
          
              $CI =& get_instance();
          
              $CI->load->library('email', $configemail);
          
              $CI->email->initialize($configemail);
              $CI->email->set_newline("\r\n");
          
              $CI->email->from($from, $fromName);
              $CI->email->to($to); 
          
              $CI->email->subject($subject);
              $CI->email->message($body);
              if ($attachments != null && !empty($attachments)){
                  foreach($attachments as $a){
                      $CI->email->attach($a);
                  }
              }
          
              try{
                  $CI->email->send();
                  return true;
              }
              catch (Exception $e){
                  //var_dump($e);
              }
          

          【讨论】:

            【解决方案7】:

            Gmail 阻止访问您的帐户。您需要对您的 gmail 进行一些更改:-

            步骤:1

            某些应用和设备使用不太安全的登录技术,这使得 您的帐户更容易受到攻击。您可以关闭这些应用的访问权限, 我们推荐,或者如果您想使用它们,请打开访问权限,尽管 风险。

            Turn on less secure app

            步骤:2

            启用 IMAP 状态
            启用 POP 状态

            Enable IMAP and POP Status

            【讨论】:

              【解决方案8】:

              添加以下代码行:

              $this->email->set_mailtype("html");
              $this->email->set_newline("\r\n");
              $this->email->set_crlf("\r\n");
              

              【讨论】:

              • 虽然此代码可能会解决问题,including an explanation 关于如何以及为什么解决问题将真正有助于提高您的帖子质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人。请edit您的回答添加解释并说明适用的限制和假设。
              【解决方案9】:

              我的问题是 Codeigniter 的全局 XSS 过滤正在编码一些 html 标签,如 &lt;html&gt;,因此电子邮件客户端无法再识别它们。

              要解决这个问题,请查看我的另一个 post

              【讨论】:

                【解决方案10】:

                像这样使用它。它对我很有用。

                $this->load->library('email');
                
                $config['charset'] = 'iso-8859-1';
                
                $config['wordwrap'] = TRUE;
                
                $config['mailtype'] = 'html';
                
                $this->email->initialize($config);
                
                $this->email->from($fromemail);
                
                $this->email->to($toemail);
                
                $this->email->subject('Subject');
                
                $this->email->message($html);
                
                $success=$this->email->send();
                

                【讨论】:

                  【解决方案11】:
                  $message = '
                  <html>
                  <head>
                  <title>Birthday Reminders for August</title>
                  </head>
                  <body>
                  <p>Here are the birthdays upcoming in August!</p>
                  <table>
                  <tr>
                  <th>Person</th><th>Day</th><th>Month</th><th>Year</th>
                  </tr>
                  <tr>
                  <td>Johny</td><td>10th</td><td>August</td><td>1970</td>
                  </tr>
                  <tr>
                  <td>Sally</td><td>17th</td><td>August</td><td>1973</td>
                  </tr>
                  </table>
                  </body>
                  </html>
                  ';
                  
                  $config = Array(
                  'protocol' => 'smtp',
                  'smtp_host' => 'ssl://mail.example.com',
                  'smtp_port' => 465,
                  'smtp_user' => 'noreply@example.com',
                  'smtp_pass' => 'example@123',
                  'mailtype'  => 'html',
                  'charset'   => 'iso-8859-1'
                  );
                  
                  $this->load->library('email', $config);
                  $this->email->set_mailtype("html");
                  $this->email->set_newline("\r\n");
                  $this->email->from('noreply@example.com', 'example');
                  $this->email->to('example@example.in');
                  $this->email->subject('Enquiry');
                  $this->email->message($message);
                  

                  【讨论】:

                  • 这个答案可能是正确的,但您应该详细说明您的示例代码与问题中的代码有何不同。 (我猜重要的区别是 set_mailtype("html"); 代码中的行,问题的代码中缺少该行。)
                  【解决方案12】:

                  你会试试的!!在显示出许多错误后它对我的工作是 100% 工作。

                          $subject = 'New message.';
                          $config = Array(        
                              'protocol' => 'sendmail',
                              'smtp_host' => 'Your smtp host',
                              'smtp_port' => 465,
                              'smtp_user' => 'webmail',
                              'smtp_pass' => 'webmail pass',
                              'smtp_timeout' => '4',
                              'mailtype'  => 'html', 
                              'charset'   => 'utf-8',
                              'wordwrap' => TRUE
                          );
                          $this->load->library('email', $config);
                          $this->email->set_newline("\r\n");
                          $this->email->set_header('MIME-Version', '1.0; charset=utf-8');
                          $this->email->set_header('Content-type', 'text/html');
                  
                          $this->email->from('from mail address', 'Company name ');
                          $data = array(
                               'message'=> $this->input->post('message')
                                   );
                          $this->email->to($toEmail);  
                          $this->email->subject($subject); 
                  
                          $body = $this->load->view('email/sendmail.php',$data,TRUE);
                          $this->email->message($body);   
                          $this->email->send();
                  

                  【讨论】:

                    猜你喜欢
                    • 2022-01-06
                    • 2015-08-02
                    • 1970-01-01
                    • 2011-10-02
                    • 2017-10-08
                    • 1970-01-01
                    • 1970-01-01
                    相关资源
                    最近更新 更多