0

我有一个 PHP 表单。该表单有效,并且可以通过它发送电子邮件。它看起来不像是从特定的电子邮件地址将它们发送到我希望电子邮件发送到的电子邮件地址 (xxx@a.com)。

我希望从yyy@a.com我在下面配置的 发送这些电子邮件。这是PHP:

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'Contact'; 
$to = 'me@a.com'; 
$subject = 'Contact';

$body = "From: $name\n E-Mail: $email\n Message:\n $message";

if ($_POST['submit']) {              
    if (mail ($to, $subject, $body, $from)) {
        echo '<script type="text/javascript">
                alert("Your message has been sent!");
            </script>';
    } else { 
        echo '<script type="text/javascript">
                alert("Something went wrong, try again.");
            </script>'; 
        }
    }
?>

我尝试将 $from 更改为 yyy@a.com,但这不会更改电子邮件的发件人地址。为什么没有设置发件人地址?

4

1 回答 1

1

第四个参数不是from,是extra headers。因此,要包含一个额外的from标题,请执行以下操作:

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'Contact <yyy@a.com>'; // You can combine name and address
$to = 'xxx@a.com'; 
$subject = 'Contact';


$body = "From: $name\n E-Mail: $email\n Message:\n $message";

$extraHeaders = 'From:'.$from; // Header field + header field value.

if ($_POST['submit']) {              
    if (mail ($to, $subject, $body, $extraHeaders)) { // Pass the extra headers...
        echo '<script type="text/javascript">
                alert("Your message has been sent!");
            </script>';
    } else { 
        echo '<script type="text/javascript">
                alert("Something went wrong, try again.");
            </script>'; 
    }
}
于 2013-08-11T00:19:11.737 回答