dongsuo9982 2014-10-20 12:02
浏览 31
已采纳

通过php联系表单发送和接收邮件[重复]

This question already has an answer here:

Recently, I have purchased a domain and hosting. I've also created a webpage having contact form in it. I want when the user wrote something in textarea of contact form and clicks submit then that message is sended to my gmail account and so that i can reply also to those messages. I've also used this script to do so but its not working.

This is sendemail.php file

<?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 . "

" . 'Email: ' . $email . "

" . 'Subject: ' . $subject . "

" . 'Message: ' . $message;

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

    echo json_encode($status);
    die;

This is my contact form code in 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>  

I've also tried "http://www.mybloggertricks.com/2012/06/connect-your-website-email-address-to.html" this tutorial but its not working. Unable to find "Send through Gmail (easier to set up)" this option in gmail on adding another accounts windoww.

How can i do that?? Plzz hlp

</div>
  • 写回答

2条回答 默认 最新

  • doudan1123 2014-10-20 12:33
    关注

    You don't have a named subject form element which may explain why mail is failing and may very well be sent to Spam instead.

    For example: <input type="text" name="subject">. Either add one of replace.

    You will however need to make sure that this is filled out using a conditional statement
    (see further below for an example).

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

    with

    $subject = "Form submission";
    

    You could also add this instead to your form:

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

    Either this or what I've outlined just above will work.

    Many Email clients will either send mail to Spam or reject it altogether if a "subject" is missing, which is clearly missing from your code.

    Checking if subject has been filled example:

    if(isset($_POST['subject']) && !empty($_POST['subject'])){
    
    // do someting
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单