1

我在 Google 上搜索了解决方案,但找不到任何东西。

我正在使用 Code Igniter Framework 和 SendGrid 来执行 SMTP。我的脚本如下:

$this->email->initialize(array(
    'protocol' => 'smtp',
    'smtp_host' => 'smtp.sendgrid.net',
    'smtp_user' => 'MY_SENDGRID_USERNAME',
    'smtp_pass' => 'MY_SENDGRID_PASSWORD',
    'smtp_port' => THE_PORT_I_AM_USING,
    'crlf' => "\r\n",
    'newline' => "\r\n"
));     

$this->email->from('info@gmail.com', 'Info');
$this->email->to("example@example.com");
$this->email->subject("My subject");
$message = "<p>Hello ...</p>
<a href="http://google.com">Click here</a> to go Google.";
$this->email->message($message);
$this->email->send();

但是,当我收到电子邮件时,它只包含纯文本形式的 HTML,如下所示:

<p>Hello ...</p>
<a href="http://google.com">Click here</a> to go Google.
4

5 回答 5

1

使用您似乎正在使用的 Code Igniter 的 Email Class,您必须mailtypehtml初始化时将其设置为 ,否则默认为text. 将初始化函数更改为:

$this->email->initialize(array(
    'protocol' => 'smtp',
    'smtp_host' => 'smtp.sendgrid.net',
    'smtp_user' => 'SENDGRID_USERNAME',
    'smtp_pass' => 'SENDGRID_PASSWORD',
    'smtp_port' => WHATEVER_PORT_YOURE_USING,
    'mailtype' => 'html',
    'crlf' => "\r\n",
    'newline' => "\r\n"
));

然后,您将能够使用所有其他代码发送 HTML。

于 2013-07-30T22:36:31.200 回答
0

$message变量的内容是什么?尝试定义完整的 HTML(像往常一样,使用所需的链接,例如:)<a href="your_link">Your link</a>$message因为它是作为电子邮件的正文(消息)发送的。

于 2013-07-30T08:27:00.360 回答
0

看一看

url = 'http://sendgrid.com/';
$user = 'USERNAME';
$pass = 'PASSWORD'; 

$params = array(
    'api_user'  => $user,
    'api_key'   => $pass,
    'to'        => 'example3@sendgrid.com',
    'subject'   => 'testing from curl',
    'html'      => 'testing body',
    'text'      => 'testing body',
    'from'      => 'example@sendgrid.com',
  );


$request =  $url.'api/mail.send.json';

// Generate curl request
$session = curl_init($request);
// Tell curl to use HTTP POST
curl_setopt ($session, CURLOPT_POST, true);
// Tell curl that this is the body of the POST
curl_setopt ($session, CURLOPT_POSTFIELDS, $params);
// Tell curl not to return headers, but do return the response
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);

// obtain response
$response = curl_exec($session);
curl_close($session);

// print everything out
print_r($response);

希望它能解决你的问题。

于 2013-07-30T10:13:25.177 回答
0

不需要额外的库。初始化时只需添加 'mailtype' => 'html'

    $this->email->initialize(array(
      'protocol' => 'smtp',
      'smtp_host' => 'smtp.sendgrid.net',   
      'smtp_user' => 'xxxx',
      'smtp_pass' => 'xxxx',
      'smtp_port' => 587,
      'mailtype' => 'html',
      'crlf' => "\r\n",
      'newline' => "\r\n"
    ));
于 2013-11-13T22:58:41.970 回答
0

这对我有用

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

$config['mailtype'] = 'html';
$config['protocol'] = 'sendmail';

$this->email->initialize($config);
于 2016-02-03T18:41:36.360 回答