我无法弄清楚为什么我尝试向自己发送电子邮件(使用其他电子邮件)时不会收到电子邮件。包括论坛,php和javascript代码。
下面是php代码
$from = '';
$sendTo = 'myemailhere';
$subject = '';
$fields = array('name' => 'Name', 'surname' => 'Surname', 'phone' => 'Phone', 'email' => 'Email', 'message' => 'Message');
$okMessage = 'Contact form successfully submitted. Thank you, I will get back to you soon!';
$errorMessage = 'There was an error while submitting the form. Please try again later';
error_reporting(0);
try
{
if(count($_POST) == 0) throw new \Exception('Form is empty');
$emailText = "You have a new message from your contact form\n=============================\n";
foreach ($_POST as $key => $value) {
if (isset($fields[$key])) {
$emailText .= "$fields[$key]: $value\n";
}
}
$headers = array('Content-Type: text/plain; charset="UTF-8";',
'From: ' . $from,
'Reply-To: ' . $from,
'Return-Path: ' . $from,
);
mail($sendTo, $subject, $emailText, implode("\n", $headers));
$responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
$responseArray = array('type' => 'danger', 'message' => $errorMessage);
}
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);
header('Content-Type: application/json');
echo $encoded;
}
else {
echo $responseArray['message'];
}
下面是JavaScript代码
$(function () {
$('#contact-form').validator();
$('#contact-form').on('submit', function (e) {
if (!e.isDefaultPrevented()) {
var url = "contact.php";
$.ajax({
type: "POST",
url: url,
data: $(this).serialize(),
success: function (data)
{
var messageAlert = 'alert-' + data.type;
var messageText = data.message;
var alertBox = '<div class="alert ' + messageAlert + ' alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>' + messageText + '</div>';
if (messageAlert && messageText) {
$('#contact-form').find('.messages').html(alertBox);
$('#contact-form')[0].reset();
}
}
});
return false;
}
})
});
下面是论坛代码
<form id="contact-form" method="post" action="contact.php" role="form">
<div class="messages"></div>
<div class="controls">
<div class="row" style="margin: auto;">
<div class="col-md-6">
<div class="form-group">
<label for="form_name">Firstname</label>
<input id="form_name" type="text" name="name" class="form-control" placeholder="Please enter your firstname *" required="required" data-error="Firstname is required.">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="row" style="margin: auto;">
<div class="col-md-6">
<div class="form-group">
<label for="form_lastname">Lastname</label>
<input id="form_lastname" type="text" name="surname" class="form-control" placeholder="Please enter your lastname *" required="required" data-error="Lastname is required.">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="row" style="margin: auto;">
<div class="col-md-6">
<div class="form-group">
<label for="form_email">Email</label>
<input id="form_email" type="email" name="email" class="form-control" placeholder="Please enter your email *" required="required" data-error="Valid email is required.">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="row" style="margin: auto;">
<div class="col-md-12">
<div class="form-group">
<label for="form_message">Message</label>
<textarea id="form_message" name="message" class="form-control" placeholder="Message for me *" rows="4" required="required" data-error="Please, leave us a message."></textarea>
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-12">
<input type="submit" class="btn btn-success btn-send float-right" value="Send message">
</div>
</div>
</div>
</div>
</form>
我还没有完全涉足php,但是我知道这应该可以工作,但是我不确定为什么它不起作用。我已经尝试过在实时服务器上向自己发送电子邮件,但似乎无济于事,所以我不知道为什么它不发送电子邮件。真的很感谢任何帮助,因为我不知道为什么它不起作用。
答案 0 :(得分:0)
确保您有可用的SMTP服务器。
我试图使用https://github.com/verot/class.upload.php从本地服务器发送电子邮件,但是直到我开始在远程服务器(如您所说的实时服务器)上工作之前,我什么都没收到。
看看phpmailer
,它对我有用。
示例:
require_once("phpmailer/class.phpmailer.php");
$mail = new PHPMailer(true);
$mail->CharSet = "UTF-8";
$mail->isSMTP();
$mail->From = 'donotreply@xxx.com';
$mail->FromName = "Your company's name";
$mail->AddAddress("$email","");
$mail->isHTML(true);
$mail->Body = "Hi";
$mail->Send();