如何在发送电子邮件后显示成功消息?

时间:2014-09-02 06:06:04

标签: php wordpress html-email

我正在尝试创建一个在wordpress网站侧栏显示的表单。作为基本步骤,我创建了表单而没有验证。代码如下。

<?php

if($_POST["submit"]) {
    $recipient="myemail@gmail.com";
    $subject="New reservations request";
    $sender=$_POST["sendername"];
    $senderEmail=$_POST["senderEmail"];
    $message=$_POST["message"];
    $name_title=$_POST["name_title"];   

    $mailBody="Name: $name_title $sender\nEmail: $senderEmail\n\n$message";

    mail($recipient, $subject, $mailBody, "From: $sender <$senderEmail>");

    $thankYou="<p>Thank you! Your message has been sent.</p>";
}

?>

<form id="" name="" action="<?php echo get_permalink(); ?>" method="post">
    <div class="form-note">We will contact you back within 24 hours with the availability and the estimated cost.</div>
    <br/>

    <div class="section-heading"><h6>Your Details</h6></div>
    <div class="label-input-wrapper">
        <div class="form-label">Title</div>
        <div class="form-input">
            <select name="name_title" class="name-title-input">
            <option value="" selected="selected">Select Title</option>
            <option value="Mr">Mr</option>
            <option value="Mrs">Mrs</option>
            <option value="Miss">Miss</option>
            <option value="4">Ms</option>
            <option value="5">Dr</option>
            <option value="6">Professor</option>
            <option value="7">The Rt Revd Dr</option>
            <option value="8">The Most Revd</option>
            <option value="9">The Rt Revd</option>
            <option value="10">The Revd Canon</option>
            <option value="11">The Revd</option>
            <option value="12">The Rt Revd Professor</option>
            <option value="13">The Ven</option>
            <option value="14">The Most Revd Dr</option>
            <option value="16">Rabbi</option>
            <option value="17">Canon</option>
            <option value="18">Dame</option>
            <option value="19">Chief</option>
            <option value="20">Sister</option>
            <option value="21">Reverend</option>
            <option value="22">Major</option>
            <option value="23">Other</option>
            <option value="24">Cllr</option>
            <option value="25">Sir</option>
            <option value="26">Rt Hon Lord</option>
            <option value="27">Rt Hon</option>
            <option value="28">The Lord </option>
            <option value="29">Viscount</option>
            <option value="30">Viscountess</option>
            <option value="31">Baroness</option>
            <option value="32">Captain</option>
            <option value="33">Master</option>
            <option value="34">Very Revd</option>
            <option value="35">Lady</option>
            <option value="38">MP</option>
            <option value="39">King of Kings and Lord of Lords</option>
            <option value="40">Conquering Lion of the Tribe of Judah</option>
            <option value="41">Elect of God and Light of this World</option>
            <option value="42">His Own Divine Majesty</option>
            <option value="43">First Ancient King of Creation</option>
            <option value="44">King Alpha</option>
            <option value="45">Queen Omega</option>
            <option value="46">Beginning with Our End and First with Our Last</option>
            <option value="47">Protector of All Human Faith</option>
            <option value="48">Ruler of the Universe</option>
            <option value="49">Dude</option>
            <option value="50">Mx (gender-netural)</option>
            </select>
        </div>
    </div>  

    <div class="label-input-wrapper">
        <div class="form-label">Name</div><div class="form-input"><input type="text" name="sendername"/></div>
    </div>

    <div class="label-input-wrapper">
        <div class="form-label">E-Mail</div><div class="form-input"><input type="email" name="senderEmail" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$" required /></div>
    </div>  

    <div class="label-input-wrapper special">   
        <div class="form-label">Write us in details if you have any special requirement</div><div class="form-input">​<textarea name="message"></textarea></div>
    </div>  

    <input type="submit" value="Submit" name="submit">
</form>

好吧,它正在使用这些输入值发送到我的电子邮件。 现在我想在提交表单后显示成功消息,表单应该消失。我通过谷歌搜索尝试了几个代码示例,但我无法将其实现到我的代码中。 我怎么能这样做?

[在第一次回复后编辑] 感谢那。但它说语法错误。

<?PHP
if($_POST["submit"]) {
    $recipient="am.riparz@gmail.com";
    $subject="Arugambay Bookings : New reservations request";
    $sender=$_POST["sendername"];
    $senderEmail=$_POST["senderEmail"];
    $message=$_POST["message"]; 

    $mailBody="Name: $sender\nEmail: $senderEmail\n\n$message";

$mail_sent = mail($recipient, $subject, $mailBody, "From: $sender <$senderEmail>");

if ($mail_sent) {
?>

  <p>Mail sent</p>

<?php 
} else {
?>


<form id="" name="" action="<?php echo get_permalink(); ?>" method="post">
    <div class="form-note">We will contact you back within 24 hours with the availability and the estimated cost.</div>
    <br/>
    <div class="section-heading"><h6>Your Details</h6></div>

    <div class="label-input-wrapper">
        <div class="form-label">Name</div><div class="form-input"><input type="text" name="sendername"/></div>
    </div>

    <div class="label-input-wrapper">
        <div class="form-label">E-Mail</div><div class="form-input"><input type="email" name="senderEmail" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$" required /></div>
    </div>  

    <div class="label-input-wrapper special">   
        <div class="form-label">Write us in details if you have any special requirement</div><div class="form-input">​<textarea name="message"></textarea></div>
    </div>  

    <input type="submit" value="Submit" name="submit">
</form>

<?php
}
?>

1 个答案:

答案 0 :(得分:3)

mail将在成功时返回true,否则返回false,只需检查此值以了解要显示哪个HTML部分

$mail_sent = mail(...);

if ($mail_sent) {
?>

  <p>Mail sent</p>

<?php 
} else {
?>

  <form>
  ...

<?php
}
?>