简体   繁体   English

使用phpmailer通过联系表单发送邮件

[英]Send mail via a contact form using phpmailer

As I mentioned in title I want to Send mail With a contact form to gmail using phpmailer. 正如我在标题中提到的,我想使用phpmailer将带有联系表单的邮件发送到gmail。 I have written the code for my contact form and sendmail php file is given below. 我已经为我的联系表编写了代码,下面给出了sendmail php文件。

I am got error on sendemail.php file in the line $mail-> Port=465; 我在$ mail-> Port = 465行中的sendemail.php文件上遇到错误;

contact.html contact.html

<script type="text/javascript" src="jquery-2.1.4.min.js"></script>
<script type="text/javascript">
$(function(){

$('input[type=submit]').click(function(){

$.ajax({
    url: "sendemail.php",
    type: 'POST',
    data: $("#myForm").serialize(),
    success: function(data){
       $('#result').html(data);
    }
});

});

});
</script>

    <form action="" method="post" onsubmit="return false;" id ="myForm">
          <input type="text" class="form-control" required="required" name="guest_name" placeholder="Name">
          <input type="text" class="form-control" required="required" name="guest_mail" placeholder="Email address">
          <input type="text" class="form-control" required="required" name="guest_cont" placeholder="Contact No.">
          <input type="text" class="form-control" required="required" name="guest_comp" placeholder="Company Name">
          <textarea name="guest_msg" id="message" required="required" class="form-control" rows="8" placeholder="Enquery or Feedback"></textarea>
          <input type="submit" name="submit" value="Submit" class="btn btn-danger btn-lg">
     </form>

And sendemail.php 还有sendemail.php

<?php


    $subject = "Query/Feedback"; 
    $name = $_POST['guest_name']; 
    $email = $_POST['guest_mail']; 
    $contact = $_POST['guest_cont']; 
    $company = $_POST['guest_comp'];
    $message = $_POST['guest_msg']; 

    $email_from = $email;
    $email_to = 'rexxxxxxxxxxxxd@gmail.com';

    if($sender== '' || $mail_id== '' || $cont_no== '' || $company== '' || $msg_txt== ''){
        echo "check the fields";
    }else{

    $subject='Query from '.$sender;
    $message='Dear Sir,<br><br>'.$msg_txt.'<br><br>From: '.$sender.'<br>Contact: '.$cont_no.'<br>Company Name: '.$company;

    require "phpmailer/class.phpmailer.php";  //including the phpmailer file

    //Installing Class
    $mail = new PHPMailer();

    // Set up SMTP
    $mail->IsSMTP();
    $mail->SMTPAuth = true;
    $mail->SMTPSecure = "ssl";
    $mail->Host = "smtp.gmail.com;"
    $mail->Port = 465;
    $mail->Encoding = '7bit';

    // Authentication
    $mail->Username = "mymailid@gmail.com"
    $mail->Password = "mypassword"

    // Compose
    $mail->SetFrom($_POST['guest_mail'], $_POST['guest_name']);
    $mail->AddReplyTo($_POST['guest_mail'], $_POST['guest_name']);
    $mail->Subject = "/new Contact From Enquery"
    $mail->MsgHTML($guest_msg);

    // Send To
    $mail->AddAddress($email_to, "Banti");
    $result = $mail->Send();
    if($result)
        {
        echo "Thank you for your Feedback";
    }

    }


?>

I can't comment yet due to my low rep, so maybe i have misunderstood the question.. but, if with "i got an error setting the Port" you mean that your PHP script runs but the email is not sent, the problem is probably that gmail needs: 由于回复率低,我无法发表评论,所以也许我误解了这个问题..但是,如果出现“我在设置端口时出错”,则表示您的PHP脚本正在运行,但电子邮件未发送,则问题gmail可能需要:

$mail->SMTPSecure = "tls";
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;

I use PHPMailer with gmail as well. 我也将PHPMailer与gmail一起使用。

Edit 编辑

You are missing some ";" 您缺少一些“;” :

$mail->Host = "smtp.gmail.com;" // you have this
$mail->Host = "smtp.gmail.com"; // you need this
$mail->Username = "mymailid@gmail.com"; // add this
$mail->Password = "mypassword"; // add this

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM