-1

I have the following code which I adapted from one of the posts. It sends mail to my email address ok, which is text from a file, as the body of the email. The problem I have is that the text loses all lines breaks when it comes into php. I have read many ways of introducing lines breaks, but my problem is the body of the text is very sensitive what is presented. I have tried two ways, one with implode and the other enclosing the body within <pre></pre> tags. both do not work. Here is my code

<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require '/home/zmumba/PHPMailer/src/Exception.php';
require '/home/zmumba/PHPMailer/src/PHPMailer.php';
require '/home/zmumba/PHPMailer/src/SMTP.php';

$mail = new PHPMailer();                      // create a new object
$mail->SMTPDebug = 2;                         /* Enable verbose debug output */

$mail->isSMTP();                              /* Set mailer to use SMTP */
$mail->Host = 'smtp.gmail.com';               /* Specify main and backup SMTP servers */
$mail->SMTPAuth = true;                       /* Enable SMTP authentication */
$mail->Username = 'zmumba@gmail.com';         /* SMTP username */
$mail->Password = 'app~password';         /* SMTP password */
$mail->SMTPSecure = 'tls';                    /* Enable TLS encryption, 'ssl' also accepted */
$mail->Port = 587;                            /* TCP port to connect to */
$mail->Helo = 'localhost';                    /* Permite usar EHLO / HELO */
$mail->Hostname = 'gmail.com';                /* Permite usar un hostname */

$mail->From = 'zmumba@gmail.com';             /* Gmail */
$mail->FromName = 'ZiloreMumba';              /* Nombre de usuario Gmail */
$mail->addAddress('zmumba@gmail.com', 'Zilore Mumba');     /* Add a recipient */
// $mail->addAddress('ellen@example.com');    /* Name is optional */
// $mail->addCC('cc@example.com');
// $mail->addBCC('bcc@example.com');

// $mail->addAttachment('/var/tmp/file.tar.gz');         /* Add attachments / agregar un archivo*/
// $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    /* Optional name */

#$file = 'weather.txt';
    #$contents = file($file); 
    #$contents = file_get_contents($file); 
    #$string = implode($contents); 
#echo $contents;
#exit;

$mail->isHTML(true);                               /* Set email format to HTML */
$mail->Subject = 'Test mail on severe Weather';    /* Subject */

#$contents      = file_get_contents("/home/zmumba/DA/Send_Mail/weather.txt");
#$mail->Body    = implode("<br>", $contents);                        /* Message body  */

$raw = file_get_contents("/home/zmumba/DA/Send_Mail/weather.txt");
echo '<pre>';
$mail->Body    = str_replace($raw, array('>','<','&','%'), array('&gt;','&lt;','&amp;','&#37;'));
echo '</pre>';

#$mail->Body    = file_get_contents("/home/zmumba/DA/Send_Mail/weather.txt");

 if(!$mail->send()) {
  echo 'Message could not be sent.';
  echo 'Mailer Error: ' . $mail->ErrorInfo;
 } else {
  echo 'Message has been sent';
 }
?>
Moshe Katz
  • 15,992
  • 7
  • 69
  • 116
Zilore Mumba
  • 1,346
  • 4
  • 23
  • 33
  • _"The problem I have is that the text looses all lines breaks when it comes into php"_ - no, it loses them when it "comes into" _HTML_. – CBroe Mar 18 '22 at 12:49
  • Does this answer your question? [PHP - how to create a newline character?](https://stackoverflow.com/questions/4238433/php-how-to-create-a-newline-character) – CBroe Mar 18 '22 at 12:50
  • `echo
    ` that you have does not affect the body of the message at all, only what is output when you run the code.
    – Moshe Katz Mar 18 '22 at 13:01
  • just a
    to have new lines
    – 丹尺爪工れ Mar 18 '22 at 13:01
  • Can you show us the content of the text file? – Moshe Katz Mar 18 '22 at 13:01
  • @CBroe that question about new lines in PHP won't help for the content of a file – Moshe Katz Mar 18 '22 at 13:02
  • @MosheKatz applying `nl2br` as suggested in some of those answers, will definitively help. (There might be better duplicates for this, but I didn't find one on a quick search.) – CBroe Mar 18 '22 at 13:04
  • 1
    https://stackoverflow.com/questions/6706461/why-does-php-echod-text-lose-its-formatting might be a better duplicate to close this with, agreed. – CBroe Mar 18 '22 at 13:05
  • Does this answer your question? [Why does PHP echo'd text lose its formatting?](https://stackoverflow.com/questions/6706461/why-does-php-echod-text-lose-its-formatting) – Moshe Katz Mar 18 '22 at 13:09

1 Answers1

0

Does your text file actually contain HTML in it? If not, change $mail->isHTML(true) to $mail->isHTML(false) and then the email will be sent in plain text, which will make the line breaks in the text file stay in the email.

If the text file actually does contain HTML, you have two options:

  1. Use the PHP function nl2br to convert all line breaks to HTML <br> tags.
  2. Put <br> or <p>...</p> tags in the text file in the appropriate places.
Moshe Katz
  • 15,992
  • 7
  • 69
  • 116
  • thanks for knowledge sharing, My text file contains plain text not html. The solution was as simple as changing $mail->isHTML(true) to $mail->isHTML(false). The problem is solved. I really appreciate. – Zilore Mumba Mar 18 '22 at 15:10