【问题标题】:Adding a link on an email that was executed by a PHP code [duplicate]在由 PHP 代码执行的电子邮件上添加链接 [重复]
【发布时间】:2019-06-19 17:11:00
【问题描述】:

我有这个表单来邮寄代码,用户将收到并从提交的表单发送电子邮件。

这是整个代码:

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

【问题讨论】:

  • 您在尝试使用它之后创建了变量。您还创建了一个语法错误,所以这根本不会执行,您会在 PHP 日志中看到一个错误...
  • \r\n 需要在引号内。

标签: php html email


【解决方案1】:

只需使用 HTML 来创建您的链接:

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

并将其放在邮件正文中

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

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

【讨论】:

  • 但该代码是邮件的正文。
  • 您需要将Content-type: text/html 放在标题中,并使邮件的其余部分有效HTML。
  • @Barmar 嗯,我该把Content-type: text/html放在哪里?
  • 你把它放在$headers
  • 尝试谷歌搜索如何从 php 发送 html 电子邮件。
猜你喜欢
  • 2020-05-10
  • 1970-01-01
  • 2013-03-20
  • 2011-09-03
  • 2016-04-04
  • 1970-01-01
  • 1970-01-01
  • 2013-08-22
  • 2017-05-26
相关资源
最近更新 更多