无法在我的网站中发送邮件

时间:2015-01-28 10:15:52

标签: php html phpmailer

我的网站上有联系表格。我希望通过该表格收到访问者的邮件。但奇怪的是我的代码不起作用。我的网站上有一些验证部分。页面获取刷新,但没有收到任何邮件。有人可以帮我解决这个问题。

我的HTML代码是

<form novalidate name="contacto" method="post" action="" class="form_contacto" id="formContacto">
    <fieldset>
                                                <label for="idEmpresa">
            <span>Company</span>
            <input type="text" name="comp" id="idEmpresa" placeholder="Company" value="">
                        </label>

        <label for="idContacto">
            <span>Name and surname/s</span>
            <input type="text" name="name" id="idContacto" placeholder="Name and surname/s" value="">
                        </label>

        <label for="idEmail">
            <span>E-mail</span>
            <input type="email" name="email" id="idEmail" placeholder="E-mail" value="">
                        </label>

        <label for="idTelefono">
            <span>Telephone number</span>
            <input type="tel" name="telephone" id="idTelefono" placeholder="Telephone number" value="">
                        </label>

        <label for="idConsulta">
            <span>Any remarks?</span>
            <textarea name="message" id="idConsulta" rows="3" placeholder="Any remarks?"></textarea>
                        </label>



    <input type="submit" class="send" name="form_contacto" value="send" >

        <div class="clearfix"></div>

        <input type="hidden" name="control" value="Este campo no debe rellenarse">
        <input type="hidden" name="id_content" value="12">
        <input type="hidden" name="h1" value="Contact">
        <input type="hidden" name="lang" value="en">
        <input type="hidden" name="redir" value="/en/contact/">
        <input type="hidden" name="plantilla" value="contacto">
        <input type="hidden" name="referer" value="">

    </fieldset>

</form> 

我的PHP代码是

    <?php
 $msg="";
if(isset($_POST['form_contacto']))
{
print_r($_POST['form_contacto']);

$comp=$_POST['name'];
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['telephone'];

$detail=$_POST['message'];

require("class.phpmailer.php"); // path to the PHPMailer class

$mail = new PHPMailer();  

$mail->IsSMTP();  // telling the class to use SMTP
$mail->Mailer = "smtp";
$mail->Host = "ssl://smtp.gmail.com";
$mail->Port = 465;
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "xxx@gmail.com"; // SMTP username
$mail->Password = "sunnight"; // SMTP password 

$mail->From     = "$email";
$mail->AddAddress("yyy@gmail.com");  

$mail->Subject  = "quote from $email";
$mail->Body     = "$detail";
$mail->WordWrap = 50;  


if(!$mail->Send()) {


echo 'Mailer error: ' . $mail->ErrorInfo;
}
$msg="<p style=\"color:#99CC00; font-size:13px;\">your registration was Successfull!.</p>";

}
?>  

3 个答案:

答案 0 :(得分:0)

您的代码中有一个小错误。

在表单标签中,名称为&#34; contacto&#34;并在提交标签中&#34; form_contacto&#34;。所以给他们一个相同的名字,我认为它会起作用。

干杯

答案 1 :(得分:0)

您必须将连接前缀从主机移至SMTPSecure

在你的情况下:

$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com";

请看一下这个例子:http://phpmailer.worxware.com/?pg=examplebgmail(不要混淆,他们使用的是TLS而不是SSL,但两者都在使用gmail)

答案 2 :(得分:0)

非常彻底地涵盖了此错误in the docs。您正在使用旧版本的PHPMailer,并将您的代码基于旧示例。我建议您重新开始使用最新版本,特别是this one

不要将From地址设置为提交表单的人的电子邮件地址 - 如果您使用的是gmail或yahoo(和其他人),则无法发送,并且可能会被其他地方拒绝任何已正确设置SPF的域。这样做:

$mail->setFrom('xxx@gmail.com'); //Use your own gmail address
$mail->addReplyTo($email);

您用于Host值的语法是有效的(尽管端口465上的ssl已被弃用了15年!)。您可以一次性设置HostSMTPSecurePort属性的值,如下所示:

$mail->Host = 'tls://smtp.gmail.com:587';
相关问题