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('>','<','&','%'));
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';
}
?>
to have new lines – 丹尺爪工れ Mar 18 '22 at 13:01