可以使用PHP命令编写和发送整个电子邮件吗?

时间:2013-06-05 18:05:48

标签: php email

我正在遵循本指南: 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放在服务器上吗?

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

2 个答案:

答案 0 :(得分:4)

是的,PHP可以发送电子邮件。但是您需要在php.ini中配置邮件(SMTP)服务器。

如果您有一个本地运行的邮件服务器,php.ini默认设置为使用它。不要忘记打开邮件服务器并将其配置为允许中继本地邮件。

我有一个远程邮件服务器(你甚至可以使用Google's SMTP server),设置php.ini来使用它。

您可能想要更改

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

进入

if (mail($to,$subject,$message,$headers))
    echo "Mail Sent.";
else
    echo "Failed sending e-mail";

答案 1 :(得分: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 ";
         }        
?>
相关问题