PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, July 7, 2022

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

 July 07, 2022     php, phpmailer     No comments   

Issue

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;



Answered By - user14860400
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing