php simple contact form not sending mail even after confirmation - php

I have been trying to get a php contact form working on my portfolio site (currently on a free megabyet.net account), but on testing it(on the uploaded site) even though i get the thankyou/confirmation message, I still don't receive any message on my mail account (specified in the code), I can't seem to understand the problem here....help needed!
can it be something related to SMTP??
Here's the code :
<?php
if(isset($_POST['submit'])) {
$to = "vishu_unlocker#yahoo.com";
$subject = "Portfolio Contact";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$headers = "From: $email_field";
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
echo "Mail has been sent, thankyou!";
mail($to, $subject, $body, $headers);
} else {
echo "blarg!";
}
?>
HTML Code:
<form id="contact_frm" action="mail.php" method="POST">
<h4>Name :</h4>
<input type="text" id="f_name" name="name"/><br/><br/>
<h4>E-Mail Address :</h4>
<input type="text" id="f_email" name="email"/><br/><br/>
<h4>Message :</h4>
<textarea id="f_msg" name="message" cols="22" rows="5"/></textarea><br/><br/>
<input id="send_btn" type="submit" value="Send >>" name="submit" /><br/>
</form>

Firstly you should be checking if mail() returns true or not to determine if mail has been sent successfully:
<?php
if(isset($_POST['submit'])) {
$to = "vishu_unlocker#yahoo.com";
$subject = "Portfolio Contact";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$headers = "From: $email_field";
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
$success = mail($to, $subject, $body, $headers);
if ($success) {
echo "Mail has been sent, thankyou!";
// redirect to thank you page here
}
else {
echo "message failed";
}
} else {
echo "blarg!";
}
?>
Try that and let us know if that works.
Also, have you tried sending to a different email address? It may be that Yahoo is blocking that web host for spam. Being a free host it is a very likely scenario.

If you are looking for something related to sending email via SMTP. I would recommend you use Code Igniters mailer class.
http://codeigniter.com/user_guide/libraries/email.html
This also allows for debugging and handling SMTP errors gracefully.

can it be something related to SMTP??
Probably. Why don't you check your mailq and the log files from your MTA?

#John .. checked with that if condition with the code below and i get a failed output =/ ...so my mail() function is returning false =( ...and yea i've tried gmail but with the mail function not running fine on the first place.... it doesn't work...
<?php
if(isset($_POST['submit'])) {
$to = "vishu_unlocker#yahoo.com";
$subject = "Portfolio Contact";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$headers = "From: $email_field";
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
$success = mail($to, $subject, $body, $headers);
if($success) {
echo "Mail has been sent, thankyou!";
} else {
echo "message sending failed!";
}
} else {
echo "blarg!";
}
?>
output- message sending failed!
so, do I need to define some extra params here?...also i saw that my host has given the path to sendmail as -- /usr/sbin/sendmail does it has anything to do with my mail function acting bad?...i mean do I need to define the sendmail param in it?
#unknown- hmm codeigniter may help, but i've never used it before...let's see...
#symcbean- sorry i don't know how to do that :P...probabaly cuz i'm not very well versed with SMTP yet?.... still a learner/beginner...

If the E-Mail goes out correctly, but never arrives, it could be that it gets caught by a spam filter. A few bullet points I wrote in reply to an similar question a few months ago:
Does the sender address ("From") belong to a domain on your server? If not, make it so.
Is your server on a blacklist (e.g. check IP on spamhaus.org)? This is a remote possibility with shared hosting.
Are mails filtered by a spam filter? Open an account with a freemailer that has a spam folder and find out. Also, try sending mail to an address without a spam filter.
Do you possibly need the fifth parameter "-f" of mail() to add a sender address? (See mail() command in the PHP manual)
If you have access to log files, check those, of course, as suggested above.
Do you check the "from:" address for possible bounce mails ("Returned to sender")? You can also set up a separate "errors-to" address.

Related

How to send mail from bootstrap form on completition and submission to a mail address?

I have created a bootstrap form .. lets say it is for a survey and it generally has a lot of yes no questions . with radio buttons and some text fields for the name,time, date and remarks.. here i want to send the completely filled form to the mail address of admin .. what should be done i am using bootstrap for this..
I tried this but it is not much applicable to me .. it sends the mail but all the forms are not sent by it.. please help..
<?php $name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "emailaddress#here.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>
Use ini_set to change smtp, port, Authorization details, sendmailfrom etc in this script and then use mail function. Hope it will help.
Ex:-
ini_set("SMTP", "smtp.google.com");

My PHP form wont send me an email

I have a rather simple HTML/PHP form that just needs to send the data to my email. The email I'm using is not using the same domain as the website.
I've been stuck on this for hours now and I cant seem to find the solution. Could someone take a look?
<?php
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$message = $_POST['message'];
$gender = $_POST['gender'];
$user_name = $_POST['user_name'];
$email = $_POST['email"];
$message = $_POST['message'];
$from = $_POST['user_name'];
$to = 'example#email.com';
$subject = 'Comment';
$body = "From: $first_name\n From: $last_name\n Sex: $gender\n Username: $user_name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
echo 'Your message has been sent!';
} else {
echo 'Something went wrong, go back and try again!';
}
}
?>
Assuming that your PHP code file is sendMail.php
let this be form.html
<form name="sendMail" id="sendMail" action="sendMail.php" method="post">
<input type="text" name="first_name" />
<input type="text" name="last_name" />
<input type="text" name="message" />
<input type="text" name="gender" />
<input type="text" name="user_name" />
<input type="text" name="email" />
<input type="submit" name="submit" />
</form>
Basically, your PHP code should work according to the form above. Of course you may want to change gender field to radiogroup/dropdown and/or message to textarea.
<?php
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$message = $_POST['message'];
$gender = $_POST['gender'];
$user_name = $_POST['user_name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['user_name'];
$to = 'example#email.com';
$subject = 'Comment';
$body = "From: $first_name\n From: $last_name\n Sex: $gender\n Username: $user_name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
echo 'Your message has been sent!';
}
else {
echo 'Something went wrong, go back and try again!';
}
}
?>
There is one point to correct that $email = $_POST['email"]; should be $email = $_POST['email'];.
And you should ensure that PHP mail settings have to be set properly. I suggest you to use PHPMailer which is so simple and runs smoothly with too few configuration.
Just write isset() in first if condition.
try above code again.
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$message = $_POST['message'];
$gender = $_POST['gender'];
$user_name = $_POST['user_name'];
$email = $_POST['email"];
$message = $_POST['message'];
$from = $_POST['user_name'];
$to = 'example#email.com';
$subject = 'Comment';
$body = "From: $first_name\n From: $last_name\n Sex: $gender\n Username: $user_name\n E-Mail: $email\n Message:\n $message";
if (isset($_POST['submit'])) {
if (mail ($to, $subject, $body, $from)) {
echo 'Your message has been sent!';
} else {
echo 'Something went wrong, go back and try again!';
}
}
The PHP
The fourth parameter of the mail() function should be a list of additional email headers. You're passing it a user name. You should have something like this:
$email = $_POST['email'];
$email = preg_replace('/[[:cntrl:]]/', '', $email);
$headers = "From: $email\r\n";
...
mail ($to, $subject, $body, $headers);
The second line filters out control characters from the email address. Without this, a malicious user could insert newline characters to add their own email headers, such as CC: headers to send unsolicited spam.
Mixing domains
Also, you might not be allowed to send email with a From: address from a different domain name than your web site (or mail server). You could contact the administrator of your server/web site and ask what your options are.
Some things to consider:
You could try to use a local From: address and leave the actual (external) email address in the Reply-To: header. Eg:
$headers = "From: Me#mydomain.example\r\n";
$headers .= "Reply-To: Someone#otherdomain.example\r\n";
Some email clients might not respect the Reply-To: header, though.
If the address in the From: header is not the actual sender of the email, you should specify the real sender in a Sender: header. Eg:
$headers = "From: Someone#otherdomain.example\r\n";
$headers .= "Sender: Me#mydomain.example\r\n";
You may need to specify the From: address in an additional parameter to the Mail Transfer Agent:
mail ($to, $subject, $body, $headers, "-f '$email'");
Sending email from a different domain name may count against you by SPAM filters. You may need an SPF record to ensure that your emails go through.
You might be better off using a full-featured email class, such as PHPMailer rather than the crude mail() function.
See also
Documentation
RFC 5322 - Internet Message Format: 3.6.2. Originator Fields
The PHP mail() function
SPF - Sender Policy Framework
Stack Overflow
Should I use the Reply-To header when sending emails as a service to others?
How do you make sure email you send programmatically is not automatically marked as spam?
Potential issues using member's "from" address and the "sender" header

PHP Mail script problems

This is the first time I've used PHP at all and I'm having trouble implementing a mail form of all things, I can't seem to get this working. Below is my code, I'd be most appreciative if somebody could point me in the right direction in terms of debugging.
<?php
$job_number = $_POST['job_number'];
$completion_time = $_POST['completion_time'];
$email = $_POST['email'];
$formcontent = "From: $email \n \n Job Number: $job_number \n \n Completion Time: $completion_time \n";
$recipient = "data#rak.co.uk";
$subject = "Repeat Order";
$mailheader = "From: $email \r\n";
mail(
$recipient,
$subject,
$formcontent,
$mailheader
)
or die(
"
Error!
"
);
echo( "
<div style='font-size:24px; margin-top: 100px; text-align: center;'>
Thank You!
"
. " - " .
" <a href='home.html' style='color: #1ca03e;'>
Return Home
</a>
</div>
"
);
?>
Thank you,
Cameron
edit: Some more info, the server supports PHP mail scripts as it had one on there before (according to the friend I'm building this for), the error I've had while internal testing is that the mail is being sent but without any of the '$formcontent' content... Only the titles (aka: From:, Job Number:, Completion Time:)
edit edit: if it helps here is a staging server I have it up on at the moment (don't hate me for poor web-design... it's a work in progress) http://temp.fullaf.com/cameron/rak/repeat.html
You can get the swiftmailer package on their site here -> http://swiftmailer.org/
require_once 'swiftmailer/lib/swift_required.php';
function new_mail($subject, $content, $recipients, $from)
{
// Create the message
$message = Swift_Message::newInstance();
// Give the message a subject
$message->setSubject($subject);
// Set the From address with an associative array
$message->setFrom($from);
// Set the To addresses with an associative array
$message->setTo($recipients);
// Give it a body
$message->setBody($content);
$transport = Swift_MailTransport::newInstance();
$mailer = Swift_Mailer::newInstance($transport);
$result = $mailer->send($message);
}
$job_number = $_POST['job_number'];
$completion_time = $_POST['completion_time'];
$email = $_POST['email'];
$message = "From: $email \n \n Job Number: $job_number \n \n Completion Time: $completion_time \n";
$recipients = array('data#rak.co.uk' => 'Your name of choice');
$subject = "Repeat Order";
$from = array($email => 'Name of choice.');
new_mail($subject, $message, $recipients, $from);
I'm currently not in the position where I can access a ftp server to test this specific snippet but try it out. If there are any problems let me know.
Your code is working and sending email without any issues.
Check your email mail Spam box. Some times, the email will go to Spam box.
You are using this "data#rak.co.uk" email address as recipient email. So don't use the same email address for sender email address. Use another email address as sender email.
Make sure, Your email address "data#rak.co.uk" is receiving emails properly, when send email from other email accounts such as yahoo and gmail.
Please make sure, you have setup the mail server properly in your server.

ajax php mail function not working for hotmail addresses

I am having a problem with my contact form and the php mail() function. For some reason, they work for every email address (#gmail, #yahoo, #outlook and even #facebook!) except the old dreaded hotmail. I am just curious as to where my code is missing something. I have checked the mail servers and there is apparently no issue with hotmail addresses.
The email does not even get delivered to the spam/junk folder (it does not reach hotmail). I had a look online and some say to change the headers to avoid being caught in the spam filter. Any pointers to this?
PHP CODE
<?php
header('Content-Type: application/json charset=utf-8');
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'companyname#mail.com';
$to = 'myemail#hotmail.com';
$subject = $name . ' has sent you a message';
$human = $_POST['antispam'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if (isset($_POST['name']) && $human == '4') {
if (mail ($to, $subject, $body, $from)) {
echo '{"status":"1"}';
} else {
echo '{"status":"0"}';
}
}
else
{
echo '{"status":"2"}';
}
?>
The if statements is just a check if all forms are valid and the anti spam (2+2) is correctly entered. nothing much to do in this part. The issue I guess is somewhere in the header
Try using these changes:
<?php
header('Content-Type: application/json; charset=utf-8');
...
$from = 'companyname#mail.com';
$headers = 'From: '. $from. "\r\n";
...
if (mail ($to, $subject, $body, $headers)) {
...
?>
4th parameter of mail function is expected to be additional_headers, not just from address.

php sending email

mailer.php
<?php
if(isset($_POST['submit'])) {
$to = "abc#gmail.com";
$subject = "Contact via website";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
echo "1";
echo "Data has been submitted to $to!";
mail($to, $subject, $body);
} else {
echo "0";
}
?>
jquery code
$('.submit').click(function(){
$('span.msg').css({'visibility': 'visible'}).text('Sending...');
$.post("mailer.php", $(".contactPage").serialize(),
function(data){
$('span.msg').text((data == "1") ? 'Your Message has been received. Thank you' : "Could not send your message at this time").show();
});
return false;
});
I am always getting
Could not send your message at this time
I have almost no knowledge in php, whats i am doing wrong?
Your php script never prints just 1 it prints 0 or 1Data has been submitted to... thus (data == "1") can never be true.
btw: Your script could at least use the return value of mail() to decide whether it signals success or failure.
First of all, check server's mail settings. If your script resides on a server, which you don't administer, the simplest way is to do this is creating a simple PHP file with the following content:
<?php
mail("youraccount#example.com","test","works"); <br>
?>
Run it and check your e-mail.

Categories