PHP联系表单不发送电子邮件

时间:2016-08-26 17:44:51

标签: php html email phpmailer

我为我的网站下载了一个模板 该模板附带一个应该有效的联系表格。

我的MailHandler.php文件(法语):

<?php
    $owner_email='david.lobstein@gmail.com';
    //SMTP server settings
    $host = '';
    $port = '465';//"587";
    $username = '';
    $password = '';

    $subject='Vous avez reçu un message depuis le formulaire de contact de votre site ';
    $user_email='';
    $message_body='';
    $message_type='html';

    $max_file_size=50;//MB
    $file_types='/(doc|docx|txt|pdf|zip|rar)$/';
    $error_text='Quelque chose cest mal passe';
    $error_text_filesize='File size must be less than';
    $error_text_filetype='Failed to upload file. This file type is not allowed. Accepted files types: doc, docx, txt, pdf, zip, rar.';

    $private_recaptcha_key='6LeZwukSAAAAACmqrbLmdpvdhC68NLB1c9EA5vzU'; //localhost


    $use_recaptcha=isset( $_POST["recaptcha_challenge_field"]) and isset($_POST["recaptcha_response_field"]);
    $use_smtp=($host=='' or $username=='' or $password=='');
    $max_file_size*=1048576;

    if($owner_email=='' || $owner_email=='#'){
        die('Attention, recipient e-mail is not set! Please define "owner_email" variable in the MailHanlder.php file.');
    }

    if(preg_match('/^(127\.|192\.168\.)/',$_SERVER['REMOTE_ADDR'])){
        die('Attention, contact form will not work locally! Please upload your template to a live hosting server.');
    }

    if($use_recaptcha){
        require_once('recaptchalib.php');
        $resp = recaptcha_check_answer ($private_recaptcha_key,$_SERVER["REMOTE_ADDR"],$_POST["recaptcha_challenge_field"],$_POST["recaptcha_response_field"]);
        if (!$resp->is_valid){
            die ('wrong captcha');
        }
    }

    if(isset($_POST['name']) and $_POST['name'] != ''){$message_body .= '<p>Visitor: ' . $_POST['name'] . '</p>' . "\n" . '<br>' . "\n"; $subject.=$_POST['name'];}
    if(isset($_POST['email']) and $_POST['email'] != ''){$message_body .= '<p>Email Address: ' . $_POST['email'] . '</p>' . "\n" . '<br>' . "\n"; $user_email=$_POST['email'];}
    if(isset($_POST['state']) and $_POST['state'] != ''){$message_body .= '<p>State: ' . $_POST['state'] . '</p>' . "\n" . '<br>' . "\n";}
    if(isset($_POST['phone']) and $_POST['phone'] != ''){$message_body .= '<p>Phone Number: ' . $_POST['phone'] . '</p>' . "\n" . '<br>' . "\n";}
    if(isset($_POST['fax']) and $_POST['fax'] != ''){$message_body .= '<p>Fax Number: ' . $_POST['fax'] . '</p>' . "\n" . '<br>' . "\n";}
    if(isset($_POST['message']) and $_POST['message'] != ''){$message_body .= '<p>Message: ' . $_POST['message'] . '</p>' . "\n";}
    if(isset($_POST['stripHTML']) and $_POST['stripHTML']=='true'){$message_body = strip_tags($message_body);$message_type='text';}

try{
    include "libmail.php";
    $m= new Mail("utf-8");
    $m->From($user_email);
    $m->To($owner_email);
    $m->Subject($subject);
    $m->Body($message_body,$message_type);
    //$m->log_on(true);

    if(isset($_FILES['attachment'])){
        if($_FILES['attachment']['size']>$max_file_size){
            $error_text=$error_text_filesize . ' ' . $max_file_size . 'bytes';
            die($error_text);
        }else{
            if(preg_match($file_types,$_FILES['attachment']['name'])){
                $m->Attach($_FILES['attachment']['tmp_name'],$_FILES['attachment']['name'],'','attachment');
            }else{
                $error_text=$error_text_filetype;
                die($error_text);
            }
        }
    }
    if(!$use_smtp){
        $m->smtp_on( $host, $username, $password, $port);
    }

    if($m->Send()){
        die('success');
    }

}catch(Exception $mail){
    die($mail);
}
?>

当我从联系表单发送邮件时,它会显示我应该已经发送了邮件,因为弹出了#34;成功!&#34;。但我从来没有收到任何邮件。

3 个答案:

答案 0 :(得分:1)

实际上您应该可以使用Gmail。这真的很慢。使用

//SMTP server settings
$host = 'tls://smtp.gmail.com';
$port = '587';
$username = 'david.lobstein@gmail.com';
$password = 'your_password';

确保您的邮件库使用smtp。其他人使用phpmailer作为其他人建议使用上述设置。

答案 1 :(得分:0)

您无法将您的Gmail电子邮件用作发件人电子邮件$owner_email='david.lobstein@gmail.com';

  

您的 发件人电子邮件 收件人电子邮件 应该是您在其上配置的电子邮件网站的邮件服务器或 cPanel

出于安全原因,大多数托管服务提供商都不允许这样做。

将其更改为在您网站的邮件服务器上配置的电子邮件地址,它应该可以正常工作。

答案 2 :(得分:0)

如果您想发送电子邮件,请下载PHPMailer并编写此代码

//in localhost
 require("phpmailer/AutoloadPHPMailer");    

$mail=new PHPMailer(true);
$mail->IsSMTP();
if(isset($_POST["btn"]))
{
    try
    {
        $mail->Host='__host addres__';
        $mail->SMTPAuth=true;
        $mail->SMTPSecure="ssl";
        $mail->Port=465;
        $mail->Username="__User Name__";
        $mail->Password="__Password__";
        $mail->AddAddress($_POST["to"]);
        $mail->SetFrom("__From__","__Name__");
        $mail->Subject=$_POST["subject"];
        $mail->CharSet="UTF-8";
        $mail->ContentType="text/htm";
        $mail->MsgHTML($_POST["content"]);
        $mail->Send();
        echo '<font color="#00CC00" size="2" face="tahoma">email send</font>';
    }
    catch(phpmailerException $e)
    {
        echo $e->errorMessage();
    }
    catch(Exception $e)
    {
        echo $e->getMessage();
    }
}

这是Html代码

<form id="form" action="" method="post" style="border:2px solid #03F;width:350px;margin-left:400px;">
<table width="400" >
  <tr>
    <td style="text-align: center">To:</td>
    <td><input type="text" name="to" id="to" style="border:2px solid #060"></td>
  </tr>
  <tr>
    <td style="text-align: center">Subject:</td>
    <td><input type="text" name="subject" id="subject" style="border:2px solid #060"></td>
  </tr>
  <tr>
    <td style="text-align: center">Content</td>
    <td><textarea name="content" cols="30" rows="5" id="content" style="border:2px solid #060"></textarea></td>
  </tr>
  <tr>
    <td colspan="2"><input type="submit" name="btn" id="btn" value="SEND"></td>
  </tr>
</table>
</form>