Search code examples
phphtml-email

PHP mail() ' (single quote) in subject being escaped


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


Solution

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