[英]Contact form not sending message to Gmail
請為此我需要幫助。 我的聯系表格沒有向我的gmail發送消息。 我也檢查了我的垃圾郵件,沒有任何反應。 不知道這是否與我的代碼有關。 提前致謝。 我已經意識到該信息正在進入我的高迪面板。 主題是
郵件失敗-收件人地址格式錯誤
消息正文說
您發送的郵件包含一個收件人地址,該地址的構造不正確:收件人:本地部分丟失或格式不正確(期望字詞或“ <”)該郵件尚未傳遞給任何收件人。
還請忽略$ip
字段,該字段未添加我的代碼的一部分。
<?php
$emailErr = "";
$commentErr = "";
if(isset($_POST['submit'])){
//declares variable
$email = $_POST['email'];
$comment = $_POST['comment'];
if(empty($_POST['email'])){
$emailErr = "Please enter your email";
}
if(empty($_POST['comment'])){
$commentErr = "comment field can't be empty";
}
}
if(!empty($_POST['email']) && !empty($_POST['comment'])){
// Send the email
$to = "myname@gmail.com";
$email = "From: $email";
$comment = "Message: $comment";
$message = "$message" . "\n\n\n==- Sent from the website with IP Address: " . $ip . " -==";
$headers = "From: $email,";
$send_contact = mail($to,$email,$comment,$message,$headers);
header("Location: index.php");
}
?>
這應該工作。
<?php
$emailErr = "";
$commentErr = "";
if(isset($_POST['submit'])){
//declares variable
$email = $_POST['email'];
$comment = $_POST['comment'];
if(empty($_POST['email'])){
$emailErr = "Please enter your email";
}
if(empty($_POST['comment'])){
$commentErr = "comment field can't be empty";
}
}
if(!empty($_POST['email']) && !empty($_POST['comment'])){
// Send the email
$to = "myname@gmail.com";
$title = "Message from my website";
$message = "Comment: {$comment}" . "\r\n";
$message .= "Sent from the website with IP Address: {$ip}" . "\r\n";
$headers = "From: " . strip_tags($email) . "\r\n";
$headers .= "Reply-To: ". strip_tags($email) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type:text/plain;charset=UTF-8" . "\r\n";
mail($to,$title,$message,$headers);
header("Location: index.php");
}
?>
我建議您在這里學習如何使用郵件功能http://php.net/manual/en/function.mail.php
嗨,早上好,
如果您在離線模式下工作,則可能需要phpmailer。
首先,您需要從此處https://github.com/PHPMailer/PHPMailer/archive/master.zip下載phpmailer。
然后粘貼到您的文件夾中。 如果我的編碼不能清除您的聲音,您可以從
https://github.com/PHPMailer/PHPMailer
<?php
require 'PHPMailerAutoload.php'; // Your Path
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user@example.com'; // Your mail
$mail->Password = 'secret'; // Your mail password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;
$mail->From = 'from@example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('joe@example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen@example.com'); // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
//Check Condition
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
第二種方式。
如果您以在線模式(擁有自己的域和托管)進行測試,則可以隨意復制和粘貼。
不需要phpmailer。
<?php
$error = [];
$receipientName="Fido";
$receipientEmail ="receipientmail.gmail.com";
$ccEmail ="";
//declares variable
if(isset($_POST['name'])) $name = $_POST['name'];
else $name = "";
if(isset($_POST['email'])) $email = $_POST['email'];
else $email = "";
function send_mail($myname, $myemail, $contactname, $contactemail, $subject, $message) {
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "X-Priority: 1\n";
$headers .= "X-MSMail-Priority: High\n";
$headers .= "X-Mailer: php\n";
$headers .= "From: \"".$myname."\" <".$myemail.">\r\n";
return(mail("\"".$contactname."\" <".$contactemail.">", $subject, $message, $headers));
}
if(isset($Submit) && $Submit=="Go") {
$emailContent ='';
$sent=send_mail($name, "yourmailname.gmail.com", "Fido", $receipientEmail, "Testing", $emailContent);
if($sent) {
echo $emailContent;
header('Location: contact.php');
}else{
echo "Failed";
exit;
}
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.