1

I am trying to send emails using php mail() function. My code is below.

$subject = "Let's Connect";
$to = $_POST['to'];
$message = $_POST['message'];

mail( $to, $subject, $message, array('Content-Type: text/html; charset=UTF-8'));  

The problem is ' in the subject line becomes \' in email clients such as Gmail and Yahoo. The Let's Connect becomes Let\'s Connect. I have tried several solutions here, like

$sub = '=?UTF-8?B?'.base64_encode($subject).'?=';
$decoded_str = html_entity_decode ( $value_to_decode, ENT_QUOTES, 'UTF-8' );

None of them seems to work. What should I do to get it solved?

Thaks

Synchro
  • 35,538
  • 15
  • 81
  • 104
Abhik
  • 664
  • 3
  • 22
  • 40

2 Answers2

3

I would use stripslashes() and html_entity_decode()

mail( $to, stripslashes(html_entity_decode($subject, ENT_QUOTES, 'UTF-8' )), $message, array('Content-Type: text/html; charset=UTF-8')); 
Sanan Guliyev
  • 711
  • 5
  • 11
1

Either use double quotes like this:

$subject = "Let's Connect";

Or you can use slash to skip it

$subject = 'Let\'s Connect';

You can pass stripslashes($subject)

Exterminator
  • 1,221
  • 7
  • 14
  • My bad, yes, I was using double quotes in my development code. Mistakenly used single quote while typing this question. Edited my question. – Abhik Oct 09 '18 at 12:18