Html 使用codeigniter发送html邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9095528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 22:12:31  来源:igfitidea点击:

send html mail using codeigniter

htmlemailcodeigniter

提问by user1182623

Error in mail content using SMTP in codeigniter Actually, my mail is sent with HTMLtags and it is showing the HTMLtags which is not correct.

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

$config = Array(
'protocol' => 'smtp',
        'smtp_host' => 'ssl://smtp.googlemail.com',
        'smtp_port' => 465,
        'smtp_user' => '[email protected]',
        '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('[email protected]', 'ddd');

    $list = array('[email protected]');
    $this->email->to($list);
    $this->email->subject('Testing Email');
    $this->email->message($email_body);

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

If am sending mail without using SMTP it works fine. What is my mistake?

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

回答by Gabriel

You can try this line of code which will set the mail type to be for HTML:

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

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

回答by Madan Sapkota

As of CodeIgniter 3.x. There are many features added. This example is almost same with earlier versions, but you can do much more.

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

Follow the link for documentation.

按照文档链接。

// load email library
$this->load->library('email');

// prepare email
$this->email
    ->from('[email protected]', 'Example Inc.')
    ->to('[email protected]')
    ->subject('Hello from Example Inc.')
    ->message('Hello, We are <strong>Example Inc.</strong>')
    ->set_mailtype('html');

// send email
$this->email->send();

If you have template design. You can also include template in messagemethod like this ...

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

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

Here, the first parameter is email_template.phpin your views directory, second parameter the data to be sent to email template, you can set it ''or array()or []if not passing any dynamic data and last parameter truemake sure, you grab the template data instead output.

在这里,第一个参数email_template.php在您的视图目录中,第二个参数是要发送到电子邮件模板的数据,您可以设置它''array()或者[]如果不传递任何动态数据,最后一个参数true确保您获取模板数据而不是输出。

Hope this is helpful.

希望这是有帮助的。

回答by kebyang

Setting the mail type to HTMLworks for me:

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

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

回答by Carlos Atencio

Try to manually set the content type header doing this:

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

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

That solve the issue for me.

那为我解决了问题。

回答by Umesh Patil

To send HTML email you first have to compose your message in a variable and then pass that variable to codeigniter's "$this->email->message()" method, like below,

要发送 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";
   }

hope it will help.

希望它会有所帮助。

enjoy!!

请享用!!

回答by Parimal Nakrani

Gmail prevent access of your account. You need some changes on your gmail :-

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

Step : 1

第1步

Some apps and devices use less secure sign-in technology, which makes your account more vulnerable. You can turn off access for these apps, which we recommend, or turn on access if you want to use them despite the risks.

某些应用程序和设备使用安全性较低的登录技术,这会使您的帐户更容易受到攻击。您可以关闭这些应用程序的访问权限(我们建议您这样做),或者如果您想使用它们而不顾风险打开访问权限。

Turn on less secure app

打开安全性较低的应用

Step : 2

第2步

Enable IMAP Status
Enable POP Status

启用 IMAP 状态
启用 POP 状态

Enable IMAP and POP Status

启用 IMAP 和 POP 状态

回答by Nikunj K.

Can you please try with this code, B'z I can able to send HTML email with this code.

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

$configemail = Array(
        'protocol' => 'smtp',
        'smtp_host' => 'ssl://smtp.gmail.com', //mail.webconsort.com
        'smtp_port' => 465, //5074
        'smtp_user' => '[email protected]', //[email protected]
        '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);
    }

回答by Vishal P Gothi

Use it like this.. it works great for me.

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

$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();

回答by zechdc

My issue was that Codeigniter's Global XSS Filtering was encoding some html tags like <html>so the email clients could no longer recognize them.

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

To get around this, check out my other post.

要解决这个问题,请查看我的另一篇文章

回答by Andres Orduz

add this code lines:

添加此代码行:

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