We help IT Professionals succeed at work.

Formatting mail() output - random characters appearing (! and \)

SteveFerson asked
Last Modified: 2013-12-03
I'm trying to write a simple script to allow the owner of a website I'm developing send out weekly updates of scores and match reports.  However I'm having a few problems with the formatting.

Firstly, the when I sent formatted HTML through the system it seemed to arrive with random exclamation marks (!) in it.  About half way down it eventually disrupted a closing </h3> tag and resulted in the remainder of the email being formatted in big white writing on a blue background.

Even in plain text, anywhere where there is an apostrophe (') it is immediately preceded by a backslash.  I know this is an escape character in regexes but I don't know why it's getting through to the end result.  The code is below:

$headers = "From: CATTL Online<list@antrimttl.org.uk>\nBcc: $subscribers\n";
            if ($_POST['type'] == "html") {
                  $headers = $headers . "Content-type: text/html\n";
                  $msg = "<html><head><style> <snip> STYLE RULES </snip></style></head><body>";

                  $s = preg_replace("/\\n/", "<br/>",$_POST['msg']);

                  $msg = $msg . '<div id="head">(picture and link)</div>' . $s ."<p>CATTL Online</p>";
                  $msg = $msg . '<p style="font-size: smaller">--<br/>You are receiving this email because you subscribed to CATTL Online\'s NewsLetter.  If you no longer wish to receive these messages, email webmaster@antrimttl.org.uk and ask to be removed.</p></body></html>';
            } else {
                  $headers = $headers . "Content-type: text/plain\n";
                  $msg = $_POST['msg'] . "\nCATTL Online\n\n--\nYou are receiving this email because you subscribed to CATTL Online's Newsletter.  If you no longer wish to receive these messages, email webmaster@antrimttl.org.uk and ask to be removed.";
            mail('CATTL Subscribers<list@antrimttl.org.uk>', $_POST['subject'], $msg, $headers)       or $failed = true;
Watch Question

This one is on us!
(Get your first solution completely free - no credit card required)


OK, but it's quite  longwinded:

<html><head><style>body {font-family: verdana,sans-serif; font-size: x-small;} div#head {height: 82px; background: #006699;      border: 2px outset #006699; margin: 0;} img {border: 0;} h1 {font-size: 200%;} h2 {font-size: 150%; border-bottom: 1px solid #006699;} h3 {font-size: 110%; background: #006699; color: #ffffff;} h3 span.score { color: #cccccc;}</style></head><body><div id="head"><a href="http://antrimttl.org.uk/"><img src="http://www.antrimttl.org.uk/gfx/newlogo.png" style="display: block;"></a></div><p>Dear Subscribers,
<br/>Just a little introduction.  This week sees the first of our match reports being emailed to all subscribers.  From now on (hopefully) you should see the match reports in your inbox before they hit the press! Dont forget if you see your name here and want the report in print, it should ordinarily be out by midweek in the East Antrim Times, Ballymena Guardian and also, I believe, the Carrick Advertiser.  As usual, any feedback on the idea is app!
reciated to the usual address, webmaster@antrimttl.org.uk</p><p>Regards,
<br/>Steve Ferson (Web Manager)</p>
<br/><h1>KNOCKAGH NICK IT!</h1><h2>DIVISION 1</h2><h3>Knockagh <span class=score> 6 - 4 </span>   Turfahun</h3><p>League leaders Knockagh nearly came unstuck in this match against Bushmills side Turfahun.
<br/>The visitors played, arguably, their strongest side consisting of David Nicholl, Francis Shirley and Jim Elliott. The home side was made up of Harry Ferson, Alvis Wright and Gary Ferson.
<br/>Turfahun got off to a flying start with Nicholl defeating Harry Ferson and Shirley defeating Wright. This shock to the system for the home side spurred a positive reaction, however, with Gary Ferson pulling one back by defeating Elliott. This was followed by wins for Harry Ferson over Shirley and Wright over Elliott before Nicholl levelled matters at 3 - 3 when he beat Gary Ferson.
<br/>Harry Ferson beat Elliott but Wright lost to that man Nicholl and the sides were level !
again at 4 - 4.
<br/>The last singles game was, without doubt, the match of the night with the big-hitting Gary Ferson battling against Shirley. Ferson went a game up but Shirley levelled, Ferson went 2 - 1 up but Shirley levelled again. In the final set Ferson went into overdrive and was 9 - 0 ahead before finally clinching the match 11 - 3.
<br/>The two Fersons paired up in the doubles and clinched both points for the home side with a straight-sets win over Nicholl and Shirley.
<br/>Knockagh stay top, but only just.</p><h3>Carnmoney A <span class=score> 6 - 4 </span>   Larne A</h3><p>Second placed Carnmoney A had to work every bit as hard as leaders Knockagh did against Turfahun to overcome a spirited Larne A side.
<br/>Amanda Mogey got the home side off to a good start when she defeated Robert Ramsey after five hard sets. Yet another five-setter saw David Campbell make it 2 - 0 to the home side when he beat Neil McRandal. Larne pulled one back with a Brian Edgar win over !
Wayne Charlton - yet again the win coming in five sets.
<br/>Mogey opened a two match gap with a straight sets win over McRandal but Larne fought back with wins for Edgar over Campbell and Ramsey over Charlton.
<br/>Carnmoney got their noses back in front when Mogey beat Edgar in another five-setter and Campbells defeat of Ramsey ensured that Carnmoney had at least one point from the match.
<br/>McRandal kept the pot boiling, though, when he beat Charlton in yet another five set marathon to leave the match score at 5 - 4 with just the doubles remaining.
<br/>Mogey and Campell steered the Carnmoney ship home to both points with a four-set win against Ramsey and Edgar.</p><h2>DIVISION 2</h2><h3>Doagh B <span class=score> 6 - 4 </span>   Ballyclare Methodists B</h3><p>This re-arranged fixture turned out to be a bit of a cliff-hanger.
<br/>Doagh B kept up the pressure at the top of Division 2 - but only by a whisker.
<br/>Colin Clements gave Doagh a winning start by defeating B!
ob McConnell. Jonathan Mogey kept up the home momentum with a victory over Ken Beggs (sr) but Ken Beggs (jr) put the visitors on the score-card when he defeated Hugh Duddy.
<br/>Further home wins for Clements over George Dodds and Mogey over McConnell had the home side 4 - 1 up and looking good for both points.
<br/>Two consecutive wins for the visitors changed that complexion totally though.
<br/>First Beggs (sr) beat Duddy and then Beggs (jr) beat Clements and suddenly it was game on again.
<br/>Mogey steadied matters for Doagh with a defeat of Dodds but McConnell kept the visitors in with the chance of a point when he beat Duddy.
<br/>Clements and Mogey paired up for the concluding doubles and took on McConnell and Beggs (sr) for the visitors. The home pair just had enough in the tank to take the game but the score-line of 11 - 9, 7 - 11, 17 - 15, 12 - 10 says it all.
<br/>A terrific encounter!</p><h3>Carnmoney 92 D  <span class=score> 0 - 10 </span>   Carnmoney 92 C </h3!
><p>Yet another re-scheduled fixture but not quite a cliff-hanger in this one.
<br/>The C-team trio of Leslie Forrester, Geraldine Craig and Ryan Lowry were made to fight by the D-side trio of Hugh Patterson, Peter Trimble and Brian Trimble but the result was never really in doubt from the moment the first ball was struck.
<br/>Brian Trimble can consider himself particularly unfortunate after taking both Ryan Lowry and Leslie Forrester to a fifth and deciding set before succumbing to both.
<br/>Similarly Hugh Patterson will be a little disappointed that his encounter with Lowry also ended up in a five-set defeat.
<br/>Geraldine Craig was in superb form for the C side and did not drop a set in the entire match.</p>
<br/><h2>DIVISION 3</h2><h3>Legacurry  <span class=score>2 - 8</span> Ballyclare High School B</h3><p>The result of this re-arranged fixture has kept alive the challenge of the High School side for the Division 3 title. Their talented trio consisted of Simon Baxter!
, Alastair Bell and Alan Blain and they were pitched against a spirited Legacurry threesome of Ryan Patterson, Patrick Ferguson and Tim Ferguson.
<br/>Baxter gave the visitors a great start with a win over Patterson and Bell followed this up with a victory over Patrick Ferguson.
<br/>Tim Ferguson pulled one back for the home side when he beat Blain but wins for Bell over Patterson, Blain over Patrick Ferguson and Baxter over Tim Ferguson dismissed any thoughts of an upset.
<br/>Even when Patterson beat Blain to make the match score 2 - 5 there was never any real doubt as to who the victors would be.
<br/>Wins for Baxter over Patrick Ferguson and Bell over Tim Ferguson finished off the singles and the same pair defeated Patterson and Patrick Ferguson in the doubles to give the Ballyclare side an 8 - 2 win.</p><h3>Carnmoney 92 E  <span class=score> 3 - 7 </span>   Ballyclare High School</h3><p>Ashleigh McBride was back for the school side and made an immediate impact by infli!
cting a defeat on the home sides talented youngster, Simon McIntyre.
<br/>Simon Baxter then beat David Kerr and Alistair Bell beat John McIntyre to put the visitors three up.
<br/>In what proved to be the match of the night, and arguably the key moment of the match, Baxter won a terrific match 15 - 13 in the fifth and final set against Simon McIntyre to put the school side 4 - 0 ahead.
<br/>David Kerr pulled one back for Carnmoney with a defeat of Bell but McBrides defeat of John McIntyre restored the four match gap.
<br/>Simon McIntyre beat Bell to give a glimmer of hope to the home side but a McBride victory in four sets over Kerr followed by a four set win for Baxter over John McIntyre put the away side 7 - 2 up with only the doubles to play.          
<br/>Anyone watching the doubles would not have believed that there was nothing at stake other than pride. It turned out to be a terrific five-set marathon with Simon McIntyre and Kerr just pipping McBride and Baxter for t!
he visitors.
<br/>This win keeps the school side hot on the heels of leaders Knockagh C.</p><p>CATTL Online</p><p style="font-size: smaller">--<br/>You are receiving this email because you subscribed to CATTL Online's NewsLetter.  If you no longer wish to receive these messages, email webmaster@antrimttl.org.uk and ask to be removed.</p></body></html>


I changed the HTML one also to

$s = nl2br(stripslashes($_POST['msg']));

and it seems to work ok - no exclamation marks.. I've no idea why and right at the minute I don't care anymore lol, just glad to have it working.  Thanks for the help.

maybe it was your client expressing its dislike for html tags which had slashes before the quotes, not sure really but as long as it works... good luck with the project



Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.