我一直在试图解决这个问题,我在这里和其他论坛找到的解决方案,但似乎没有任何工作。我对PHP一窍不通。我不知道这个信息是否相关,但我在byethost.com上托管我的项目,他们说它支持PHP。
我在我的HTML联系人页面中制作了一个联系人表单,制作了一个PHP文件来处理数据并将其发送到我的gmail地址。它说消息已成功发送,但数据从未到达我的gmail收件箱。我尝试过用其他方法更改PHP代码(使用复制粘贴解决方案),但没有运气。我也试着把它发送到我的hotmail账户,但也不行。有人能帮我吗?
下面是HTML联系人表单:
<div id="form_wrap">
<form method="post" action="form_process.php" id="contact_form" class="contact">
<label>
<span>NOME*</span>
<input name="nome" type="text" tabindex="1" required>
</label>
<label>
<span>E-MAIL*</span>
<input name="email" type="email" tabindex="2" required>
</label>
<label>
<span>TELEFONE*</span>
<input name="phone" type="tel" tabindex="3" required>
</label>
<label>
<span>WEBSITE</span>
<input name="website" placeholder="http://" type="url" tabindex="4">
</label>
<label>
<span>MOTIVO DE CONTACTO*</span>
<select class="escolha" name="motivo" size="1" tabindex="5" required>
<option>Contratá-lo</option>
<option>Fazer uma pergunta</option>
<option>Dizer olá ou agradecer</option>
</select>
</label>
<div>
<label>
<span>MENSAGEM*</span>
<textarea name="message" tabindex="6" required></textarea>
</label>
</div>
<div>
<input name="submit" type="submit" value="Enviar" id="submit">
</div>
</form>
<a class="subtop" href="#subtop">– Voltar ao topo</a>
</div>
这是我的form_process.php:
<?php
if(isset($_POST['submit'])) {
$nome = $_POST['nome'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$website = $_POST['website'];
$motivo = $_POST['motivo'];
$message = $_POST['message'];
$to = "mygmailadress@gmail.com";
$subject = "Site contact form";
$header = "From: ".$fromText."'r'n";
$header .= "Cc: ".$ccText."'n";
$header .= "Reply-To : ".$fromText."'r'n";
$header .= "Return-Path : ".$fromText."'r'n";
$header .= "X-Mailer: PHP'r'n";
$header .= "MIME-Version: 1.0'r'n";
$header .= "Content-Type: text/plain; charset=iso-8859-1'r'n";
mail($to, $subject, $header, $message, "From: " . $nome);
}
if(@mail($emailRecipient, $subject, $message, $headers))
{
echo "Mail Sent Successfully";
}else{
echo "Mail Not Sent";
}
?>
为什么有两次邮件功能?
- mail有4个参数,你给5个参数(from是第四个的一部分,它应该在$header中)
- 邮件功能中使用的变量
$emailRecipient
不存在 - 在'from'头应该是一个电子邮件地址(和名字),而不仅仅是一个非邮件字符串
邮件没有收到可能有很多原因。
它可能被垃圾邮件捕获,被您的主机阻止,或者您的邮件功能可能在php.ini文件中设置错误。
但是,从你的代码来看,你似乎错误地使用了mail()函数。
你试图通过调用函数两次发送邮件。只需调用一次,如:
if(mail(...)) {
echo 'good times';
} else {
echo 'boo, no email was sent';
}
其次,你使用函数不正确。根据这里的文档,邮件函数有五个参数,如下所示:
bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
$additional_headers
和$additional_parameters
是可选的,用[方括号]表示。$to
、$subject
、$message
按此顺序排列。
第三,我强烈建议不要使用内置的mail()函数。
建议使用SwiftMailer。这是一个全面的PHP库,它会照顾你。
您试图发送两次邮件,您使用了错误的变量名。
这段代码适合我。
<?php
if(isset($_POST['submit'])) {
$nome = $_POST['nome'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$website = $_POST['website'];
$motivo = $_POST['motivo'];
$message = $_POST['message'];
$to = "asdf@gmail.com";
$subject = "Site contact form";
$header = "From: ".$email."'r'n";
$header .= "Cc: ".$email."'n";
$header .= "Reply-To : ".$email."'r'n";
$header .= "Return-Path : ".$email."'r'n";
$header .= "X-Mailer: PHP'r'n";
$header .= "MIME-Version: 1.0'r'n";
$header .= "Content-Type: text/plain; charset=iso-8859-1'r'n";
if(mail($to, $subject, $message, $header))
{
echo "Mail Sent Successfully";
}else{
echo "Mail Not Sent";
}
}
?>
如果您使用的是免费主机,它们可能会限制您发送电子邮件的能力。类似的事情正在发生:
https://www.freehosting.com/client/knowledgebase.php?action=displayarticle& id = 25
PHP邮件功能仅限于免费帐户,以防止滥用。为了使其工作,您的邮件脚本应该配置为使用SMTP服务器'cpanel.freehosting.com',并使用在cpanel中设置的电子邮件帐户的凭据对其进行身份验证。付费帐户可以无限制地访问PHP邮件功能。
你可以在这个链接中找到更多关于在PHP脚本中设置电子邮件认证的信息:http://email.about.com/od/emailprogrammingtips/qt/PHP_Email_SMTP_Authentication.htm
可选的,PHP mail()可以通过购买相应的插件HERE来启用。