使用代码点火器和SendGrid向电子邮件添加HTML标记


Adding HTML Tags to an Email Using Code Igniter and SendGrid

我在谷歌上搜索了一个解决方案,但什么都找不到。

我正在使用代码点火器框架和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.

对于您似乎正在使用的Code Igniter的电子邮件类,初始化时必须将mailtype设置为html,否则默认为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了。

看看

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

希望它能解决你的问题。

$message变量的内容是什么?尝试在$message中定义完整的HTML(就像通常一样,带有您想要的链接,例如:<a href="your_link">Your link</a>),因为它是作为电子邮件的正文(消息)发送的。

不需要额外的库。初始化时只需添加"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"
    ));

这对我有效

$this->load->library('email');
$config['mailtype'] = 'html';
$config['protocol'] = 'sendmail';
$this->email->initialize($config);