Reputation: 51
I am trying to assign an html link as the value of a php variable. Like so, (which I know doesn't work):
$mlink = <a href = "download.php">Download Link</a>;
I am trying to send an email with a link as the message of the email. I am using mail() to do so. Here is my code for the script which sends the mail. This is where I want to use the $mlink variable that has the html link as its value.
<?php
$to = $_POST['email1'];
$subject = "Test mail";
$message = $mlink;
$from = "[email protected]";
$headers = "From:" . $from;
if( mail($to, $subject, $message, $headers) )
{
echo ("<p>Mail Sent!</p>");
}
else
{
echo ("<p>Mail could not be sent!</p>");
}
?>
I am assigning the value of $mlink within another script that calls on this one. I can post that script as well. I just wasn't sure if that was necessary.
I can't seem to figure out how to get this to work. I've tried to use echo but it gives me an error when I do so in a variable assignment statement. I've tried a few other things but they all either give me an error or unwanted output. I am at a loss as to how to make this work :(
Any help is greatly appreciated. Thanks in advance for any enlightenment!
Upvotes: 0
Views: 4068
Reputation: 74232
NOTE:
As noted from comments in another answer, am providing some added information that the OP can look into doing and to search the Internet (Google link example) on achieving this.
How to send a multipart email using HTML and plain text:
In order for a link to be downloadable from a Website (WWW
), this is an http
call, thefore a server variable needs to first be assigned.
<?php
$to = $_POST['email1'];
$subject = "Test mail";
$server = "http://www.example.com";
$mlink = "download.php";
$message = "
<html>
<head>
<title></title>
</head>
<body>
<a href=\"$server/$mlink\">Download Link</a>
</body>
</html>
";
$from = "[email protected]";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From:" . $from;
if( mail($to, $subject, $message, $headers) )
{
echo ("<p>Mail Sent!</p>");
}
else
{
echo ("<p>Mail could not be sent!</p>");
}
?>
Here is an example (not for Emailing purposes, but on Website only):
<?php
$mlink = "download.php";
echo "<a href='$mlink'>Download Link</a>";
echo "<br>";
echo "<a href='$mlink'>$mlink</a>"; // displays file name
?>
Upvotes: 0
Reputation: 8334
To send HTML mail, the Content-type header must be set Content-type:text/html . this way , your code will be interpreted as html code not as plain text:
<?php
$mlink = "download.php";
$to = $_POST['email1'];
$subject = "Test mail";
$message = "<a href=" . $mlink . ">Download Link</a>";
$from = "[email protected]";
// To send HTML mail, the Content-type header must be set
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= "From:" . $from;
if( mail($to, $subject, $message, $headers) )
{
echo ("<p>Mail Sent!</p>");
}
else
{
echo ("<p>Mail could not be sent!</p>");
}
?>
Upvotes: 1