在由PHP代码执行的电子邮件上添加链接

时间:2019-01-25 20:46:10

标签: php html email

我有此表单用于邮寄代码,用户将在其中接收和提交已提交表单中的电子邮件。

这是整个代码:

<?php

error_reporting(E_ALL); ini_set('display_errors', 1);

require_once ('database.php');

if (isset($_POST['send'])) {

$reqname = $_POST['reqname'];
$day = $_POST['day'];
$month = $_POST['month'];
$year = $_POST['year'];
$empname = $_POST['empname'];
$position = ($_POST['position']);
$account = $_POST['account'];
$platform = $_POST['platform'];
$processor = $_POST['processor'];
$ram = $_POST['ram'];
$monitor = $_POST['monitor'];
$phone = $_POST['phone'];
$phonetype = $_POST['phonetype'];
$headset = $_POST['headset'];

{
$database->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$insert_query = "INSERT INTO request (reqname, day, month, year, empname, position, account, platform, processor, ram, monitor, phone, phonetype, headset)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

$insert = $database->prepare($insert_query);
$insert->execute(array($reqname, $day, $month, $year, $empname, $position, $account, $platform, $processor, $ram, $monitor, $phone, $phonetype, $headset));

$email_from = "PC Request";//<== update the email address
$email_subject = "PC Request for $account";
$email_body = "Here are the specifications:\n\n".
		"Requested by: $reqname\n".
		"Start Date: $month/$day/$year\n".
		"Employee Name: $empname\n".
		"Position: $position\n".
		"Account: $account\n".
		"Platform: $platform\n".
		"Processor: $processor\n".
		"RAM: $ram\n".
		"Monitor: $monitor\n".
		"Phone: $phone\n".
		"Phone Type: $phonetype\n".
		"Headset: $headset\n".
        "Link: $link\n".
    
$to = "email@email.com";//<== update the email address
$headers = "From: $email_from \r\n";
$link = "www.google.com" \r\n";
mail($to,$email_subject,$email_body, $headers);
//done. redirect to thank-you page.
//header('Location: index.php');

echo "<script>alert('Successfully sent!'); window.location='index.php'</script>";
}
}
?>

那么是否可以在电子邮件正文上添加链接?

如您所见,我尝试创建一个名为link的变量,然后将其声明到电子邮件正文,但是显然它没有用。

新编辑的代码:

<?php

error_reporting(E_ALL); ini_set('display_errors', 1);

require_once ('database.php');

if (isset($_POST['send'])) {

$reqname = $_POST['reqname'];
$day = $_POST['day'];
$month = $_POST['month'];
$year = $_POST['year'];
$empname = $_POST['empname'];
$position = ($_POST['position']);
$account = $_POST['account'];
$platform = $_POST['platform'];
$processor = $_POST['processor'];
$ram = $_POST['ram'];
$monitor = $_POST['monitor'];
$phone = $_POST['phone'];
$phonetype = $_POST['phonetype'];
$headset = $_POST['headset'];

{
$database->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$insert_query = "INSERT INTO request (reqname, day, month, year, empname, position, account, platform, processor, ram, monitor, phone, phonetype, headset)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

$insert = $database->prepare($insert_query);
$insert->execute(array($reqname, $day, $month, $year, $empname, $position, $account, $platform, $processor, $ram, $monitor, $phone, $phonetype, $headset));

$email_from = "PC Request";//<== update the email address
$email_subject = "PC Request for $account";
$email_body = "Here are the specifications:\n\n".
		"Requested by: $reqname\n".
		"Start Date: $month/$day/$year\n".
		"Employee Name: $empname\n".
		"Position: $position\n".
		"Account: $account\n".
		"Platform: $platform\n".
		"Processor: $processor\n".
		"RAM: $ram\n".
		"Monitor: $monitor\n".
		"Phone: $phone\n".
		"Phone Type: $phonetype\n".
		"Headset: $headset\n".
		<a href='https://google.com'>Google</a>
    
$to = "email@email.com";//<== update the email address
$headers = "From: $email_from \r\n";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
mail($to,$email_subject,$email_body, $headers);
//done. redirect to thank-you page.
//header('Location: index.php');

echo "<script>alert('Successfully sent!'); window.location='index.php'</script>";
}
}
?>

1 个答案:

答案 0 :(得分:0)

只需使用HTML创建链接:

<a href='https://google.com'>Google</a>

并将其放在您的邮件正文中

并确保在标题中输入正确的内容类型:

$headers .= "Content-Type: text/html;";