0

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 = "somewhere@somewhere.com";
$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!

4

2 回答 2

1

要发送 HTML 邮件,必须将 Content-type 标头设置为Content-type:text/html。这样,您的代码将被解释为 html 代码而不是纯文本:

 <?php

 $mlink = "download.php";

 $to = $_POST['email1'];
 $subject = "Test mail";
 $message = "<a href=" . $mlink . ">Download Link</a>";
 $from = "somewhere@somewhere.com";

 // 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>");
 } 
 ?>
于 2013-08-23T14:49:49.927 回答
0

注意

正如另一个答案中的评论所指出的,我提供了一些附加信息,OP 可以考虑这样做并在互联网上搜索(谷歌链接示例)以实现这一目标。

如何使用 HTML 和纯文本发送多部分电子邮件:

  • 诀窍在于标题信息。您将需要创建两份电子邮件副本,一份是纯文本,另一份是 HTML 格式。

编辑(仅 HTML 格式的工作示例)

为了从网站 ( WWW) 下载链接,这是一个http调用,因此需要首先分配一个服务器变量。

<?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 = "somewhere@somewhere.com";

$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>");
}
?>

原始答案

这是一个示例(不用于发送电子邮件,仅用于网站):

<?php

$mlink = "download.php";

echo "<a href='$mlink'>Download Link</a>";

echo "<br>";

echo "<a href='$mlink'>$mlink</a>"; // displays file name

?>
于 2013-08-23T14:36:59.777 回答