0

我收到了一个成功向我发送电子邮件的联系表。

这是摘录:

$_POST['message'] = wordwrap($_POST['message'], 70);
mail ('myemail@test.com', $_POST['subject'], $_POST['message'] , $_POST['email']);
echo "<div class='registertext'>Your email was succesfully sent to a member of the administration team. Please wait 24 hours for as to reply and ensure you check your junk mail!<br />To login please click <a href='login.php'>here</a></div>";

我遇到的问题是,电子邮件是从我的主机发送的。不是我想指定的电子邮件。我将如何克服这一点?

4

2 回答 2

0

您可以使用将使用 SMTP 帐户的 PEAR 邮件。这是我使用的邮件表单中的一些代码

        $from = "Name <webmaster@domain.com>";
        $to = "Name <address@domain.com>";
        $subject = "Subject";
        $body = 'A message!';

        $host = "ssl://domain.com";
        $port = "465";
        $username = "username";
        $password = "password";

        $headers = array ('From' => $from,
                          'To' => $to,
                          'Subject' => $subject);
        $smtp = Mail::factory('smtp',
                              array ('host' => $host,
                                     'port' => $port,
                                     'auth' => true,
                                     'username' => $username,
                                     'password' => $password));

        $mail = $smtp->send($to, $headers, $body);

        if (PEAR::isError($mail)) {
            echo("<p>" . $mail->getMessage() . "</p>");
        } else {
            echo("<p>Message successfully sent!</p>");
        }
于 2012-12-06T21:16:36.753 回答
0

您可以在电子邮件标题中指定它:

$recipient = "recipient@test.com";
    $from      = "You@yoursite.com";
    $replyTo   = "You@yoursite.com";
    $subject   = "Hi!";
    $text      = "<p>This is a test!<p>";

    $headers = "MIME-Version: 1.0\r\n"
      ."Content-Type: text/html; charset=utf-8\r\n"
      ."Content-Transfer-Encoding: 8bit\r\n"
      ."From: =?UTF-8?B?". base64_encode([Your Name]) ."?= <$from>\r\n"
      ."Reply-To: $replyTo\r\n"
      ."X-Mailer: PHP/". phpversion();

    //send it!
    if (mail($recipients, $subject, $text, $headers, "-f $from")){
        echo "sent";
        } else {
           echo "did not send";
        };

但很有可能它会被垃圾邮件过滤器捕获。在这种情况下,您最好的选择是使用一个 PHP 邮件库来处理 SMTP 电子邮件并使用您的实际帐户发送邮件(有几个包可以为您处理这个问题:Pear MailPHP Mailer等。

于 2012-12-06T21:14:25.043 回答