Mail function doesn't work for gmail - php

I'm creating php page and create button that post to php script. In my script, I create what normal mail functions need. I passed all the variables, it successfully sent. But my gmail didn't received any mail.
sendmail.php
<?php
$to = 'mygmail#gmail.com';
$subject = 'Message from my page';
$headers = "To: email#hotmail.com\n" .
"From: From Address <from#mydomain.com>\n" .
"MIME-Version: 1.0\n" .
"Content-Type: text/html; charset=iso-8859-1";
$message = 'TheMessage Body';
mail( $to, $subject, $message, $headers );
$result = mail($to, $subject, $message, $headers);
if($result) {
echo "MAIL SENT";
}else {
echo 'MAIL NOT SENT';
}
?>
HTML
<form action="sendmail.php" method="POST" enctype="text/plain" id="contactForm">
<div class="col-sm-12">
<!-- if mail sent successfully -->
<h4 class="success">
<i class="fa fa-check"></i> Your message has been sent successfully.
</h4>
<!-- if mail sent unsuccessfully -->
<h4 class="error">
<i class="fa fa-warning"></i> E-mail must be valid and message must be longer than 1 character.
</h4>
</div>
<div class="col-sm-6">
<input id="name" type="text" name="name" placeholder="Your Name">
</div><!-- col-sm-6 end -->
<div class="col-sm-6">
<input id="email" type="email" name="email" placeholder="Your Email">
</div><!-- col-sm-6 end -->
<div class="col-md-12">
<textarea type="text" name="messages" class="textarea-box" id="message" rows="5" placeholder="Message"></textarea>
<button class="btn-new btn-bold" type="submit" id="submit" name="submit">Submit Message</button>
</div>
</form>

The email you are using on "From:" must exist in your server.
Another point, have you tried to send it a non-Gmail email account - does it arrive?

Related

Error 405 when trying to send an email from a static page

I have a static site up (my portfolio site), with a form to send an email to me using a custom php.
the page is through github pages and the custom domain is through google.
the form is:
<div class="col-sm-12 col-md-6 wow fadeInUp" data-wow-duration="0.8s" data-wow-delay="0.2s">
<form id="contactForm" class="single-form quate-form wow fadeInUp" data-toggle="validator">
<div id="msgSubmit" class="h3 text-center hidden"></div>
<div class="row">
<div class="col-sm-12">
<input name="name" class="contact-name form-control" id="name" type="text" placeholder="First Name" required>
</div>
<div class="col-sm-12">
<input name="name" class="contact-email form-control" id="L_name" type="text" placeholder="Last Name" required>
</div>
<div class="col-sm-12">
<input name="name" class="contact-subject form-control" id="email" type="email" placeholder="Your Email" required>
</div>
<div class="col-sm-12">
<textarea class="contact-message" id="message" rows="6" placeholder="Your Message" required></textarea>
</div>
<!-- Subject Button -->
<div class="btn-form col-sm-12">
<button type="submit" class="btn btn-fill btn-block" id="form-submit">Send Message</button>
</div>
</div>
</form>
</div>
And the php to send it is:
<?php
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];
$EmailTo = "#gmail.com";
$Subject = "Portfolio CV/Resume";
// prepare email body text
$Body .= "Name: ";
$Body .= $name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $message;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From:".$email);
// redirect to success page
if ($success){
echo "success";
}else{
echo "invalid";
}
?>
The actual error I get is:
POST https://codewithmarcus.com/process.php 405 jquery.min.js:4
I removed my actual email address from the code but rest assured it is in there correctly, any help would be appreciated.
405 http code means Not Allowed response status code indicates that the request method is known by the server but is not supported by the target resource.
And you won't be able to send email via Gmail through this script. To send email through Gmail, you need send it through SMPT server. You can use PHPmailer libraryfor that.

Is there anything wrong with the code in my form?

I'm using this tutorial to create a contact form for my site and it doesn't seem to be working. After I fill in my form and press submit, nothing happens. I would like to know whats wrong with it and how I can fix it.
This is my code:
HTML:
<form action="mail/contact.php" method="post">
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Name:</label><br />
<input type="text" name="name" id="name" class="form-control" placeholder="Name"/>
</div></div>
<br />
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Email:</label><br />
<input type="text" name="email" id="email" class="form-control" placeholder="Email"/>
</div></div>
<br />
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Message:</label>
<textarea name="message" rows="5" id="message" class="form-control" placeholder="Message"></textarea>
</div></div>
<br />
<input type="submit" name="submit" value="Submit" class="btn btn-success btn-lg"/>
</form>
PHP:
<?php
// from the form
$name = trim(strip_tags($_POST['name']));
$email = trim(strip_tags($_POST['email']));
$message = htmlentities($_POST['message']);
// set here
$subject = "Contact form submitted!";
$to = 'name#mail.com';
$body = <<<HTML
$message
HTML;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
// send the email
mail($to, $subject, $body, $headers);
// popup success
echo '<script language="javascript">';
echo 'alert("Your message has been sent!")';
echo '</script>';
?>
It's not a problem actually, you hace to change $to to $email in the email() method because the variable that has the email you want to send to is $email.
Your contact.php code will be:
<?php
// from the form
$name = trim(strip_tags($_POST['name']));
$email = trim(strip_tags($_POST['email']));
$message = htmlentities($_POST['message']);
// set here
$subject = "Contact form submitted!";
$body = <<<HTML
$message
HTML;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
// send the email
mail($email, $subject, $body, $headers);
// popup success
echo '<script language="javascript">';
echo 'alert("Your message has been sent!")';
echo '</script>';
?>
Another recommendation is to check this tutorial for a secure contact form.
Also you should check out this two php frameworks:
Symfony 2 Mail
Zend 2 Mail

Contact Form Not Working (PHP)

I have a contact form on a page that should send the details/message to an email address. You can view it here (it's in the footer): http://tedsrestaurant.com/index-working.html
HTML
<form id="contact-form" class="row style1" action="#" method="post">
<div class="col-xs-12 col-sm-4">
<div class="form-group">
<input name="name" type="text" id="name" placeholder="Your name" required />
</div>
<div class="form-group">
<input name="number" type="text" id="number" placeholder="Your number" required />
</div>
<div class="form-group">
<input name="email" type="email" id="email" placeholder="Your e-mail" required />
</div>
</div>
<div class="col-xs-12 col-sm-8">
<textarea name="message" rows="4" class="h130" id="message" placeholder="Write your message..." required></textarea>
</div>
<div class="clear"></div>
<div class="col-md-9"><p>* When booking the <strong>Food Truck</strong>, please write date, time and number of guests into your message.</p></div>
<div class="col-md-3 right">
<input class="submit btn" type="submit" value="Send a message" />
<input class="modal btn hidden" type="button" value="Send a message" name="" data-toggle="modal" data-target="#modalBox" />
</div>
<!-- Modal window -->
<div class="modal fade bs-modal-md" id="modalBox" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog modal-md">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Contact form</h4>
</div>
<div class="modal-body"></div>
<div class="modal-footer">
<button type="button" class="btn right" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</form>
And the PHP is:
<?php
//******** Email settings ********//
//your email
$email_to = "myemail#gmail.com";
//subject of email
$email_subject = "This is a message";
//message after success send mail
$email_send = "<div class='alert alert-success'>
<strong>Contact form was sent successfully.</strong>
<br/>Thank you for contacting us. We will be in touch with you very soon.</div>";
if($_POST) {
//******** Email data ********//
$cust_name = $_POST['name'];
$cust_number = $_POST['number'];
$cust_email = $_POST['email'];
$cust_message = $_POST['message'];
$header = "From:" . $cust_email . "\r\n";
$header .= 'MIME-Version: 1.0' ."\r\n";
$header .= 'Content-Type: text/html; charset=UTF-8' . "\r\n";
$body = "<strong>Customer email:</strong> " . $cust_email . "<br/>";
$body .= "<strong>Customer number:</strong> " . $cust_number . "<br/>";
$body .= "<strong>Customer name:</strong> " . $cust_name . "<br/>";
$body .= "<strong>Message:</strong> " . $cust_message;
if($cust_name && $cust_number && $cust_email && mail($email_to, $email_subject, $body, $header)) {
echo $email_send;
echo '<script>$("#contact-form").each(function(){
this.reset();
});
</script>';
} else {
echo "<div class='alert alert-warning'><strong>Error sending mail. Please fill the contact form correctly or contact website administrator.</strong></div>";
}
}
Does everything look correct? FYI the PHP on the site has a real email address, not myemail#gmail.com. Once I hit the submit button I am taken to the same page, but nothing ever comes to my e-mail.
What is the output that you receive? An email_sent or error message?
If you get neither which I guess is the case, you should be using
if($_SERVER['REQUEST_METHOD'] == "POST") in place of if($_POST) to check if the request type is POST and further proceed.
I recommend to use SwiftMailer.
require_once 'lib/swift_required.php';
// Create the Transport
$transport = Swift_SmtpTransport::newInstance('smtp.example.org', 25)
->setUsername('your username')
->setPassword('your password')
;
/*
You could alternatively use a different transport such as Sendmail or Mail:
// Sendmail
$transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs');
// Mail
$transport = Swift_MailTransport::newInstance();
*/
// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
// Create a message
$message = Swift_Message::newInstance('Wonderful Subject')
->setFrom(array('john#doe.com' => 'John Doe'))
->setTo(array('receiver#domain.org', 'other#domain.org' => 'A name'))
->setBody('Here is the message itself')
;
// Send the message
$result = $mailer->send($message);

Simple contact form won't work - php

Again - I've looked and troubleshooted for about an hour and cannot get the php contact form to work. I get the "Thank you!" email, but I haven't received the email to my inbox ever. I've done it about 20 times.
<div class="container">
<div class="row">
<div class="col-lg-12">
<h5>Want to contact me quickly? Use this form!</h5>
<form id="contact" method="post" action="submit.php" class="form" role="form">
<div class="row">
<div class="form-group col-lg-4">
<label>Name</label></br>
<input type="text" name="name" placeholder="Enter your full name" required autofocus>
</div>
<div class="form-group col-lg-4">
<label>Email address</label></br>
<input type="email" name="email" placeholder="Enter your email address" required autofocus>
</div>
<div class="form-group col-lg-4">
<label>Subject</label></br>
<input type="text" name="subject" placeholder="Subject">
</div>
<div class="form-group col-lg-12">
<label>Message</label>
<textarea name="comments" name="message" data-provide="markdown-editable" rows="6" placeholder="Let's chat!" style="width:100%"></textarea>
</div>
<div class="form-group col-lg-12 form-action">
<input type="hidden" name="Submit" value="contact">
<button type="submit" class="btn btn-default">Submit</button>
</div>
</form>
</div>
</div>
PHP:
<?php
if ($_POST['Submit'])
{
$name=$_POST['name'];
$email=$_POST['email'];
$subject=$_POST['subject'];
$comments=$_POST['comments'];
$to='MY EMAIL ADDRESS';
$from='website';
if (mail($to, $subject, $name, $email, $comments))
{
echo 'Thank you!';
}
else
{
echo 'Something went wrong! Try again';
}
}?>
Any help is greatly appreciated. I have my MAMP server on and I get the "thank you!" message every time, but I haven't gotten any emails. Also, I'd really like it to show the "thank you!" and then return to my webpage, but I can't even get the php to work so I maybe should hold off on that part.
You have a wrong call of mail() function.
It should be like this:
bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
So, for your case:
<?php
if ($_POST['Submit'])
{
$name=$_POST['name'];
$email=$_POST['email'];
$subject=$_POST['subject'];
$comments=$_POST['comments'];
$to='email#email.com';
$from='website';
$message = 'Message: ' . $comments . "\r\n";
$message .= 'From: ' . $from;
$headers = 'From: ' . $email . "\r\n" . 'Reply-To: ' . $email . "\r\n";
if (mail($to, $subject, $message, $headers))
{
echo 'Thank you!';
}
else
{
echo 'Something went wrong! Try again';
}
}
?>
The mail() function simply returns true if the message has been queued without error in your system... but it doesn't tell you whether the email was actually sent or not. I see you're using Mac OS X, so you'll have to configure SMTP settings (provided by your ISP) in the PHP environment to get mail sending working.
Alternately, you can use a service like Amazon SES to send emails through PHP.

Mail is not coming properly using php?

I am using below simple code to send mail using php. when i echo the line $headers = "From: . $email\r\n"; //echo $headers; exit; which results like From: . test#email.com.
I found dot is printing before mail address , so mail is coming but not properly.If i remove dot , mail is not even coming.Whats wrong with this code ?
<?php
if (isset($_REQUEST['user_email']))
//if "email" is filled out, send email
{
//send email
$email = $_REQUEST['user_email'];
$user_name = $_REQUEST['user_name'];
$user_message = $_REQUEST['user_message'];
include 'contact_mail_form.php';
$to = "testtest#gmail.com";
$message = $contactText;
$subject = "Test Mail";
$headers = "From: . $email\r\n"; //echo $headers; exit;
$headers .= "Content-type: text/html\r\n";
mail($to,$subject,$message,$headers);
echo "Thank you for using our mail form";
}
else
//if "email" is not filled out, display the form
{
?>
<form id="contact_form" name="ContactForm" action="" method="post" onsubmit="return checkContact(this);">
<div class="wrapper">
<div class="fright">
<div class="name">Message:</div>
<textarea cols="1" rows="1" name="user_message" id="user_message" ></textarea>
<span class="clear"></span> </div>
<div class="fleft">
<div>
<div class="name">Your Name:</div>
<input type="text" class="input" name="user_name" id="user_name" />
<span class="clear"></span> </div>
<div>
<div class="name">E-mail:</div>
<input type="text" class="input" name="user_email" id="user_email" />
<span class="clear"></span> </div>
</div>
</div>
<div class="buttons"><a class="more" href="javaScript:void(0);" onClick="document.getElementById('contact_form').reset()">clear</a> <input class="more1" type="SUBMIT" class="button2" value="Send" name="" /> </div>
</form>
<?php } ?>
Try it like this your dot is acting as string not as concatenation
$headers = "From:" . $email."\r\n";
Maybe because \r\n should be surrounded with " not '
And Also
$headers .= 'Content-type: text/html'. "\r\n";

Categories