简体   繁体   English

如何使用PHP发送电子邮件?

[英]How to send an E-Mail with PHP?

I'm not really familiar with PHP and I tried to implement a simple mail-script onto my page. 我对PHP不太熟悉,因此尝试在页面上实现一个简单的邮件脚本。 But nothing seems to happen. 但是似乎什么也没发生。 The Server I'm on definitely supports PHP. 我所在的服务器绝对支持PHP。 Do I have to do anything else than just upload my files? 除了上传文件,我还需要做其他事情吗?

So I have my contact.php and in it there is just normal HTML-Code with a < ?php ?> Tag in the < head> section. 因此,我有了我的contact.php,并且其中只有<head>部分中带有<?php?>标记的普通HTML代码。

It goes like this: 它是这样的:

<?php
    // Ausführen wenn Formular gesendet
    if (isset($_POST["submit"]))
    {

        // Sammeln der Formulardaten
        $an = "MYMAILADRESS@outlook.com";
        $name = $_POST['name'];
        $email = $_POST['email'];
        $betreff = $_POST['betreff'];
        $nachricht = $_POST['nachricht'];

        // Mailheader UTF-8 fähig machen
        $mail_header = 'From:' . $email . "\n";
        $mail_header .= 'Content-type: text/plain; charset=UTF-8' . "\r\n";

        // Nachrichtenlayout erstellen
        $message = "
            Name:       $name\n
            Email:      $email\n
            Nachricht:  $nachricht\n
        ";

        // Verschicken der Mail
        mail($an, $betreff, $message, $mail_header );
    };
?>

Of Course I changed "MYMAILADRESS" intentionally. 当然,我有意更改了“ MYMAILADRESS”。 The HTML-form looks like this: HTML表单如下所示:

<form id="form" action="thank_you.html" method="post">
        <label for="name">Name:</label>
        <input id="name" name="name" type="text" /><br />
        <label for="email">E-Mail: *</label>
        <input id="email" name="email" type="text" /><br />
        <label for="betreff">Betreff:</label>
        <input id="betreff" name="betreff" type="text" /><br />
        <label for="nachricht">Nachricht: *</label>
        <textarea id="nachricht" name="nachricht"></textarea><br />
        <p class="fussnote">* kennzeichnet Pflichtfelder</p>
        <input id="submit" name="submit" type="submit" value="Absenden" />
    </form>

If anybody could help me with this, it'd be great! 如果有人可以帮助我,那就太好了! :) :)

The action of your form is pointing to a URL that is probably resolved by a .html file. action的形式指向一个可能是由一个解析的URL .html文件。 Most web servers that support PHP are not configured to run .html files through the PHP processor. 大多数支持PHP的Web服务器未配置为通过PHP处理器运行.html文件。 Use .php extension instead. 请改用.php扩展名。

The code you've supplied should work, but be warned that you are leaving yourself open to potential hacks. 您提供的代码应该可以使用,但是要注意,您可能会遭受潜在的黑客攻击。

PHP's mail() function makes no effort to sanitise the input it receives; PHP的mail()函数毫不费力地清理了收到的输入。 it would be possible for your visitors to send a from address with line feeds, and add a kinds of additional mail headers (eg CC or BCC) in order to send spam from your site. 这将有可能为你的访问者发送from地址与换行符,并且为了从您的网站发送垃圾邮件添加各种附加的邮件标题(如CC或BCC)。

It's relatively easy to sanitise the input, but even then you're left with the mail() function not being very easy to use - you'll end up with lots of code for relatively little functionality. 清理输入是相对容易的,但是即使那样,您仍然会感到mail()函数的使用不是很容易-您最终将获得大量的代码,而功能却很少。

For this reason, I would always recommend using a third party mailer library. 因此,我总是建议您使用第三方邮件库。 There are several good ones available for PHP; PHP有几个不错的选择。 my preferred choice is phpMailer . 我的首选是phpMailer Download this PHP script, include it as part of your site, and follow the instructions on how to send mail using it -- you'll find it's a lot easier to use than the mail() function, and you don't need to worry about hacking attacks because it does all the input sanitisation work for you. 下载此PHP脚本,将其包含在您的网站中,并按照有关如何使用它发送邮件的说明进行操作-您会发现它比mail()函数更容易使用,并且您不需要担心黑客攻击,因为它可以为您完成所有输入清除工作。

Hope that helps. 希望能有所帮助。

This your problem, 这是你的问题

So I have my contact.php and in it there is just normal HTML-Code with a < ?php ?> Tag in the < head> section. 因此,我有了我的contact.php,并且其中只有<head>部分中带有<?php?>标记的普通HTML代码。

Move the PHP code (inc tags), into your HTML body tags. 将PHP代码(inc标签)移到HTML正文标签中。

Also, change your form action to your .php file. 另外,将表单操作更改为.php文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM