php form - godaddy经济托管Linux

时间:2015-10-14 00:52:02

标签: php sendmail shared-hosting

我有这样的表格:

<form action="/sendemail.php" id="main-contact-form" method="post" name="contact-form" role="form">
<div class="form-group">
    <input class="form-control" id="name" name="name" placeholder="put your name" required="" type="text" />
</div>

<div class="form-group">
    <input class="form-control" id="email" name="email" placeholder="Email" required="" type="email" />
</div>

<div class="form-group">
    <input class="form-control" id="subject" name="subject" placeholder="Subject..." required="" type="text" />
</div>

<div class="form-group">
    <textarea class="form-control" name="message" placeholder="Mensaje" required="" rows="8"></textarea>
</div>

<input class="btn btn-primary" id="submit" name="submit" type="submit" value="Enviar" />

这是sendemail.php:

<?php
        $pos = $_POST;
        $name = $_POST['name'];
        $email = $_POST['email'];
        $message = $_POST['message'];
        $from = $_POST['email']; 
        $to = 'email@gmail.com '; 
        $subject = 'contact message ';

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




    mail ($to, $subject, $body, $from)



?>

电子邮件是空的,我尝试了很多东西,但总是以同样的方式,就像表格无法将数据从输入传递给php变量。我是php的新手,欢迎任何帮助。

感谢。

2 个答案:

答案 0 :(得分:0)

<强>更新
经过一些研究,我发现:

  

您需要请求godaddy技术支持才能启用“sendmail”。

PHP mail() not working on GoDaddy

----------

我已经测试了您的代码,正常运行。我收到了所有 $_POST内容的电子邮件,没有任何问题。

$pos = $_POST;
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['email']; 
$to = 'email@gmail.com '; 
$subject = 'contact message ';

注意:

1 - $pos = $_POST;array,它将显示为Array
2 - 通过mail()发送的电子邮件很可能会在垃圾邮件文件夹上结束(请阅读下面的评论)。

答案 1 :(得分:0)

mail()函数的使用是如此不安全,是一种向大量垃圾邮件发送的矢量攻击,不再使用了,因此最好使用PHP Mailer libray。

在您的托管中创建一个EMAIL帐户(例如:no-reply@domain.com),然后安装此库PHP Mailer,现在您可以执行此操作:

<?php
require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

//$mail->SMTPDebug = 3;                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'user@example.com';                 // SMTP username
$mail->Password = 'secret';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
$mail->addAddress('ellen@example.com');               // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');

$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}