500 error after PHP mail() - php

UPDATE
I was able to get the hosting info from my client and I contacted support, apparently there's an issue with the hosts mail function at the moment and they are working on a resolution. Will wait to see if that's the cause of this problem and will report back.
END UPDATE
I am trying to set up a simple contact form that will send an email. I have the form action set to the below PHP file.
The email gets sent, but the user experience ends with a 500 error instead of sending the user to the confirmation page.
If I comment out the mail() part, then the form redirects the user to the confirmation page successfully, but of course no email gets sent.
The website is hosted on GoDaddy, and I don't have access to the hosting account, though I can try to get it if I need it.
Here's the PHP code:
<?php
$name = $_POST['name'];
$address = $_POST['address'];
$city = $_POST['CITY'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$email = $_POST['email'];
$howdidyouhear = $_POST['hear_about'];
$notifyshow = $_POST['notify_shows'];
$notifyonline = $_POST['notify_online'];
$interest_jewelry = $_POST['Interest_jewelry'];
$interest_prints = $_POST['interest_prints'];
$interest_folkart = $_POST['interest_folkart'];
$interest_indian = $_POST['interest_indian'];
$interest_closeouts = $_POST['interest_closeouts'];
$interest_other = $_POST['interest_other'];
$interest_other_text = $_POST['interest_other_text'];
$spamvalid = $_POST['validate'];
$honeypot = $_POST['website'];
//Spammer Handling
if ($honeypot!=null){echo 'You have been flagged as a spammer, please go away!'; exit;}
if ($spamvalid != '357'){
echo "
<script>
function goBack() {
window.history.back()
}
</script>
You didn't enter the correct number at the bottom of the form. Please try again.<br><button onclick='goBack()'>Go Back</button>";
exit;
}
//START EMAIL
//Body
$mailbody="Name: {$name}\n\nAddress: {$address}\n\nCity: {$city}\n\nState: {$state}\n\nZip: {$zip}\n\nEmail: {$email}\n\nHow did you hear about us?: {$howdidyouhear}\n\nWould you like to be notified when we will be doing a show in your area?: {$notifyshow}\n\nWould you like to receive email notifications of special sales and online events?: {$notifyonline}\n\nWhat brought you to mishuganah.com?: {$interest_jewelry} {$interest_prints} {$interest_folkart} {$interest_indian} {$interest_closeouts} {$interest_other}: {$interest_other_text}\n\n";
//Send Email
mail('matt.rodela#gmail.com','New submission from Mishuganah.com', $mailbody, "From:{$email}\r\n" );
header("Location: http://".$_SERVER["HTTP_HOST"]."/mailing_list/confirmation_page.htm");
?>
I am a relative novice with PHP, so please explain your solutions fully. Thanks!

Use phpMailer instead of php mail() function below you will find reasons not to use built in php mail function
In some cases, mails send via PHP mail() did not receive the recipients although it was send by WB without any error message. The most common reasons for that issue are listed below.
wrong format of mail header or content (e.g. differences in line break between Windows/Unix)
sendmail not installed or configured on your server (php.ini)
the mail provider of the recipeint does not allow mails send by PHP mail(); common spam protection
Errors in the format of header or content can cause that mails are treated as SPAM. In the best case, such mails are transfered to the spam folder of your recipient inbox or send back to the sender. In the worst case, such mails are deleted without any comment. If sendmail is not installed or not configured, no mails can be send at all.
It is common practice by free mail provider such as GMX, to reject mails send via the PHP function mail(). Very often such mails are deleted without any information of the recipient.

So it turns out it was an issue on GoDaddy's end and it has been resolved. The form is working now. Apparently there was nothing wrong with the code.
Thanks for the suggestions folks, I learned some stuff (going to sanatize and filter my inputs now).

Related

how to send email in server using core php?

I have written a email send php code and uploaded onto server. It is working and email is being send to the inbox. But when i uploaded the same code onto another server, it is not working, mail is not being send but the message "your message has been send successfully" is being displayed. why is it like that ? Can anyone tell ?
There are a lot of shared webhosting providers who have disabled PHP's mail() function.
WHat you could do is switch to, for example: PHPMailer
Without seeing you code i'ts not possible to tell you why it isn't working.
You say your getting the message "your message has been send successfully" but that isn't the response from the send() command - that should be a boolean - so check that your code is reading that correctly and reporiting the correct result.
If the send() is returning true then it means your message has left the server but doesn't mean it's arrived at it's destination. Most likely the recipients server is blocking your new server for some reason.
My suggestion is to use an external SMTP server to send the mail through so it's consistent across where ever your app is hosted. Amazon SES is very good, cheap and allows you to easily monitor bounces too.
This is my code for phpgmailer. This is showing the above error.
<?php
$name = $_POST['fname'];
$telephone = $_POST['phone'];
$mobile = $_POST['mobile'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$msg = $_POST['message'];
require_once("phpgmailer/class.phpgmailer.php");
$mail=new PHPGMailer();
$mail->Username="cd4xltech";
$mail->Password="dfddfdfddf";
$mail->From="cd4xltech#gmail.com";
$mail->FromName=$name;
$mail->Subject="Feedback:".$subject;
$mail->AddAddress("annan#gmail.com");
$mail->Body=$message."From: $name";
if(!$mail->Send())
{
echo "Error".$mail->ErrorInfo;
}
else
{
echo "Mail Sent";
}
?>

Can't embed variable in mail function php

I want to send email with different subjects. I have the following code that get mail subject and message from the database.
$sql = "SELECT mail_subject, mail_content FROM new_business WHERE business_name = '$biz_name'";
$query = mysqli_query($db, $sql);
while ($row = mysqli_fetch_array($query)) {
$subject = $row['mail_subject']; //Getting mail subject to send
$mail_content = $row['mail_content']; //Getting mail content
}
echo $subject; //checking if it gets subject and it surely does.
$to = 'utopian51#gmail.com';
mail($to, $subject, $mail_content);//Mail is not sent
If subject pulls data from database mail is not sent. I have checked several times including spam and also checked if subject gets any data from database before embedding it in mail function. It seems it gets the right data but the mail is not sent.
But when i hardcode the subject like this: $subject = 'this is a test subject'; , the mail is sent successfully. Why mail function does not let me embed variable that gets subject from database? I need to send the mails with different subject from database. Whats the solution?
There's many things which are confusing about the code you've written. You never check for error conditions in the mysqli_ calls nor the mail() call. You run a query which will discard all but the last row (if any). However the way to diagnose issues with mail() is not to watch your inbox. In more than 99% of cases the problem is with the interface between PHP and the MTA or with the MTA configuration itself.
You have provided no details of either.
I would recommend you use simply:
mail('utopian51#gmail.com', 'test', date('r'));
and start debugging your MTA.
Independently you can work on your bulk emailing script, simply writing to a file instead of calling mail() until both parts are working.

having a php page send an email

This question should have a simple, simple answer, but I just can't seem to get it working. Here's the scenario:
I created a php page -> this one: http://adianl.ca/pages/member_application.php. Once the form is completed, it proceeds to http://adianl.ca/pages/member_application_action.php, puts the data into a MySQL db, & thanks the user for their interest. Anyway, the form works perfectly, except for one little thing: whenever someone fills out that form, I want an email to be sent to sbeattie#adianl.ca, informing them that the form was filled out, & the email would include the form components. The problem is, I can NOT get an email to be sent to that address, or any address truth be told. Having a php page send an email should be a simple thing to do, but it's really baffling me.
Can anyone help me with this? This particular problem has been troubling me since yesterday, & if anyone can help me with this...man, thank you soooooo much.
JP
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.adianl.ca"; // SMTP server $mail->From = "webadmin#adianl.ca";
$mail->FromName = "Web Administration [ADIANL]";
$mail->AddAddress("sbeattie#adianl.ca");
$mail->AddCC("justinwparsons#gmail.com"); the #messageBody variable is just a string
If you want to have the email sent using the server's sendmail client, you can use mail.
If you want it to use another mail server, there are extensions to connect to an SMTP server. I use PHPMailer.
If mail doesn't work, it could be that the server is not set up to send email, or it could be that the mail server is rejecting emails sent from php, amongst other reasons.
this code can also be used to email in php so have a look, you can find many more examples of emailing in php look around
?php
$to = "recipient#example.com";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
if (mail($to, $subject, $body)) {
echo("<p>Message successfully sent!</p>");
} else {
echo("<p>Message delivery failed...</p>");
}

Using PHP mail( )

I have a php mail script which works perfectly on a one host. However, when I attempt to use the same script on a network solutions host, the function returns true but no email ever sends.
//get mail function data
$case = $_POST['case'];
$to = addslashes(strip_tags($_POST['to']));
$message = addslashes(strip_tags($_POST['message']));
$subject = addslashes(strip_tags($_POST['subject']));
$message = addslashes(strip_tags($_POST['message']));
$from = "confirmation#website.co";
$headers = "From: $from\r\n";
//send email
if (mail($to,$subject,$message,$headers)){
//formatting for error message
$emailSent = "block";
$emailFailed = "none";
}
else //if the email fails to send
{
$emailSent = "none";
$emailFailed = "block";
}
?>
Does anyone know if different hosts require specific info in mail script?
This is a question for Network Solutions customer support. Sending mail from shared hosting servers is usually well locked down -- if they allow it at all, it's throttled. Also, calling mail just means the message was successfully passed to sendmail, not that the mail ever left the server. It could be sitting in a queue to be sent, it could have bounced for a million reasons beyond your control, etc.
Some hosts have this issue. On mine (Mosso), I had to adjust the last parameter (from) like so:
mail($to, $subject, $message, $headers, "-f".$from)
May not be the solution for NetSol but worth a try. I know some hosts disable the script and require using the smtp class to send mail.
This is assuming, of course, everything is correct with your DNS and MX records. If you're trying to send from an account that is different than the domain being sent from, some providers will automatically block that.
It maybe that they have not enabled php mail, but ask the host for specifics.
Looking at your code it is not cleaning the input sufficiently. take a look at the is_forbidden function here:
http://thedemosite.co.uk/phpformmailer/source_code_php_form_mailer_more_secure_than_cgi_form_mailers.php

Why is email not being sent through PHP even though it is being reported as sent?

I'm trying to sends mails in PHP. The code I used for sending a mail in CakePHP is given below. I get the message 'Simple Email Sent' in my web page but the mail is not delivered to my inbox. Am I missing something?
The values in the to, subject and link fields are set with the values entered in the user interface.
$this->set('to',$this->params['form']['to']);
$this->set('subject',$this->params['form']['subject']);
$this->set('link',$this->params['form']['link']);
$this->Email->to = to;
$this->Email->subject = subject;
$this->Email->from = 'someperson#somedomain.com';
$this->Email->delivery= 'mail';
$this->Email->sendAs='text';
$this->Email->template = 'simple_message';
//$this->Email->send(link);
if ( $this->Email->send(link) ) {
$this->Session->setFlash('Simple email sent');
} else {
$this->Session->setFlash('Simple email not sent');
}
On a Linux system, you'll probably have a sendmail script installed already, and PHP will use that. If this is what you have and it's not working, then I'd look for mail configuration problems in your Linux system itself.
On a Windows system, you'll need to configure the SMTP server you want PHP to send mail to. The normal way to do this is in php.ini. The instructions for this are here.
Unless you have set Email->delivery this should be the same for CakePHP - it should default to whatever PHP uses.
Note: If you are using your own Linux install, it could just be that your ISP is blocking port 25, which your mail server is using. In that case you'll need to configure linux to route email to your ISP's email server. Maybe this will help?
Since when is 'to' (line 4) a valid destination email address?
You need to use variable syntax for setting to 'to' line, and the 'subject' line. Those lines should read
$this->Email->to = to;
$this->Email->subject = subject;
Also, I believe there is an attribute in the Email component called error (I cannot find it in the documentation currently) that will help you debug. This may not be totally correct; I use the Email component with SMTP, and there is an attribute that gets set by the Email component called smtpError. I believe there is one called error that you can use to check for an error -- it should contain code that will tell you where your problem lies.
In case that's an incorrect statement, you can always do a var_dump( $this->Email ); after you try to send an email. That will dump the entire contents of the object, so you can see if you have set attributes correctly, and it should help you find out what the error attribute is named.

Categories