-1

Everything is working properly, I'm having issue in $mail->body.

Issue is: if a user comes to the form and fill up only 2 text fields and submit that form, data will be record in database and an Email will be sent and all the other empty fields will also be shown on email. I dont want that empty fields.

I want only populated fields that contains data to be shown on email. empty fields should not be shown on email. How I can achieve this?

Any help will be appreciated...

Here is a php mailer code, where I have some issue.

<?php

    //Import PHPMailer classes into the global namespace
    //These must be at the top of your script, not inside a function
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\SMTP;
    use PHPMailer\PHPMailer\Exception;



if(isset($_POST['insert']))

    {
    //Load Composer's autoloader
    require 'phpmailer/Exception.php';
    require 'phpmailer/SMTP.php';
    require 'phpmailer/PHPMailer.php';

    //Create an instance; passing `true` enables exceptions
    $mail = new PHPMailer(true);

    try {
    //Server settings
                 
    $mail->isSMTP();                                            //Send using SMTP
    $mail->Host       = 'smtp.gmail.com';                       //Set the SMTP server to send through
    $mail->SMTPAuth   = true;                                   //Enable SMTP authentication
    $mail->Username   = 'hereIsEmail';                     //SMTP username
    $mail->Password   = '********';                               //SMTP password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;            //Enable implicit TLS encryption
    $mail->Port       = 465;                                    //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`

    //Recipients
    $mail->setFrom('hereIsEmail', 'Testing');       //mailer name
    $mail->addAddress('hereIsEmail', 'User');     //Add a recipient
   

    //Content
    $mail->isHTML(true);                                        //Set email format to HTML
    $mail->Subject = 'ABC';
    **$mail->Body    = 

    "First name : $firstname
    <br> Last name : $lastname
    <br> Company : $company
    <br> Email Address : $emailaddress
    <br> Phone Number : $phonenumber
    <br> Site Address : $siteaddress
    <br> Map Reference : $mapreference
    <br> Account No : $accountno
    <br> Order No : $orderno
    <br> Date Ordered : $dateordered
    <br> Date Required : $daterequired
    <br> Delivery/Pickup : $deliverypickup
    <br> Pickup Time : $pickuptime
    <br> Pass Up : $passup
    <br>10mmPLASTER_6000x1350 : $PLASTER_6000x1350_13mm
    <br>13mmPLASTER_6000x1200 : $PLASTER_6000x1200_13mm
    <br>BATTENS_129_6000 : $BATTENS_129_6000
    <br>BATTENS_301_6000 : $BATTENS_301_6000
    <br>BATTENS_308_6000 : $BATTENS_308_6000";
**

    $mail->send();
    echo 'Form Submitted Successfully';
    } catch (Exception $e) {
        echo "Form could not be sent. Mailer Error: {$mail->ErrorInfo}";
    } 
}
ADyson
  • 57,178
  • 14
  • 51
  • 63
  • 3
    This has nothing to do with email specifically. You just need some conditional logic to build up the message body string gradually and only concatenate a line onto it if the field is populated (which you can check using isset()). Have you actually tried anything? – ADyson Sep 01 '23 at 05:39
  • I've tried this one for one text field. but syntax error is shown on first line. if (!empty($firstname)) { echo "First name : $firstname"; } – Comsian_Guy Sep 01 '23 at 06:05
  • What should I write in $mail->body , so that I can achieve non empty fields data? – Comsian_Guy Sep 01 '23 at 06:11
  • @ADyson Do you know any peice of code? – Comsian_Guy Sep 01 '23 at 06:12
  • 1
    Why do you try to replace building a string with using `echo`? That are two completely different techniques – Nico Haase Sep 01 '23 at 06:29
  • `if (!empty($firstname)) { echo "First name : $firstname"; }` does not, by itself, cause a syntax error. The problem must be the way you used it. But it doesn't make sense anyway - why would you use echo? That will send output back to the browser, but you need to put it into a string. – ADyson Sep 01 '23 at 06:33

1 Answers1

1

You need to build up a string variable gradually using conditional logic, to that you only concatenate a line into it if the relevant value from the form is not empty. When you've finished, you can assign that string as the body of the email.

For example:

$message = "";
if (!empty($firstname)) $message .= "First name : $firstname";
if (!empty($lastname)) $message .= "<br>Last name : $lastname";
if (!empty($company)) $message .= "<br>Company : $company";
/* .... etc ... */
$mail->Body = $message;

Reference: How can I combine two strings together in PHP?

ADyson
  • 57,178
  • 14
  • 51
  • 63