0

我正在关注本指南: http ://www.w3schools.com/php/php_mail.asp

因此,按照本指南,我真的可以通过在 Chrome 或其他互联网浏览器中打开这个我称为“email.php”的页面来向自己发送电子邮件(someemail@website.com)吗?

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<h1> Email Application </h1>

<?php
$to = "someemail@website.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "Some Guy";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>

</body>
</html>

我尝试在本地运行上面看到的 email.php,但没有任何反应。我需要将 email.php 放在服务器上吗?

我目前正在处理联系表,我希望将在联系表字段中输入的所有信息发送到指定的电子邮件地址。我正在为一家公司的网站做这个。

4

2 回答 2

4

Yes, PHP can send e-mails. But you need to configure a mail (SMTP) server in php.ini.

If you have a mail server running locally, php.ini is set to use it by default. Don't forget to turn on the mail server and configure it to allow relaying of local mails.

I you have a remote mail server (you can even use Google's SMTP server), set php.ini to use it.

You might want to change

mail($to,$subject,$message,$headers);
echo "Mail Sent.";

into

if (mail($to,$subject,$message,$headers))
    echo "Mail Sent.";
else
    echo "Failed sending e-mail";
于 2013-06-05T18:23:33.663 回答
0

这是由于您的 smtp 设置而不是您的代码造成的。您应该验证您的 smtp 是否已正确设置。

尝试使用

echo ini_get("SMTP");
echo ini_get("smtp_port");

获取您的 SMTP 详细信息。检查你的 php.ini

应该是这种格式

[mail function]
; For Win32 only.
SMTP = localhost
smtp_port = 25



     <?php
        $to = "something@email.com";
        $subject = "Test mail";
        $message = "Hello! This is a simple email message.";
        $headers  = "MIME-Version: 1.0\r\n";
        $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
        $headers .= "From: XYZ <xyz.com>\r\n";
        if(mail($to,$subject,$message,$headers))
        {
           echo "Mail Sent.";
        }
       else
        {
          echo "Mail Failed ";
         }        
?>
于 2013-06-05T18:38:31.820 回答