Jump to content

trouble sending email with apostrophe


php_begins

Recommended Posts

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 = "myself@gmail.com";
$subject = "test";
$body='<html><body><font size="1"> We're sending this email.It's been a while</font></body></html>';
$message=$total;

$from = "webmaster@example.com";
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Admin <Admin@test.com>' . "\r\n";


// Send email
if(mail($to,$subject,$body,$headers))
{
// Inform the user
  echo "Your email has been sent";
}
else
{
  echo "MAIL not sent";
}
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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..

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

@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.

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.