简体   繁体   中英

Adding HTML Tags to an Email Using Code Igniter and SendGrid

I searched Google for a solution but was unable to find anything.

I am using the Code Igniter Framework and SendGrid to do SMTP. My script is as follows:

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

However when I receive the email, it just contains the HTML as plain text, like such:

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

With Code Igniter's Email Class , which you appear to be using, you must set your mailtype to html when initializing, otherwise it defaults to text . Change your initialize function to:

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

You'll then be able to send HTML using all your other code.

What is the content of $message variable? Try defining complete HTML (as you normally would, with links you want, eg.: <a href="your_link">Your link</a> ) in $message since it is sent as body (message) of the email.

Have a look

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

Hope it will solve your problem.

No need for additional libraries. Simply add 'mailtype' => 'html' when you initalize

    $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 worked for me

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM