Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am putting blocks of text through a cgi programme to create an email. The text blocks have line breaks in them so I change the line breaks to '\n' before feeding the string into the cgi programme, but when the email comes out it reads the '\n' literally rather than converting it to a line break

ie:
this is line one\nthis is line two

when what I want is:
this is line one
this is line two

how do I fix this?

Replies are listed 'Best First'.
Re: getting text to line break in an email
by azatoth (Curate) on Sep 27, 2001 at 12:31 UTC
      okay, first I put the text block from a database into a text file (requests.txt) and replace the html break tags with '\n'


      cat $HOME/housekeeping/requests.txt | sed -e 's/<br>/\\n/g' -e s/\&#39 +\;/\'/g > $HOME/housekeeping/requests2.txt


      this gives me a text file containing for example:

      Date : 2001-09-26 10:47:00
      Request No. 107
      Request Title: CHILDREN
      Request : 1. 13 year old twin boys\n2. A boy aged 13 years old standing facing the camera\n3. A girl aged 13 years old standing facing the camera\n4. A girl & a boy aged approx 13 years old standing in front of the Eiffel Tower, Paris\n5. A girl aged approx 11 years old on a beach in Greece(or could be anot her location as long as it is not too exotic)
      Format :
      Deadline : 2001-9-27
      Budget : £85 per pic used
      Name : Louise Edgeworth
      Company : Pearson Education
      Country : United Kingdom
      Telephone : 01279 793472
      Fax : 01279 793472
      Email : edgeworthl@aol.com

      Then I run the perl script and use this code to format the email:

      open(REQUESTS,"/home/picturesea/housekeeping/requests3.txt") || di +e "requests3 file not found"; # Open The Mail Program open(MAIL,"|$mailprog -t"); print MAIL "Bcc: $emails\n"; print MAIL "From: chrisbarton\@postmaster.co.uk (PICTURE SEARCH)\n +"; print MAIL "Subject: PICTURE SEARCH REQUESTS $date\n\n"; print MAIL "If you think you have suitable images, please CONTACT +THE RESEARCHER DIRECT.\n YOU DO NOT NEED TO REPLY TO THIS EMAIL.\n"; print MAIL "$date\n"; print MAIL "-" x 75 . "\n"; while ($requests = <REQUESTS>) { print MAIL "$requests"; } close(REQUESTS);


      this all works fine and the email is formatted fine, except the request text comes out:

      Request : 1. 13 year old twin boys\n2. A boy aged 13 years old standing facing the camera\n3. A girl aged 13 years old standing facing the camera\n4. A girl & a boy aged approx 13 years old standing in front of the Eiffel Tower, Paris\n5. A girl aged approx 11 years old on a beach in Greece(or could be anot her location as long as it is not too exotic)

      when what I want is:

      Request : 1. 13 year old twin boys
      2. A boy aged 13 years old standing facing the camera
      3. A girl aged 13 years old standing facing the camera
      4. A girl & a boy aged approx 13 years old standing in front of the Eiffel Tower, Paris
      5. A girl aged approx 11 years old on a beach in Greece(or could be another location as long as it is not too exotic

      ie the '\n's don't get translated as line breaks - any idea why this is?

        The problem comes from the sed command. As I'm no sed expert give perl a try:

        perl -pe "s/<br>/\n/g; s/\&#39;/'/g;"

        alex pleiner <alex@zeitform.de>
        zeitform Internet Dienste

        Guys, It was very difficult to figure how we can break the line text email. I have found a very simple solution just "break the line in your code" and it will do the same in the email as well. eg: I have done this in PHP ------------------------ message ="Hello World Now this is my next line message"; Message .= "Dear".$var.", again this next lien message" --------------------------- Thats it and it works fine with me.. Cheers, Azim
Re: getting text to line break in an email
by hopes (Friar) on Sep 27, 2001 at 12:41 UTC
    Could you post an example?

    I think you are changing breaks by '\n' instead "\n"
    Note that there is a difference (The interpolation)
    but an example could help to see what is wrong
    Example 1:
    $text = "How are you"; $break ='\n'; $text =~ s/\s/$break/g; print $text;
    will print:
    How\nare\nyou
    while
    Example 2:
    $text = "How are you"; $break ="\n"; $text =~ s/\s/$break/g; print $text;
    will print:
    How are you

    Hope this helps
Re: getting text to line break in an email
by derby (Abbot) on Sep 27, 2001 at 16:38 UTC
    AM,

    As projek21 pointed out, your problem is definetly with the sed command. Sed, from a perl perspective, always had strange semantics when it comes to patterns. Sed will not convert the two characters "\n" into the one character newline. Now there are ways around this limitation. One is to embed an actual newline like so:

    shell prompt$ sed -e 's/<br>/\ > /g' < file.txt > file.out
    (note that '>' character is not something you type in but whatever your shell's subshell prompt is.)

    But that's kinda wacky. You could also place your sed patterns in a file and use the -f option of sed:

    file sedpat contains:

    s/<br>/\ /g
    and the command is sed -f sedpat file.txt > file.out But I would dump the sed stuff all together and just put the replacment within your perl script (and using default $_):
    while (<REQUESTS>) { s/<br>/\n/g; s/\&#39;/'/g; print MAIL; }
    -derby