从PHP发送电子邮件的问题

时间:2016-06-21 06:03:10

标签: php html

我有这个表单,我正在尝试将联系我们表单的内容发送到此邮件scientology2016@gmail.com,但由于某种原因它没有发送任何邮件,我无法找出错误此

<form  method="post"  action="process.php">
<div class="col-md-6">
<div class="form-group">
<input type="text" class="form-control input-lg" name="name" placeholder="UserName">
</div>
<div class="form-group">
<input type="text" class="form-control input-lg" name="email" placeholder="E-Mail">
</div>
<div class="form-group">
<input type="text" class="form-control input-lg" name="usertel" placeholder="phone">
</div>
</div>
<div class="col-md-6">
<textarea class="form-control input-lg"  name="subject" placeholder="Your Message"></textarea>

<button type="button" class="btn btn-primary btn-lg btn-block">Contact Us </button>
</div>
</form>

这是用于发送我写的邮件的PHP代码,但它不起作用

<?php
$name = ($_GET['name']) ? $_GET['name'] : $_POST['name'];
$email = ($_GET['email']) ?$_GET['email'] : $_POST['email'];
$subject2 = ($_GET['subject']) ?$_GET['subject'] : $_POST['subject'];
$tel = ($_GET['usertel']) ?$_GET['usertel'] : $_POST['usertel'];
$comment = ($_GET['body']) ?$_GET['body'] : $_POST['body'];

if ($_POST) $post=1;
if (!$name) $errors[count($errors)] = 'Please enter your name.';
if (!$email) $errors[count($errors)] = 'Please enter your email.'; 
if (!$comment) $errors[count($errors)] = 'Please enter your comment.'; 
if (!$errors) {
    $to = 'scientology2016@gmail.com';
    $from = $name . ' <' . $email . '>';
    $subject = 'Message from ' . $name; 
    $message = '
    <!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></head>
    <body>
    <table>
        <tr><td>Name</td><td>' . $name . '</td></tr>
        <tr><td>Subject</td><td>' . $subject2 . '</td></tr>
        <tr><td>TelPhone</td><td>' . $tel . '</td></tr>
        <tr><td>Email</td><td>' . $email . '</td></tr>
        <tr><td>Message</td><td>' . nl2br($comment) . '</td></tr>
    </table>
    </body>
    </html>';

    $result = sendmail($to, $subject, $message, $from);
    if ($_POST) {
        if ($result) echo 'Thank you! We have received your message.';
        else echo 'Sorry, unexpected error. Please try again later';
    } else {
        echo $result;   
    }

} else {

    for ($i=0; $i<count($errors); $i++) echo $errors[$i] . '<br/>';
    echo '<a href="form.php">Back</a>';
    exit;
}
function sendmail($to, $subject, $message, $from) {
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
    $headers .= 'From: ' . $from . "\r\n";

    $result = mail($to,$subject,$message,$headers);
    if ($result) return 1;
    else return 0;
}
?>
在这种情况下,有谁可以帮助我。

1 个答案:

答案 0 :(得分:0)

如果您使用的是wamp服务器

首先

在php文件夹中打开php.ini文件并编写或更新

smtp_server=smtp.gmail.com

; smtp port (usually 25)

smtp_port=465

auth_username=scientology2016@gmail.com
auth_password=[MYPASSWORDHERE]