Html/php电子邮件页面/表单
我想使用php和HTML创建一个联系人页面,这样用户就可以直接从网站向我的电子邮件帐户发送消息 我唯一的问题是,它不会向我的电子邮件收件箱发送任何消息(页面上没有错误) 代码如下: HTML(无css样式):INDEX.HTMLHtml/php电子邮件页面/表单,php,html,email,Php,Html,Email,我想使用php和HTML创建一个联系人页面,这样用户就可以直接从网站向我的电子邮件帐户发送消息 我唯一的问题是,它不会向我的电子邮件收件箱发送任何消息(页面上没有错误) 代码如下: HTML(无css样式):INDEX.HTML <!DOCTYPE html > <html> <head> <title>Contact US</title> <meta http-equiv="Content-Type" con
<!DOCTYPE html >
<html>
<head>
<title>Contact US</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<div id="page-wrap">
<div id="contact-area">
SEND US A MESSAGE:
<form method="post" action="contactengine.php">
<label for="Name">Name:</label>
<input type="text" name="Name" id="Name" />
<label for="City">City:</label>
<input type="text" name="City" id="City" />
<label for="Email">Email:</label>
<input type="text" name="Email" id="Email" />
<label for="Message">Message:</label><br />
<textarea name="Message" rows="20" cols="20" id="Message"></textarea>
<input type="submit" name="submit" value="Submit" class="submit-button" />
</form>
</div>
</div>
</body>
</html>
联系我们
向我们发送一条消息:
姓名:
城市:
电邮:
消息:
PHP:contactengine.PHP
<?php
$EmailFrom = "JhonDoe@domain.com";<-- not sure about this one
$EmailTo = "MyEmail@domain.com";
$Subject = "WebSite Message";
$Name = Trim(stripslashes($_POST['Name']));
$Tel = Trim(stripslashes($_POST['Tel']));
$Email = Trim(stripslashes($_POST['Email']));
$Message = Trim(stripslashes($_POST['Message']));
// validation
$validationOK=true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
exit;
}
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Tel: ";
$Body .= $Tel;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
?>
我还建议您使用phpMailer 您只需要3个文件来发送电子邮件(“PHPMailer.php”“SMTP.php”“Exception.php”)
mail()
函数不负责。我建议使用+外部SMTP服务器。确实已将服务器配置为发送邮件吗?可能是您的服务器/主机未正确设置为发送邮件。如果我无法让服务器为我工作,我通常会使用swiftmailer。它严格基于php,在任何地方都能正常工作:目前我正在使用一个名为:ByetHost.com的免费网络托管服务,他们可能不允许发送电子邮件……如果这是您的代码,它将失败$EmailFrom=”JhonDoe@domain.com";
<?php
//change those variable only
$name_from = "organization";
$name_to = $username;
$email_sent_to = $email;
$email_sent_from = "yourmail@gmail.com";
$subject = "Your Email";
$smtp_username = "your user name";
$smtp_password = "**pass**";
$message = "
<!DOCTYPE html>
<html lang='en'>
**Your html code**
</html>
";
//Include required phpMailer files
require 'PHPMailer.php'; //add correct derectory
require 'SMTP.php'; //add correct derectory
require 'Exception.php'; //add correct derectory
//Define name spaces
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
//Create instance of phpMailer
$mail = new PHPMailer();
//Set mailer to use smtp
$mail->isSMTP();
//Define smtp host
$mail->Host = "smtp.gmail.com";
//Enable smtp authentication
$mail->SMTPAuth = "true";
//Set type of encryption (ssl/tls)
$mail->SMTPSecure = "tls";
//Set port to connect smtp
$mail->Port = "587";
//Set gmail userName
$mail->Username = $smtp_username;
//Set gmail Password
$mail->Password = $smtp_password;
//Set content type
$mail->isHTML(true);
//Set email Subject
$mail->Subject = $subject;
//Set sender email
$mail->setFrom($email_sent_from, $name_from);
//Email body
$mail->Body = $message;
//Add recipient
$mail->addAddress($email_sent_to, $name_to);
//Finally send mail
if ($mail->Send()){
header("Location: ../thankyou.php?email=$email");
exit();
}else{
header("Location: ../signup.php?error=xxx");
exit();
}
//Clossing smtp connection
$mail->smtpClose();
?>