通过PHP发送电子邮件

时间:2014-01-17 01:08:45

标签: php email email-integration

我正在尝试创建一个用户完成它并将其电子邮件放在论坛上的论坛,以便我知道它来自哪个人。完成后,我会验证并将论坛内容发送到我的电子邮件地址,但我没有收到任何来自该网站的电子邮件。

这是我的HTML

<article class="contact" id="contact">

    <div class="contact-content">

                        <div class="span5 contact-info">

            <!-- Contact title -->
            <div class="contact-title">
                                        <h2>Contact</h2>
                                </div> 



            <div class="contact-scroll">                


                                                <p>Address: 123 Baker Street</p>
                                                <p>Phone Number: (555) 555-555</p>
                                                <p>E-Mail: example@gmail.com</p>

                <div class="class1" style="position: absolute; display: none;">
                <div class="class2">
                <div class="class3" style="position: absolute; top: 0px;" oncontextmenu="return false;">
                <div class="class4" style="position:relative;"></div></div><div class="class5"></div></div></div>

                <!-- contact form -->
                <div class="contact-form" style="margin-top: 10px;">

                        <form action="" method="post">
                            <input style="width: 185px; float:left; font-size:14pt; color: white; b" type="text" name="your-name" id="your-name" value="" size="40" class="wpcf7-form-control-wrap your-name" aria-required="true" placeholder="Full Name">
                            <input style="width: 185px; float:left; font-size:14pt; color: white;" type="email" name="your-email" id="your-email" value="" size="40" class="wpcf7-form-control-wrap your-email" aria-required="true" placeholder="Email Address">
                            <input style="width: 374px; font-size:14pt; color: white;" type="text" name="your-subject" id="your-subject" value="" size="40" class="wpcf7-form-control-wrap your-subject" placeholder="Company Name">
                            <textarea style="color: white; width: 373px;" name="your-message" id="your-message" cols="40" rows="10" class="wpcf7-form-control-wrap your-message" aria-required="true" placeholder="Your Message"></textarea>

        <input type="submit" id="sub" name="sub" value="Submit">


                        </form> 

                </div>  


                </div>

            </div>
        </div>



</article>

这是我的php

if(isset($_POST['sub']))
{
    $name = $_POST["your-name"];
    $email = $_POST["your-email"];
    $companyName = $_POST["your-subject"];
    $message = $_POST["your-message"];

    $to = "sendto@gmail.com";
    $headers = "From: $email\r\n";
    $headers .= "Content-type: text/html\r\n";
    $subject = $name." from ".$companyName." just emailed from online CV";
    mail($to, $subject, $message, $headers);
    echo "Mail Sent.";
}

“if”语句正在正确执行,因为输出了echo。我还尝试将值传递给警告框,并使用此PHP代码在警报框中显示所有输入值。唯一的问题是我无法通过电子邮件将输入邮件发送到我在PHP代码中指定的电子邮件地址。我在000Webhost免费服务器上运行此站点。我必须在index.php中包含一个脚本才能使邮件功能正常工作吗?我是否必须在我的000Webhost帐户的控制面板内配置一些内容?或者这只是一个语法错误?

由于

1 个答案:

答案 0 :(得分:0)

好吧,我刚刚设法解决了这个问题。为了使这个工作在gmail上,我不得不在我的标题中添加额外的东西。

$headers = "From: {$email}\r\n"; 
$headers .= "Reply-To: {$email}\r\n"; 
$headers .= "Return-Path: {$email}\r\n"; 
$headers .= "X-Mailer: PHP5\r\n"; 
$headers .= "Content-Transfer-encoding: 8bit\r\n"; 
$headers .= "MIME-Version: 1.0\r\n"; 
$headers .= "X-MSMail-Priority: Normal\r\n"; 
$headers .= "Importance: 0\r\n"; 
$headers .= "Content-type: text/html\r\n";
$headers .= "Delivered-to: {$to}\r\n"; 

$subject = $name." from ".$companyName." just emailed you";
$headers .= "Content-Type: text/plain; charset=UTF-8\r\n\r\n"; 
if(mail($to, $subject, $message, $headers))
{
 echo "Mail Sent.";
}
else
{
echo "Mail was not sent!";
}

将邮件发送到Gmail帐户需要这些额外的标头。我原始代码中的其他所有内容都可以正常工作。