I have setup a really simple php message form on my website but for some reason it is not working, when the designer showed it to me from his website it worked perfect but now hosted in mine doesnt work. I am using godaddy.
This is the code:
<?php if (isset($_REQUEST['email'])){
$email = $_REQUEST['email'] ;
$message = " Message: ".$_REQUEST['message']." Name : ".$_REQUEST['name']." Phone : ".$_REQUEST['phone'] ;
mail( "myemail#myemail.com", "Customer Service Email", $message, "From: $email" ); echo "<script> alert('Thanks for your message!'); </script>";}?>
I put myemail#mymail.com as representation, I know im supposed to put my own email.
Hmm. My experience with the mail function is that it relies on the underlying system's mail feature(s). Godaddy probably has it blocked. I had this similar problem and ultimately I used a different mail client other than php's native "mail" function.
I dont know why you are using REQUEST, maybe because you use both GET and POST with this send mail file from two different sources but here is a quick upgrade of your actual code + a check that will return an error if your php mail() is not set/turned on in php.ini config file.
<?php
// First we retrieve our values
$email = $_REQUEST['email'];
$message = $_REQUEST['message'];
$name = $_REQUEST['name'];
$phone = $_REQUEST['phone'];
// Here we make sure that the user has filled in the values
if ( empty($email) ){
echo "<script> alert(\"Please make sure that you filled in your email address!\"); </script>\n";
} elseif ( empty($message) ){
echo "<script> alert(\"Please make sure that you entered a message!\"); </script>\n";
} elseif ( empty($name) ){
echo "<script> alert(\"Please make sure that you filled in your name!\"); </script>\n";
} elseif ( empty($phone) ){
echo "<script> alert(\"Please make sure that you filled in your phone number!\"); </script>\n";
} else {
// Here you can validate data inside of each strings...
// Yes he does, Configure email before sending.
$to = "myemail#mymail.com";
$title = "Customer Service Email";
$message = "Message: " . $message . " | Name: " . $name . " | Phone: " . $phone . " | Email: " . $email . "";
// Mail it!
$send = mail( $to, $title, $message);
// Check if it has been sent
if(!$send){
echo "<script> alert('Hoho, your server cannot send email by using mail() function..'); </script>\n";
} else {
// Show prompt to user!
echo "<script> alert('Thanks for your message!'); </script>";
}
}
?>
Related
I want to send the email from the server when someone submits the contact form. Here is the sample php code.
<?php
if($_SERVER['REQUEST_METHOD'] == "POST"){
$name = strip_tags(trim($_POST['name']));
$email = strip_tags(trim($_POST['email']));
$dept = strip_tags(trim($_POST['dept']));
$message = strip_tags(trim($_POST['message']));
if((!isset($email) || empty($email)) || (!isset($name) || empty($name)) || (!isset($dept) || empty($dept)) || (!isset($message) || empty($message))){
echo -1;
return;
}
if(filter_var($email, FILTER_VALIDATE_EMAIL)===false){
echo -2;
return;
}
$to = 'farooqahmadkhan003#gmail.com';
$email_subject = "Splendenti Eye Wear | Contact Us";
$email_body = "Contacted by\nName: $name\nEmail: $email\nDepartment: $dept\nMessage: $message";
$headers = "noreply#farooq.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply#yourdomain.com.
$headers .= "Reply-To: ".$_POST['email'];
if(mail($to,$email_subject,$email_body,$headers)){
echo 1; // email sent
}
else{
echo "failed to send email";
}
}else{
echo "Invalid Request";
}
?>
but the mail function is returning false. Can anybody guide??
I suggest you to use PHPMailer to send emails from your PHP files.
Check this link and download it. There are many documentations about PHPMailer online. You can control all the details about your mail with PHPMailer.
try to use # to suppress warnings:
if(#mail($to,$email_subject,$email_body,$headers)){
echo 1; // email sent
}
If this doesn't help you have a configuration issue.
I have the following code in a PHP file.
$to = $row['EmailID'];
$subject = "XYZ";
$message = "yes!!!!";
$from = "pallav123goyal#gmail.com";
if(!mail($to,$subject,$message,"From:" . $from))
{
echo "Confirmation E-mail couldn't be sent to " . $row['EmailID'] . "<br>";
}
else
{
echo "Confirmation E-mail sent to " . $row['EmailID'] . "<br>";
}
}
On running the code, the else part of the above code is executed as evident from its output. But no e-mail gets sent to $row['EmailID'] (which is a valid email ID) What could be the error?
Try this syntax
<?php
$to =$row['EmailID'];
$subject ="XYZ";
$txt ="yes!!!!";
$headers = "From: pallav123goyal#gmail.com";
mail($to,$subject,$txt,$headers);
?>
It is most likely a problem with your sendmail (assuming you are running the code on a linux system) configuration and not with the code per se. Try to use PEAR mail and specify an external SMTP server (e.g. gmail).
first of all, I am learning PHP and I am at an elementary level, (sorry). I have a script that send me an email once the form it filled in. The form is located on another page, this is the page that does all the magic. For SOME REASON, this code does not work, I do not get an emails at all. As I missing something!
Thank you
<?php
if(!$_POST) exit;
// Verifico email.
function isEmail($email) {
return(preg_match("/^[-_.[:alnum:]]+#((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i",$email));
}
if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$comments = $_POST['comments'];
$verify = $_POST['verify'];
if(trim($name) == '') {
echo '<div class="error_message">Attention! You must enter your name.</div>';
exit();
} else if(trim($email) == '') {
echo '<div class="error_message">Attention! Please enter a valid email address.</div>';
exit();
} else if(!isEmail($email)) {
echo '<div class="error_message">Attention! You have enter an invalid e-mail address, try again.</div>';
exit();
}
if(trim($subject) == '') {
echo '<div class="error_message">Attention! Please enter a subject.</div>';
exit();
} else if(trim($comments) == '') {
echo '<div class="error_message">Attention! Please enter your message.</div>';
exit();
} else if(!isset($verify) || trim($verify) == '') {
echo '<div class="error_message">Attention! Please enter the verification number.</div>';
exit();
} else if(trim($verify) != '4') {
echo '<div class="error_message">Attention! The verification number you entered is incorrect.</div>';
exit();
}
if(get_magic_quotes_gpc()) {
$comments = stripslashes($comments);
}
// Configuracion.
$address = "abc#xyz.com";
$addressrrhh = "abc#xyz.com";
$e_subject = 'You\'ve been contacted by ' . $name . '.';
// Campos del form ampliables.
$e_body = "You have been contacted from JACK contact form by $name with regards to $subject, their additional message is as follows." . PHP_EOL . PHP_EOL;
$e_content = "\"$comments\"" . PHP_EOL . PHP_EOL;
$e_reply = "You can contact $name via email, $email";
$msg = wordwrap( $e_body . $e_content . $e_reply, 70 );
$headers = "From: $email" . PHP_EOL;
$headers = "BCC: $addressrrhh" . PHP_EOL;
$headers .= "Reply-To: $email" . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
$headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;
if(mail($address, $e_subject, $msg, $headers)) {
// Mensaje envio OK
echo "<fieldset>";
echo "<div id='success_page'>";
echo "<h1>Email Sent Successfully.</h1>";
echo "<p>Thank you <strong>$name</strong>, your message has been submitted.</p>";
echo "</div>";
echo "</fieldset>";
} else {
echo 'ERROR!';
}
?>
Please have a look at http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods and http://php.net/manual/de/function.mail.php first
First thing first
Do you have errors when you submit in your webserver error log ?
Is your mail sent ? (Check your mail log)
If so, is it in your spam folder ?
About your mail marked directly as spam
Things "changed" a little bit, and you can't just send an email without setting the "fifth" parameters or your mail will go directly to spam boxes.
From http://php.net/manual/en/function.mail.php
The additional_parameters parameter can be used to pass additional flags as command line options to the program configured to be used when sending mail, as defined by the sendmail_path configuration setting. For example, this can be used to set the envelope sender address when using sendmail with the -f sendmail option.
The user that the webserver runs as should be added as a trusted user to the sendmail configuration to prevent a 'X-Warning' header from being added to the message when the envelope sender (-f) is set using this method. For sendmail users, this file is /etc/mail/trusted-users.
Also, i'm suggestion you the very good phpmailer
Usage exemple : http://phpmailer.worxware.com/index.php?pg=examplebmail
Hope it helps
I think that using SMTP is better than something else. If you encounter problems with the mail() function, maybe you take a look at PEAR: pear.php.net
Post is something completely different. Its not like the dutch word post which means mail :D
$_POST is a variable that stores all values a post form sends. so for example: a form with the method POST has 1 input text field with the name "foo". After you type in: "bar" in that field and submit the field a $_POST array is created where $_POST["foo"] has the value "bar"
Maybe you read something about not using php's on mail() function and better use SMTP? That makes a little bit more sense...
Good luck
I'm trying to include a simple contact form in a WordPress theme I'm coding for someone (they want it functional without using any WP plugins, so I'm simply using PHP).
Here's the code I'm using:
<?php
include "../../../../wp-blog-header.php"; // include WP to be able to use some options
if (of_get_option('ss_contact_email', 'no entry' )) { // custom WP option
$mailto = of_get_option('ss_contact_email', 'no entry' );
} else {
$mailto = get_option('admin_email'); // WP option to get email address of the admin
};
$cc = "";
$bcc = "";
$subject = "[Contact Form] " .$_POST['subject']. "";
$vname = ucwords($_POST['user']);
$email = $_POST['email'];
function validateEmail($email)
{
if(eregi('^[a-zA-Z0-9._-]+#[a-zA-Z0-9-]+\.[a-zA-Z]{2,4}(\.[a-zA-Z]{2,3})?(\.[a-zA-Z]{2,3})?$', $email))
return true;
else
return false;
}
if((strlen($_POST['user']) < 1 ) || (strlen($email) < 1 ) || (strlen($_POST['question']) < 1 ) || validateEmail($email) == FALSE){
$emailerror .= '';
if(strlen($_POST['user']) < 1 ){
$emailerror .= '<span class="wrong">Please enter your name. </span>';
}
if(validateEmail($email) == FALSE || strlen($email) < 1 ) {
$emailerror .= '<span class="wrong">Please enter a valid e-mail address. </span>';
}
if(strlen($_POST['message']) < 1 ){
$emailerror .= '<span class="wrong">Please enter your message. </span>';
}
} else {
$emailerror .= "<span>Your message has been sent. Thank you!</span>";
// NOW SEND THE ENQUIRY
$timestamp = date("F j, Y, g:ia");
$messageproper ="\n\n" .
"Name: " .
ucwords($_POST['user']) .
"\n" .
"Email: " .
$email .
"\n" .
"Website: " .
$_POST['url'] .
"\n" .
"Subject: " .
$_POST['subject'] .
"\n" .
"Comments: " .
"\n" .
$_POST['message'] .
"\n" .
"\n\n" ;
$messageproper = trim(stripslashes($messageproper));
mail($mailto, $subject, $messageproper, "From: \"$vname\" <".$_POST['email'].">\nReply-To: \"".ucwords($_POST['user'])."\" <".$_POST['email'].">\nX-Mailer: PHP/" . phpversion() );
}
?>
<?php echo $emailerror; ?>
Error handling seems fine, it returns all the correct errors but if all fields are correct it goes blank instead of sending and displaying the "Thank you.." message.
I've used the same script on the same server numerous times, and never had any problems with it but this time I stumbled upon an issue - the mail isn't getting send, even though it doesn't return any errors.
I'm not sure what goes wrong. Anyone able to skim through the code and look for some obvious mistakes?
You're using:
strlen($_POST['question']) < 1
as a check in your if statement, but then using 'message' as a replacement. If 'question' is not set it will not try and send, but will also not print an error.
Since the email's not getting sent, most likely PHP is dying at the point of the mail() call. You're not checking the return value from mail(), which is bad form - never assume a call to an external service succeeded. Turn on error reporting, enable display errors, etc... then try the contact form again. check your server logs and see if anything shows up in the browser.
The code otherwise looks fine, so without any more information, this question is most likely unanswerable.
Hey guys,
I know there are a lot better ways to send email with PHP. However, for this purpose the easiest solution is perfect. Only thing: I can't find out why, my validation is not working!
I get the email address, however the validation is not working, I'm able to send a completely empty form. Any ideas?
<?php
//Email
$Name = Trim(stripslashes($_POST['author']));
$EmailFrom = Trim(stripslashes($_POST['email']));
$Subject = Trim(stripslashes($_POST['subject']));
$Comment = Trim(stripslashes($_POST['comment']));
$EmailTo = "myemail#address.com";
/*$Name = "Some Name";
$EmailFrom = "test#test.com";
$Subject = "Test";
$Comment = "Why is the validation not working, don't get it?";
$EmailTo = "myemail#address.com";*/
/*echo $Name . " length: " . strlen($Name) . "<br/>";
echo $EmailFrom . " length: " . strlen($EmailFrom) . "<br/>";
echo $Subject . " length: " . strlen($Subject) . "<br/>";
echo $Comment . " length: " . strlen($Comment) . "<br/>";
echo $EmailTo . " length: " . strlen($EmailTo) . "<br/>";*/
//***************************
//Validation
//***************************
$validationOK=true;
if ($Name == "") $validationOK=false;
if (isValidEmail($EmailFrom) == 0) $validationOK=false;
if ($Subject == "") $validationOK=false;
if ($Comment == "") $validationOK=false;
function isValidEmail( $email = null ) {
return preg_match( "/^[\d\w\/+!=#|$?%{^&}*`'~-][\d\w\/\.+!=#|$?%{^&}*`'~-]*#[A-Z0-9][A-Z0-9.-]{1,61}[A-Z0-9]\.[A-Z]{2,6}$/ix", $email );
}
if (!$validationOK) {
print "error";
}
//***************************
//Order
//***************************
$Body = "Contactform";
$Body .= "\n\n";
$Body .= $Comment;
// Email Headers with UTF-8 encoding
$email_header = "From: " . $EmailFrom . "\r\n";
$email_header .= "Content-Type: text/plain; charset=UTF-8\r\n";
$email_header .= "Reply-To: " . $EmailFrom . " \r\n";
// send email
$success = mail($EmailTo, $Subject, $Body, $email_header);
//***************************
//Success or Error
//***************************
if ($success){
print "success";
}
else{
print "error";
}
?>
you need to return after the condition
if (!$validationOK) {
print "error";
// return or exit
}
dont continue to send mail
You don't end your script after printing "error". You should end the script or make some other change so it won't be sent on error. For example:
if (!$validationOK) {
print "error";
exit;
}
You're printing the word error if $validationOK is false but you're not halting the script at that point so php continues to process the commands after it.
Try the following
if (!$validationOK) {
print "error";
exit();
}
This code:
if (!$validationOK) {
print "error";
}
does not keep your program from sending a mail, it just prints error message.
Don't write your own email validation function when there's tried and tested free software to do it for you.
You are welcome to use my free PHP function is_email() to validate addresses. It's available to download [here][1].
It will ensure that an address is fully RFC 5321 compliant. It can optionally also check whether the domain actually exists and has an MX record.
You shouldn't rely on a validator to tell you whether a user's email address actually exists: some ISPs give out non-compliant addresses to their users, particularly in countries which don't use the Latin alphabet. More in my essay about email validation here: [http://isemail.info/about][2].