0

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?

Vitor Freitas
  • 103
  • 1
  • 11

2 Answers2

2

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;

  • It worked!! I appreciate your help.. Can I do any style editing this way, maybe just skip a line between responses? Because they all came in the same line and tight – Vitor Freitas Dec 26 '20 at 02:33
  • 1
    Yes, you can use $body.= "

    Telephone: ". $result_SRV_ . "

    ";
    –  Dec 26 '20 at 02:35
  • Hey Artem, I asked another question related to this one, can you take a look and see if you can help? https://stackoverflow.com/questions/65457345/select-information-from-the-database-for-a-page-accessed-by-different-users – Vitor Freitas Dec 26 '20 at 18:09
0

Please try this:

<?php

require  'email/PHPMailerAutoload.php';

extract($_POST);

$result = {something};

$mail = new PHPMailer;


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

$body = "";

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

$body = json_encode($body);

$mail ->Body .= $body;

  • it worked well with one variable, but when I put the other variables that way, it didn't work. 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? – Vitor Freitas Dec 26 '20 at 02:14
  • @VitorFreitas but your question code only include the one var. So, Artem's answer is correct based on the details you supplied. – GetSet Dec 26 '20 at 02:15
  • Please let me know all the possible situations which you want to fiter. –  Dec 26 '20 at 02:18