Search code examples
phpphpmailer

How do I put an if statement inside the body of a PHPMailer code?


I'm trying to put an if statement inside the body along with the html, but it's not working. I'm using PHPMailer, is something wrong with the code?

<?php

require  'email/PHPMailerAutoload.php';

extract($_POST);

$mail = new PHPMailer;


$mail ->isHTML(true);
$mail ->CharSet = 'UTF-8';

$mail ->Body .="

if($result != null){
    echo "Name: " $result
}

";

I tried it the way Artem Medianyk said and it worked, but when I put the other variables that way, it didn't work.

$body = '';

if ($result_SRV_AA_A10_name_go != null) {
  $body = "Name: " . $result_SRV_AA_A10_name_go;
}

if ($result_SRV_AA_A10_email_go != null) {
  $body = "E-mail: " . $result_SRV_AA_A10_email_go;
}

if ($result_SRV_AA_A10_tel_go != null) {
  $body = "Telefone: " . $result_SRV_AA_A10_tel_go;
}

if ($result_SRV_AA_B9_name_go != null) {
  $body = "Name: " . $result_SRV_AA_B9_name_go;
}

if ($result_SRV_AA_B9_email_go != null) {
  $body = "E-mail: " . $result_SRV_AA_B9_email_go;
}

if ($result_SRV_AA_B9_tel_go != null) {
  $body = "Telefone: " . $result_SRV_AA_B9_tel_go;
}

$body = json_encode($body);

$mail ->Body .= $body;

My idea is to check the answers that have been filled out and then send them in the best way by email. what could i do to achieve this?


Solution

  • Please try this again:

    $body = '';
    
    if ($result_SRV_AA_A10_name_go != null) {
      $body .= "Name: " . $result_SRV_AA_A10_name_go;
    }
    
    if ($result_SRV_AA_A10_email_go != null) {
      $body .= "E-mail: " . $result_SRV_AA_A10_email_go;
    }
    
    if ($result_SRV_AA_A10_tel_go != null) {
      $body .= "Telefone: " . $result_SRV_AA_A10_tel_go;
    }
    
    if ($result_SRV_AA_B9_name_go != null) {
      $body .= "Name: " . $result_SRV_AA_B9_name_go;
    }
    
    if ($result_SRV_AA_B9_email_go != null) {
      $body .= "E-mail: " . $result_SRV_AA_B9_email_go;
    }
    
    if ($result_SRV_AA_B9_tel_go != null) {
      $body .= "Telefone: " . $result_SRV_AA_B9_tel_go;
    }
    
    $mail ->Body .= $body;