So ive just implmented mailgun into a website for sending contact form info.
This works some of the time, but mostly the page hangs when i press send with the message "waiting for url..." in the bottom left of chrome.
There is no ssl on the server hence the strange mailgun constructor.
This is my php which is placed just inside the body of my website.
<?php
require 'mailgun-php/vendor/autoload.php';
use Mailgun\Mailgun;
if(isset($_POST['register'])){
$message = "Contact Form.\n\n".
"Name: ".$_POST['name']."\n".
"Email: ".$_POST['email']."\n".
"Message: ".$_POST['message']."\n";
$mg = new Mailgun(*my key*, "api.mailgun.net", "v2", false);
$domain = *my domain*;
$mg->sendMessage($domain, array(
'from'=>'Contact Form <build#<url>>',
'to'=> *email*,
'subject' => ' Contact Form',
'text' => $message
)
);
header('Location: ?sent=1');
}
?>
This is the form code:
<form method="post" action="index.php">
<div class="row 50%">
<div class="6u 12u$(mobile)"><input type="text" class="text" name="name" placeholder="Name" /></div>
<div class="6u$ 12u$(mobile)"><input type="text" class="text" name="email" placeholder="Email" /></div>
<div class="12u$">
<textarea name="message" placeholder="Message"></textarea>
</div>
<div class="12u$">
<button class="button" type="submit" name="register"> Send Message </button>
</div>
</div>
</form>
I'm not getting any errors when the email is not sent. The page will eventually reload after the submit button is pressed but the header redirect is not being applied (which im assuming is because the email was not sent successfully).
The php error logs do not show anything going wrong either.
Thanks
Maybe that's a (very) late answer, but I've just met the same problem. And I discovered, that Mailgun has whitelist of IP addresses, so in case you haven't added there your public IP address, the connection to Mailgun's API will never be established.
At the moment of writing this answer, the whitelist is present under this link: https://app.mailgun.com/app/account/security/api_keys
Related
I have put together a simple contact form for my website. Using PHP to POST the data and send directly to my email address. But for some reason every time I visit the page on my website. I still get the test message Displaying under the Form. Then when I reload the website and visit the link again it still displays the thank you message. and automatically sends an email. Im still in testing mode 2 days before my launch and I need this figure out. Considering I am novice to php I dont know what goes where.... check out my website to get a live view https://trillumonopoly.com (click "Contact Us" link in menu) I would like for the contact form to disappear and echo the thank you message once sent. And reset after the page is reloaded. I am also using Jquery ajax to load all my pages into a div container. So I would like to keep the content inside that div without forwarding to the Echo message page, leaving my index page
Heres My ajax code
$(document).ready(function () {
loadMainContent('main');
$('body').delegate('.navMenu', 'click', function (event) {
event.preventDefault();
loadMainContent($(this).attr('href'));
});
});
function loadMainContent(page) {
$('#main').load('pages/' + page + '.php');
}
here is html for the form:
<div class="general row container-fluid"><br>
<center><img src="img/divider.png" class="img-fluid"></center>
<div class="col-lg-6 col-sm-12">
<img src="img/logo.png" class="img-fluid" height="540px" width="540px">
</div>
<div class="col-lg-6 col-sm-12 container"><center>
<br><h1 class="form-title">Contact Us</h1><br></center>
<div class="container">
<form action="pages/mail.php" method="GET" class="box2">
NAME:
<input type="text" name="name" placeholder="YOUR NAME HERE" required>
<br><br>
EMAIL:
<input type="email" name="email" placeholder="YOUR EMAIL HERE" required>
<br><br>
MESSAGE:<br>
<textarea name="message" rows=10 cols=23 placeholder="YOUR MESSAGE HERE" required></textarea>
<br><Br>
<button type="submit" value="Message Sent" class="btn btn-danger btn-lg" style="background-color:"red">SUBMIT</button
</form>
<center><?php include('mail.php'); ?></center>
</div>
</div>
</div>
Here is My simple PHP:
<?php
$name = $POST['name'];
$email = $POST['email'];
$message = $POST['message'];
mail("info#trillumonopoly.com","ILLUMONOPOLY WEB Contact", $message,"From: $email\r\n");
echo "Thank You For Contacting Us!";
?>
Better than check everything before send mail.
if(isset($_POST['name']) && isset($_POST['email']) && isset($_POST['message'])){
$name = $POST['name'];
$email = $POST['email'];
$message = $POST['message'];
mail("info#trillumonopoly.com","ILLUMONOPOLY WEB Contact", $message,"From: $email\r\n");
echo "Thank You For Contacting Us!";
}
And send POST request to the index, not mail.php.
<form action="" method="POST">
...
So the user can see your message at the end of contact form.
I've searched through the forums and found a lot of threads on this topic but can't seem to find the correct answer or solution for my problem.
I've got a contact form which I can't seem to get working.
HTML:
<form action="php/index.php" id="contact-form" method="post" name="form">
<div>
<label>
<span>Naam: (verplicht)</span>
<input name="vname" placeholder="Uw naam" type="text" tabindex="1" required>
</label>
</div>
<div>
<label>
<span>Email: (verplicht)</span>
<input name="vemail" placeholder="Uw e-mail adres" type="email" tabindex="2" required>
</label>
</div>
<div>
<label>
<span>Telefoon:</span>
<input name="vphone" placeholder="Telefoon nummer" type="tel" tabindex="3">
</label>
</div>
<div>
<label>
<span>Bericht: (verplicht)</span>
<textarea name="msg" placeholder="Uw vraag of opmerking" tabindex="4" required></textarea>
</label>
</div>
<div>
<input id="send" name="submit" type="submit" value="VERZENDEN">
</div>
</form>
<!-- /Form -->
And here is the PHP code, I replaced my e-mail adress in the form with MY EMAIL for obvious reasons.
<?php
if(isset($_POST['submit'])) {
$name = $_POST['vname'];
$remark = $_POST['msg'];
$from_add = $_POST['vemail'];
$to_add = "MY EMAIL";
$subject = "Your Subject Name";
$phone = $_POST['vphone'];
$headers = 'From: $from_add';
if ($phone != null){
$message = "Naam:$name \n Opmerking: $remark \n mijn telefoon nummer is $phone";
mail($to_add,$subject,$message,$headers);
}else{
$message = "Naam:$name \n Opmerking: $remark";
mail($to_add,$subject,$message,$headers);
}
}
?>
Ow I am using XAMPP to run a localhost, perhaps that could be the problem but I'm not sure.
using the php mail driver aint good for several reasons ,
u can instead try something call phpmailer http://phpmailer.worxware.com which will teach u a better way of doing things like that without frustration ,also incase later u wanted to use a php framework u will feel right at home because they will mostly be using the same syntax.
note that sending mails at first may take some time ,maybe even hours b4 u get anything ,for that u can use something like a fakesmtp https://nilhcem.github.io/FakeSMTP/ to catch the emails.
I had that same problem with a horrible server which doesn't send emails.
So I used a code that connects to a gmail account and sends the email from there.
try this tutorial:
http://coded-words.tumblr.com/post/6936532107/configure-gmail-as-smtp-in-xampp-to-send-mail
Edit 23.12.2014:
Do you have something like this in your code?
<?php
$message = "The mail message was sent with the following mail";
$headers = "From: youremail#gmail.com";
mail("youremail#gmail.com", "Testing", $message, $headers);
?>
(source: http://jiansenlu.blogspot.co.il/2014/10/php-mail-function-using-gmail-account.html)
If you do have this code:
Try submitting a different email address than the one you used in your code.
Sometimes it goes to the "Sent mail" in your Gmail account without actually showing you that you received a new email. (So you can check your "Sent mail" to see if there is anything over there).
Given a standard html form such as the one below;
<form method="post" action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>>
<div class="row half">
<div class="6u"><input type="text" placeholder="Name" name="name" /></div>
<div class="6u"><input type="text" placeholder="Email" name="email" /></div>
</div>
<div class="row half">
<div class="12u"><textarea name="message" placeholder="Message" name="message"></textarea></div>
</div>
<div class="row">
<div class="12u">
<ul class="actions">
<li><input type="submit" class="button" value="Send Message" name="submit" /></li>
<li><input type="reset" class="button alt" value="Clear Form" name="clear"/></li>
</ul>
</div>
</div>
</form>
And the following php
if(isset($_POST['submit'])){
$to = "enquiries#appcloudkent.com"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$name = $_POST['name'];
$subject = "Website Enquiry";
$message = $name . " wrote the following:" . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
if(isValidString($from) && isValidString($name) && isValidString($message)){
mail($to,$subject,$message,$headers);
}
}
It is possible to send an email, however, this approach causes a couple of problems. Because my form is at the bottom of the page, when send is clicked the page is redirected back and the focus goes back to the top of the page.
What is the correct way to provide adequate user feedback to let the user know the email was sent, is it possible to navigate back to the page and automatically scroll to the bottom - allowing me to change the send button to green or something?
Failing that, is there a better approach to doing this?
Thanks
Add an anchor link before your form.
<a id="anchorName"></a>
Post your form to the anchor.
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>#anchorName">
The form action should point to the page itself (like it does in your example) so you can display error messages from validation (like you don't).
If the form is not at the top of your page you can add an anchor:
<h2><a name="theForm">The form</a></h2>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>#theForm">
If the form is processed properly (=mail sent), it is good practice to redirect to another page, showing a success message. So pressing F5 doesn't submit the form again (and send another mail).
if (mail($to,$subject,$message,$headers)) {
// redirect to page showing success message and exit script
} else {
// redirect to page showing error message and exit script
}
I usually redirect to the same page again, but attach an param ($_SERVER["PHP_SELF"].'?mail_success=1') and then inside the template I decide whether to show the success message, error message or the form:
if (isset($_REQUEST['mail_success'])) {
// show success message
} elseif (isset($_REQUEST['mail_error'])) {
// show error message, technical issues, try again bla bla
} else {
// show the form (including validation errors if necessary)
}
You could submit the form using an XMLHttpRequest, and cancel the default action of pressing submit. jQuery's .post() and event.preventDefault() methods are particularly good at that.
Im trying to create a "contact us" form where visitors will be able to email the site owner.
so far, my form works if the visitor who filled up the form uses a gmail address. but once they use a yahoo email address the recipient(site owner) does not receive the mail
below is how i did the form
controller
public function send_email(){
$data = $_POST;
$new = $this->base_model->send_email($data);
$this->session->set_flashdata("email_status","Message successfully sent");
redirect(base_url("contact"));
}
model
public function send_email($email_data)
{
$from = $email_data['email'];
$subject = $email_data['subject'];
$message = $this->load->view("emails/message",$email_data,TRUE);
$config['crlf'] = "\n";
$config['mailtype'] = "html";
$config['protocol'] = "sendmail";
$this->load->library('email');
$this->email->initialize($config);
$this->email->from($from,$email_data['name']);
$this->email->to("site owner email address goes here");
$this->email->subject($subject);
$this->email->message($message);
$this->email->send();
return $this->email->print_debugger();
}
view
<?php
$attributes = array("id"=>"form_req");
echo form_open("send_email",$attributes);
?>
<div class="controls">
<label>Name</label>
<input type="text" name="name" class="span12 req">
</div>
<div class="controls">
<label>Email</label>
<input type="text" name="email" class="span12 req">
</div>
<div class="controls">
<label>Subject</label>
<input type="text" name="subject" class="span12 req">
</div>
<div class="controls">
<label>Message</label>
<textarea name="message" class="span12 req"></textarea>
</div>
<button class="btn"><i class="fa fa-send"></i> Send</button>
<?php
echo form_close();
?>
have i missed something? or is there a workaround for this?
Is there any reason that the email needs to come "from" the email address that filled in the form? I've had issues in the past with spam filters, Yahoo being one of them, spamming emails where the domain of the website sending the email doesn't match the email address the email is "from". And with good reason, I suppose. Yahoo may be discarding this dangerous looking "spam" before it even reaches the ordinary spam box.
I would try having the emails always come from noreply#your-domain-here.com and add a reply to header if you absolutely want the owner to be able to reply directly to the email.
EDIT:
Found this old question with a similar issue, which suggests that changing the from email should solve it: Everytime my mail goes to spam in phpmailer
I made a from in php and it seems to work. When i hit the submit button it goes to the next page and show the message has been send, except i don't receive anything in my mailbox.
Probably easy to fix but I'm pretty new at this all and tried all afternoon yesterday and running out of ideas.
<form class="well" method="post" action="form.php">
<label><b>Klantgegevens</b></label> <br>
<span class="inline span2">Winkelnaam</span><input type="text" name="winkelnaam" class="span10">
<span class="inline span2">Straat</span><input type="text" name="straat" class="span10">
<span class="inline span2">Huisnummer</span><input type="text" name="huisnummer" class="span10">
<button type="submit" class="btn btn-info">Verstuur</button>
</form>
This is the form.php
<?php
$winkelnaam = $_POST["winkelnaam"];
$straat = $_POST["straat"];
$huisnummer = $_POST["huisnummer"];
mail('xxxx#xxxx.nl');
echo 'Uw bericht is verzonden.';
?>
You did not provide a mail subject and mail message.
See PHP mail() documentation for more info.
mail('xxxx#xxxx.nl', 'My subject', 'Mail message text here');
mail('email#address', 'Subject', 'Plain text message', $optional_headers);
But I recommend using a framework to send email instead of the mail() function.
Take a look at http://swiftmailer.org/