PHP mail on submit [duplicate] - php

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
I am trying to get a contact form using the built in mail function to work but I'm running into some problems.
What I would like:
On clicking 'submit' the form is sent to defined emailadres.php.
If sent successfully, a confirmation message is displayed.
If not sent, an error message is displayed.
What is currently happening:
Confirmation message already shows on page load; regardless of form submission.
Mail is not sent.
Code I'm using:
if ($_POST["submit"]) {
mail ($to, $subject, $body, $from);
$sendErr = "Your message has been sent";
} else {
$sendErr = "Your message could not be sent";
}
I'm fairly new to all this so any help figuring out where my thinking stalls would be appreciated. If I need to post more parts of the form I will.

The code you are using does not even check if the mail was sent successfully, it only checks it the formular was submitted. mail() returns true if the mail was sent successfully, false if not. So you can check its return value:
if ($_POST["submit"]) {
$sent = mail ($to, $subject, $body, $from);
// Check here if the mail was sent or not
if ($sent) {
$sendErr = "Your message has been sent";
} else {
$sendErr = "Your message could not be sent";
}
}

Related

PHP Mail - can't find mistake [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
So this is my code for sending an email. When I try to send an email i get right message, in this case "User Exist" and "Email is sent", but i can't receive an email. So can somebody find mistake and try to help me?
<form action="forgot2.php" method="post">
Enter you email ID: <input type="text" name="email">
<input type="submit" name="submit" value="Send">
</form>
error_reporting(0);
$email=$_POST["email"];
$Subject = "GOSPodin covek";
if($_POST['submit']=='Send')
{
mysql_connect('localhost','root','') or die(mysql_error);
mysql_select_db('login');
$query="select * from clanovi where email='$email'";
$result=mysql_query($query) or die(error);
if(mysql_num_rows($result))
{
echo "User exist";
}
else
{
echo "No user exist with this email id";
}
}
if(mysql_num_rows($result))
{
$code=rand(100,999);
$message="You activation link is: http://localhost/login_form/forgot.php? email=$email&code=$code";
mail($email, "Subject Goes Here", $message);
echo "Email sent";
}
else
{
echo "No user exist with this email id";
}
First of all, you need to check if the mail is actually sent:
if (mail($email, "Subject Goes Here", $message)) {
// then it's actually sent.
}
If mail() doesn't return false, then the mail was passed to your local mail server for delivery. Presume you already have a mail server set up on your local test box? If not, problem is obvious...
If PHP checks out OK and you still don't receive the mail (and it's not in your spam folders etc.), you need to investigate your server's mail logs for possible issues outside PHP.
...and for the love of code and all things sacred to debugging, do NOT use error_reporting(0); if you're actually trying to figure out what's wrong! (Error messages are kinda helpful for that.)
Are you sure your script is actually sending the mail?
Replace:
mail($email, "Subject Goes Here", $message);
echo "Email sent";
By:
if(#mail($email, "Subject Goes Here", $message)) {
echo "Email sent";
} else {
echo "Email not sent";
}
Assuming mail() does return true and that the email is a valid one, you want to make you have a valid smtp server configured in php.ini in the [mail function] section.
Just try
I use this works fine for me
$headers = 'From: support#example.com';
mail($payer_email, 'Tokens', "Thank you for purchase tokens $additional-message", $headers);
change this one.
$query="select * from clanovi where email='".$email."'";

Change pop up 'email has been sent' to bootstrap 'contextual background'?

I have a pop up on my website that lets the user know that the email has been sent.
I want to change that to a nicer message via bootstrap (website is made from bootstrap)
Instead of a pop up message, I'd like a contexual background.
You can see them here: Contextual backgrounds
http://getbootstrap.com/css/#helper-classes
This is what the popup code looks like.
if(wp_mail($to, $subject, $msg, $headers, $attachments)) { echo
'The message was sent. Check your email inbox.'; } else {
echo 'The message was not sent!'; };
enter code here
What do I need to do to change it?
You can echo html code with PHP:
if(wp_mail($to, $subject, $msg, $headers, $attachments)) {
echo '<p class="bg-success">The message was sent. Check your email inbox.</p>';
}
else {
echo '<p class="bg-danger">The message was not sent!</p>'; };
}

not able to send E Mail using xampp

Hi I was trying to send Email using php in xammp
I have already started Mercury Service
Heres my code
<?php
$to = "nikhil08514#gmail.com";
$subject = "Hi!";
$body="test";
$headers = "From: root#localhost.com";
if (mail($to, $subject, $body, $headers))
{
echo "Message successfully sent!";
}
else
{
echo "Message delivery failed...";
}
?>
when i execute code i am getting output as
Message successfully sent!
But when i check my mail box.I dont see mail.I checked all folders in my Mailbox but its not present
your code is right try that on server it will work surely, xampp does not send directly like that from xammp. by using smtp you can send.

PHP Sending Email Error

I'm trying to send an email using PHP code, I'm sure the code is right but I'm not receiving any emails. Can anyone see a problem with this code:
<?php
$to = 'i7906890#bournemouth.ac.uk';
$subject = 'Registration Complete';
$message = 'Thank you for joining us at Arsenic & Vice';
$header = 'From: admin#arsenicandvice.co.uk';
if (mail($to, $subject, $message, $header)){
echo('<p>Sent</p>');
} else {
echo('<p>Fail</p>');
}
?>
I've tested your code on my server and it works fine, I've received the e-mail.
I've also run it so you can receive it on i7906890#...
The problem is on your hosting. Either your e-mail server (qmail...) has a long queue or it is stuck. Contact your server admin.

ixwebhosting php mail() issues with subject

I encounter a very strange problem with ixwebhosting.
I am able to send email using the php mail() function with $subject = "test";
But if $subject = "testing of information send"; then i won't be able to receive any email
But actually "Mail sent!" was displayed in the php page.
if (!mail($email, $subject, $body, $from)) { echo "Error Sending Email!"; }
else
{ echo "Mail sent!"; }
Is it possible that the email is being classed as spam somewhere down the track? Are you able to get logs from the mailserver indicating whether mail gets sent?

Categories