正如我在标题中所提到的,我想使用phpmailer将联系表单发送到gmail。我已经为我的联系表单编写了代码,sendmail php文件如下所示。
我在$ mail->行中的sendemail.php文件上出错了端口= 465;
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>
和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";
}
}
?>
答案 0 :(得分:2)
由于我的低代表我还不能发表评论,所以也许我误解了这个问题..但是,如果“我设置了错误设置端口”你的意思是你的PHP脚本运行但电子邮件没有被发送,问题可能是gmail需要:
$mail->SMTPSecure = "tls";
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
我也将PHPMailer与gmail一起使用。
修改强>
你错过了一些“;” :
$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