Jump to content

PHP text email message - remove part of message if no data or blank


Go to solution Solved by Barand,

Recommended Posts

Hi,

I am using PHP to send an text email message after a form is submitted

Use mail at the moment, to send a text email for example:

$subject = 'Subject';
$message = 'Message goes here' . "\r\n" .
'Name: ' . $name . "\r\n" .
'Does this have data: ' . $doesthishavedata  . "\r\n" .
'something else: ' . $hasdata  . "\r\n" .
'------------------------------------';

$to = '[email protected]';
$from = '[email protected]';
$headers = 'From: ' . $from . '' . "\r\n" . 'Reply-To: ' .  $from . '' . "\r\n" . 'X-Mailer: PHP/' . phpversion();

mail ($to, $subject, $message, $headers);

 

As you can see Does this have data part, if it does not how can I remove it from being sent in the email?
 

Thanks

 

Edited by CBG
9 minutes ago, Barand said:

An "if() construct" often comes in useful at times like this.

I have tried if like this

$subject = 'Subject';
$message = 'Message goes here' . "\r\n" .
'Name: ' . $name . "\r\n" .
if ($doesthishavedata != ''){
'Does this have data: ' . $doesthishavedata  . "\r\n" .
}
'something else: ' . $hasdata  . "\r\n" .
'------------------------------------';

But get an error PHP Parse error:  syntax error, unexpected 'if' (T_IF)
Now if there away to break up the message into bits, then I could most likely use an if

Would this work to break the message up

$subject = 'Subject';
$message = 'Message goes here';
$message .= 'Name: ' . $name;
$message .= 'Does this have data: ' . $doesthishavedata;
$message .= 'something else: ' . $hasdata;
$message .= '------------------------------------';

 

Edited by CBG
  • Solution

You can't concatenate an if() statement like that. Try

$message = 'Message goes here' . "\r\n" .
'Name: ' . $name . "\r\n";

if ($doesthishavedata != '') {
    $message .= 'Does this have data: ' . $doesthishavedata  . "\r\n";
}

$message .= 'something else: ' . $hasdata  . "\r\n" .

 

  • Thanks 1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.