【问题标题】:Codeigniter Html Email Display HTML Source CodeCodeigniter Html 电子邮件显示 HTML 源代码
【发布时间】:2017-01-22 01:03:21
【问题描述】:

尝试发送电子邮件,但它的邮寄格式是 HTML 源代码。 这是我的控制器-

$config = Array(        
        'protocol' => 'SMTP',
        'smtp_host' => 'hostname',
        'smtp_port' => 'portname',
        'smtp_user' => 'abn@gmail.com',
        'smtp_pass' => '***',
        'smtp_timeout' => '4',
        'mailtype'  => 'html',
        'charset'   => 'utf-8'
    );
    $this->load->library('email', $config);
    $this->email->set_newline("\r\n");

    $this->email->from('devendra@gmail.com', 'ABC');
    $data = array(
         'userName'=> $this->input->post('email'),
         'password'=> $this->input->post('password'),
         'EmpName'=> $this->input->post('name'),
         'Number'=> $this->input->post('number'),
         'City'=> $this->input->post('city'),
         'State'=> $this->input->post('email'),
         'Address'=> $this->input->post('address')
             );
    $this->email->to($this->input->post('email')); 
    $Subject = 'User Login Details';
    $this->email->subject($Subject); 

    $body = $this->load->view('mailFormat.php',$data,TRUE);
    $this->email->message($body);   
    $this->email->send();

和电子邮件格式页面mailFormat.php

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Employee Registration</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>
<body>
<div>
 <div style="font-size: 26px;font-weight: 700;letter-spacing: -0.02em;line-height: 32px;color: #41637e;font-family: sans-serif;text-align: center" align="center" id="emb-email-header"><img style="border: 0;-ms-interpolation-mode: bicubic;display: block;Margin-left: auto;Margin-right: auto;max-width: 152px" src="<?php echo base_url(); ?>/images/logo.png" alt="WFT" width="152" height="108"></div>

 <p style="Margin-top: 0;color: #565656;font-family: Georgia,serif;font-size: 16px;line-height: 25px;Margin-bottom: 25px">Hey  <?php echo $EmpName;?>,</p> 
<p>Email Id - <?php echo $userName; ?> </p>
<p>Password - <?php echo $password; ?> </p>
<p>Contact Number - <?php echo $Number; ?> </p>
<p>City - <?php echo $City; ?> </p>
<p>State - <?php echo $State; ?> </p>
<p>Address - <?php echo $Address; ?> </p>

</div>
</body>
</html>

已发送电子邮件。但是显示 HTML 源代码的电子邮件格式。如何将其更改为 HTML 格式的电子邮件。

【问题讨论】:

  • 我正在使用 codeIgniter 电子邮件库。你能告诉我吗?如何写上面的代码@cakil
  • 抱歉,我错了,那么@Snickers 可能有答案
  • @cakil 您已将此问题标记为重复。

标签: php codeigniter email codeigniter-3


【解决方案1】:
$this->email->mailtype('html'); // This was incorrect, check below for what I used

Codeigniter Email Class

这是我不久前使用的一种方法,希望对您有所帮助

    //Setting up the email
    $config = array(
    'protocol' => 'smtp',
    'smtp_host' => 'smtp.host.com',
    'smtp_port' => 587,
    'smtp_user' => '******',
    'smtp_pass' => '******',
    'mailtype'  => 'html',
    'charset'   => 'iso-8859-1'
);
    $this->load->library('email', $config);
    $this->email->set_newline("\r\n");

    $data = array(
    'email'    => $user->user_email,
    'username' => $user->user_first_name,
    'token'    => $token,
);
    $this->email->from('*******', 'Title');
    $this->email->to($user->user_email);

    $this->email->subject('***** - Password Reset Request');
    $html = $this->load->view('help/email/forgot_password', $data, true);
    $this->email->message($html);

    $this->email->send();

所以我认为首先将它设置为变量是我修复它的方法。

【讨论】:

  • 显示错误未定义方法 CI_Email::mailtype()
  • 您可能需要初始化它,CI 2.0.3 曾经有一个错误,我不确定您正在运行哪个版本,尽管您可以尝试使用此设置邮件类型$this->load->library('email'); $this->email->initialize(array('mailtype' => 'html'));
  • 谢谢。我使用了相同的代码,但它对我不起作用。
  • 我已经在生产环境中使用过它,所以它肯定能在我这边工作,也许您的邮件服务器设置为去除标签,或者配置为确保它只发送文本电子邮件。
  • 我认为你是对的。我必须检查服务器配置。
【解决方案2】:

您可以使用前面所说的 ma​​iltype 配置,但这里有更多详细信息以及如何正确操作。

设置电子邮件首选项

有 21 种不同的首选项可用于定制您的电子邮件 消息被发送。您可以按照此处所述手动设置它们, 或通过存储在配置文件中的首选项自动进行描述 下面:

通过将一组首选项值传递给 电子邮件初始化方法。这是一个示例,说明如何设置一些 偏好:

$config['protocol'] = 'sendmail';
$config['mailpath'] = '/usr/sbin/sendmail';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;

$config['mailtype'] = 'html';         // use this setting

$this->email->initialize($config);

Email Class

【讨论】:

    猜你喜欢
    • 2011-09-10
    • 2022-07-23
    • 1970-01-01
    • 2022-11-18
    • 2018-12-29
    • 1970-01-01
    • 1970-01-01
    • 2012-12-29
    • 1970-01-01
    相关资源
    最近更新 更多