通过php联系表单发送和接收邮件

时间:2014-10-20 12:02:57

标签: php forms email contact

最近,我购买了一个域名和托管。我还创建了一个包含联系表格的网页。我想当用户在联系表单的textarea中写一些内容并点击提交然后该消息被发送到我的Gmail帐户,以便我也可以回复这些消息。我也使用过这个脚本,但是它没有用。

这是 sendemail.php 文件

<?php
    header('Content-type: application/json');
    $status = array(
        'type'=>'success',
        'message'=>'Thank you for contact us. As early as possible  we will contact you '
    );

    $name = @trim(stripslashes($_POST['name'])); 
    $email = @trim(stripslashes($_POST['email'])); 
    $subject = @trim(stripslashes($_POST['subject'])); 
    $message = @trim(stripslashes($_POST['message'])); 

    $email_from = $email;
    $email_to = 'raunakhajela@gmail.com';//replace with your email

    $body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;

    $success = @mail($email_to, $subject, $body, 'From: <'.$email_from.'>');

    echo json_encode($status);
    die;

这是 index.php

中的联系表单代码
<div class="contact" id="contact">
            <div class="container-3x">

                <div class="block-header"> <!-- Blocks Header -->
                    <h2 class="title">Contact Us</h2>
                </div>

                <div class="block-content"> <!-- Blocks Content -->

                    <form action="sendemailphp" method="post" class="trigger-form-animate">
                        <input type="text" value="" id="name" name="name" placeholder="Name *" />
                        <input type="text" value="" id="email" name="email" placeholder="Email *" />
                        <input type="text" value="" id="website" name="website" placeholder="Website *" />
                        <textarea type="text" value="" id="message" name="message" rows="3" placeholder="Your Message *" ></textarea>
                        <div class="clr"></div>
                        <button>Submit Message</button>
                    </form>

                </div>

            </div>

        </div>  

我也试过“ http://www.mybloggertricks.com/2012/06/connect-your-website-email-address-to.html ”这个教程,但它不起作用。在添加其他帐户窗口时,无法在gmail中找到“通过Gmail发送(更容易设置)”此选项。

我该怎么做? Plzz hlp

2 个答案:

答案 0 :(得分:3)

您没有名为subject的表单元素,可以解释邮件失败的原因,并且很可能会被发送到垃圾邮件。

例如:<input type="text" name="subject">。添加一个替换。

但是,您需要确保使用条件语句填写此内容 (请参阅下面的示例)。

$subject = @trim(stripslashes($_POST['subject'])); 

$subject = "Form submission";

您也可以将其添加到表单中:

<input type="hidden" name="subject" value="Form submission">

我或上面列出的内容都可以使用。

许多电子邮件客户端将向垃圾邮件发送邮件或完全拒绝邮件,如果&#34;主题&#34;缺少,但代码中明显遗漏了这些内容。

检查主题是否已填写示例:

if(isset($_POST['subject']) && !empty($_POST['subject'])){

// do someting

}

答案 1 :(得分:0)

试试这个,两个都可行

 <?php
  $to = "usermailid@gmail.com";
    $subject = "Welcome to";
    $message = " Hi $username,<br /><br />
    Thank you for signing up with us.<br />

    Thanks <br />";
    // Always set content-type when sending HTML email
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
    // More headers
    $headers .= 'From: <yourmailid@gmail.com>' . "\r\n";
    $mail=mail($to,$subject,$message,$headers);
    if($mail)   
    {
    $to = "admin@gmail.com";

    $subject = "Following Customer Signed Up";
    $message = " $username,Customer is signed up with us,<br /><br />
    Customer Details:<br />First Name:$firstname<br/>Last Name:$lastname<br/>Email:$email<br/>
    Phone:$phone<br/>Zip Code:$zip<br/>Message:$message_cust<br/><br/><br/>
    Thanks <br />";
    // Always set content-type when sending HTML email
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
    // More headers
    $headers .= 'From: <yourmailid@gmail.com>' . "\r\n";
    $mail=mail($to,$subject,$message,$headers);
    }
     ?>