php_begins Posted October 3, 2011 Share Posted October 3, 2011 I need to send emails with single apostrophes in the body of my email function. How do I do that without erroring out in my mail function. <? $to = "[email protected]"; $subject = "test"; $body='<html><body><font size="1"> We're sending this email.It's been a while</font></body></html>'; $message=$total; $from = "[email protected]"; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= 'From: Admin <[email protected]>' . "\r\n"; // Send email if(mail($to,$subject,$body,$headers)) { // Inform the user echo "Your email has been sent"; } else { echo "MAIL not sent"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/248346-trouble-sending-email-with-apostrophe/ Share on other sites More sharing options...
Psycho Posted October 3, 2011 Share Posted October 3, 2011 If you want to define the string using sigle apostrophes, then you need to escape any single apostrophes in the content $body='<html><body><font size="1"> We\'re sending this email. It\'s been a while</font></body></html>'; Of course, you could define the string using double apostrophes, but then you need to escape those in the content. $body="<html><body><font size=\"1\"> We're sending this email. It's been a while</font></body></html>"; Then again, you can use the heredoc or nowdoc methods for defining the string. Lots of info here: http://php.net/manual/en/language.types.string.php Quote Link to comment https://forums.phpfreaks.com/topic/248346-trouble-sending-email-with-apostrophe/#findComment-1275324 Share on other sites More sharing options...
php_begins Posted October 3, 2011 Author Share Posted October 3, 2011 thanks,how do I escape it if i dynamically receive the html content. for example, $body=$content_html; When the email is received I would like to have the apostrophes displayed. Quote Link to comment https://forums.phpfreaks.com/topic/248346-trouble-sending-email-with-apostrophe/#findComment-1275326 Share on other sites More sharing options...
Psycho Posted October 3, 2011 Share Posted October 3, 2011 thanks,how do I escape it if i dynamically receive the html content. for example, $body=$content_html; When the email is received I would like to have the apostrophes displayed. If $content_html already includes the apostrophes, you don't need to do anything. It is only when you define a string with single apostrophes and you want to put single apostrophes in the content. Quote Link to comment https://forums.phpfreaks.com/topic/248346-trouble-sending-email-with-apostrophe/#findComment-1275345 Share on other sites More sharing options...
php_begins Posted October 3, 2011 Author Share Posted October 3, 2011 sorry, I meant $content_html will dynamically generate the html source code which I am putting in the $body variable. for example, $content_html='<html><body><font size="1"> We're sending this email.It's been a while</font></body></html>'; How do i escape the apostrophe in that variable dynamically(since the html source is huge..it could contain many apostrophes) And when I receive the email i would need the apostrophes to be there.. Quote Link to comment https://forums.phpfreaks.com/topic/248346-trouble-sending-email-with-apostrophe/#findComment-1275351 Share on other sites More sharing options...
Psycho Posted October 3, 2011 Share Posted October 3, 2011 I already showed you how to escape the apostrophes in the content in the scenario you provided. If you are dynamically defining the content some other way you may, or may not, need to do any escaping. I would not be able to know what you should (or should not do) without seeing how you are actually defining the content. If you were, for example, defining the content from a DB query, you wouldn't need to do any escaping. Quote Link to comment https://forums.phpfreaks.com/topic/248346-trouble-sending-email-with-apostrophe/#findComment-1275356 Share on other sites More sharing options...
Deoctor Posted October 4, 2011 Share Posted October 4, 2011 guess this is what you are looking for function replace($text){ $text1 = preg_replace('/\'/','/\\\'/',$text); echo "text --> ".$text1."\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/248346-trouble-sending-email-with-apostrophe/#findComment-1275524 Share on other sites More sharing options...
Bottyz Posted October 4, 2011 Share Posted October 4, 2011 You could just use string replace to change all ' to ". $content_html=str_replace("'", '"', $content_html); Either that or convert all ' to its entity value: $content_html=str_replace("'", "'", $content_html); Quote Link to comment https://forums.phpfreaks.com/topic/248346-trouble-sending-email-with-apostrophe/#findComment-1275554 Share on other sites More sharing options...
PFMaBiSmAd Posted October 4, 2011 Share Posted October 4, 2011 @ym_chaitu and Bottyz, please read the problem. He is typing a literal string in the code. It is a fatal php parse error to put a single-quote inside of an overall single-quoted string because the syntax is broken. No amount of code to replace anything in the string will solve the problem because the code is never executed due to the fatal parse error. The solution, as already given, is to properly form the string, either by escaping the single quote in it or by changing the initial/final quotes around the string to double-quotes. Quote Link to comment https://forums.phpfreaks.com/topic/248346-trouble-sending-email-with-apostrophe/#findComment-1275563 Share on other sites More sharing options...
php_begins Posted October 4, 2011 Author Share Posted October 4, 2011 To escape the single apostrophe's the php addslashes() function should work right? But while displaying it in my email, I would still the apostrophes to be shown. Quote Link to comment https://forums.phpfreaks.com/topic/248346-trouble-sending-email-with-apostrophe/#findComment-1275572 Share on other sites More sharing options...
Psycho Posted October 4, 2011 Share Posted October 4, 2011 I don't think you are understanding what I have said previously. If you are defining a string using single quotes - then you need to escape any single quotes in that content. The escape character, the backslash, is not part of the content - it is only telling the PHP parser that the quote is not the end of the string being defined. If you go willy-nilly adding escape characters when you shouldn't be then you WILL be adding the backslash to the content. Here are some examples: $var = 'You need to escape single quotes (i.e. O\'Reilly) defined in single quotes'; echo $var; //Output: You need to escape single quotes (i.e. O'Reilly) defined in single quotes $var = "You shouldn\'t escape single quotes not defined in single quotes"; echo $var; //Output: You shouldn\'t escape single quotes not defined in single quotes Quote Link to comment https://forums.phpfreaks.com/topic/248346-trouble-sending-email-with-apostrophe/#findComment-1275732 Share on other sites More sharing options...
the182guy Posted October 5, 2011 Share Posted October 5, 2011 As mj said, you don't need to do any escaping when the email body is dynamically generated from somewhere. Quote Link to comment https://forums.phpfreaks.com/topic/248346-trouble-sending-email-with-apostrophe/#findComment-1275917 Share on other sites More sharing options...
Andy-H Posted October 5, 2011 Share Posted October 5, 2011 Use a heredoc, no need to escape anything, supports variable interpolation. $html = <<<HERE <html> <head><title>$title</title></head> <body>$body</body> </html> HERE; Quote Link to comment https://forums.phpfreaks.com/topic/248346-trouble-sending-email-with-apostrophe/#findComment-1275932 Share on other sites More sharing options...
php_begins Posted October 5, 2011 Author Share Posted October 5, 2011 Thanks Andy and MJ...it took some time for me to grasp that issue..I have understood the concept now. Quote Link to comment https://forums.phpfreaks.com/topic/248346-trouble-sending-email-with-apostrophe/#findComment-1276051 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.