Home > Blockchain >  Format before mailing contents of .txt file using php
Format before mailing contents of .txt file using php

Time:03-19

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 = '[email protected]';         /* 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 = '[email protected]';             /* Gmail */
$mail->FromName = 'ZiloreMumba';              /* Nombre de usuario Gmail */
$mail->addAddress('[email protected]', 'Zilore Mumba');     /* Add a recipient */
// $mail->addAddress('[email protected]');    /* Name is optional */
// $mail->addCC('[email protected]');
// $mail->addBCC('[email protected]');

// $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';
 }
?>

CodePudding user response:

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.
  •  Tags:  
  • php
  • Related