Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

php constant in mail header 1

Status
Not open for further replies.

PeDa

Technical User
Oct 10, 2002
227
0
0
NL
The following script, in which the 'From' parameter is defined in a variable, works as expected:

Code:
<html>
<title>Test mail</title>
<body>
<?php
define("INFO", "info@mysite.nl");
$from="info@mysite.nl";
$recipient = 'fred@bloggs.nl';
$subject='Test subject';
$body='hello from info';
$headers = "From: Joe Bloggs <{$from}>";
//$headers = "From: Joe Bloggs <{INFO}>";
mail($recipient, $subject, $body, $headers);
?>
</body>
</html>

However if I replace $headers with the version using a constant no mail gets sent. Any suggestions welcome.
 
Hi

In PHP interpolation is supported only for variables, either simple, array value, object property, object method or reference. But not for constants and functions.
Try it with a simpler example, only [tt]echo[/tt] the result of string interpolation.

If you want to use the constant, you will have to do it with concatenation :
Code:
[navy]$headers[/navy] [teal]=[/teal] [i][green]"From: Joe Bloggs <"[/green][/i] [teal].[/teal] INFO [teal].[/teal] [i][green]">"[/green][/i][teal];[/teal]
Well, actually there are other ways too, like [tt]sprintf()[/tt], but does not really worth the effort :
Code:
[navy]$headers[/navy] [teal]=[/teal] [COLOR=orange]sprintf[/color][teal]([/teal][i][green]"From: Joe Bloggs <%s>"[/green][/i][teal],[/teal] INFO[teal]);[/teal]


Feherke.
feherke.github.io
 
Hello Feherke,
Thank you; learned something new again.
Peter Da
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top