Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-15-2012, 02:28 PM   PM User | #1
Joycie
New Coder

 
Join Date: Dec 2012
Location: Spain
Posts: 19
Thanks: 14
Thanked 1 Time in 1 Post
Joycie is an unknown quantity at this point
Question PHP Contact Form issue - please help!

Hi there,

I adjusted a contact form which I downloaded but it keeps giving me an error and does not seem to be sending from my remote server but trying to find a 3rd party server - is there anyone who can help me find the error, please?

Also, I need to add a tickbox to send copy to sender - any suggestions?

Error received:
Warning: mail() [function.mail]: SMTP server response: 451 See http://pobox.com/~djb/docs/smtplf.html. in D:\hosting\10168326\html\contact-Form.php on line 51

PHP coding:
<?php

// Set email variables
$email_to = 'name@mail.net';
$email_subject = 'Web Feedback';


// Set required fields
$required_fields = array('fullname','email','comment');

// set error messages
$error_messages = array(
'fullname' => 'Please enter a Name to proceed.',
'email' => 'Please enter a valid Email Address to continue.',
'comment' => 'Please enter your Message to continue.'
);

// Set form status
$form_complete = FALSE;

// configure validation array
$validation = array();

// check form submittal
if(!empty($_POST)) {
// Sanitise POST array
foreach($_POST as $key => $value) $_POST[$key] = remove_email_injection(trim($value));

// Loop into required fields and make sure they match our needs
foreach($required_fields as $field) {
// the field has been submitted?
if(!array_key_exists($field, $_POST)) array_push($validation, $field);

// check there is information in the field?
if($_POST[$field] == '') array_push($validation, $field);

// validate the email address supplied
if($field == 'email') if(!validate_email_address($_POST[$field])) array_push($validation, $field);
}

// basic validation result
if(count($validation) == 0) {
// Prepare our content string
$email_content = 'New Website Comment: ' . "\n\n";

// simple email content
foreach($_POST as $key => $value) {
if($key != 'submit') $email_content .= $key . ': ' . $value . "\n";
}

// if validation passed ok then send the email
mail($email_to, $email_subject, $email_content);

// Update form switch
$form_complete = TRUE;
}
}

function validate_email_address($email = FALSE) {
return (preg_match('/^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/i', $email))? TRUE : FALSE;
}

function remove_email_injection($field = FALSE) {
return (str_replace(array("\r", "\n", "%0a", "%0d", "Content-Type:", "bcc:","to:","cc:"), '', $field));
}

?>
Joycie is offline   Reply With Quote
Old 12-15-2012, 10:57 PM   PM User | #2
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
The error is in how you have SMTP configured to work with your PHP install. Fix that and your mail() function will work.
__________________
Fumigator is offline   Reply With Quote
Users who have thanked Fumigator for this post:
Joycie (12-17-2012)
Old 12-15-2012, 11:10 PM   PM User | #3
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Also
Quote:
Note:

When sending mail, the mail must contain a From header. This can be set with the additional_headers parameter, or a default can be set in php.ini.
Have you configured the From header in php.ini?
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Users who have thanked AndrewGSW for this post:
Joycie (12-17-2012)
Old 12-15-2012, 11:13 PM   PM User | #4
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,504
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Alternatively you can set that in the 4th parameter of the mail() function call. If that doesn't work, the 5th parameter can force it.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Users who have thanked tangoforce for this post:
Joycie (12-17-2012)
Old 12-16-2012, 09:36 PM   PM User | #5
Joycie
New Coder

 
Join Date: Dec 2012
Location: Spain
Posts: 19
Thanks: 14
Thanked 1 Time in 1 Post
Joycie is an unknown quantity at this point
Question

Quote:
Originally Posted by Fumigator View Post
The error is in how you have SMTP configured to work with your PHP install. Fix that and your mail() function will work.
hi Fumigator, I had a look for the php.ini file - are we talking about the c/xampp/php/php.ini file? sorry, I'm rather new to this...
Joycie is offline   Reply With Quote
Old 12-16-2012, 09:52 PM   PM User | #6
Joycie
New Coder

 
Join Date: Dec 2012
Location: Spain
Posts: 19
Thanks: 14
Thanked 1 Time in 1 Post
Joycie is an unknown quantity at this point
Question

Quote:
Originally Posted by AndrewGSW
Have you configured the From header in php.ini?
Hi AndrewGSW, had a look at the php.ini but not sure what to change...frankly, a little scared to change anything without being sure what to change! Feeling slightly retarded here, but hey ho, gotto start somewhere as a beginner! Could you give me a hint of what to change within the php.ini and in what format to save the file? I'm with GoDaddy if that is of any help ;-) x

Last edited by Joycie; 12-16-2012 at 09:57 PM..
Joycie is offline   Reply With Quote
Old 12-17-2012, 01:29 AM   PM User | #7
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Quote:
hi Fumigator, I had a look for the php.ini file - are we talking about the c/xampp/php/php.ini file? sorry, I'm rather new to this...
This is your local .ini file so is only relevant for testing. Your host may have their own version of this file although it is not always accessible to you directly.

If you are not sending emails from a number of pages then you could just add 'From: ' to the mail() call in your current page. The docs. (Setting From in the php.ini file sets it as a default when sending emails.) Make sure that the additional header(s) you add end with '\r\n'. Example:
PHP Code:
<?php
$to      
'nobody@example.com';
$subject 'the subject';
$message 'hello';
$headers 'From: webmaster@example.com' "\r\n" .
    
'Reply-To: webmaster@example.com' "\r\n" .
    
'X-Mailer: PHP/' phpversion();

mail($to$subject$message$headers);
?>
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Users who have thanked AndrewGSW for this post:
Joycie (12-17-2012)
Old 12-17-2012, 07:00 AM   PM User | #8
Joycie
New Coder

 
Join Date: Dec 2012
Location: Spain
Posts: 19
Thanks: 14
Thanked 1 Time in 1 Post
Joycie is an unknown quantity at this point
Question

Quote:
Originally Posted by AndrewGSW View Post
If you are not sending emails from a number of pages then you could just add 'From: ' to the mail() call in your current page.
Hi Andrew, thank you so much for giving me some directions - however, the issue is still not solved. I have tried different changes in the script but none of them seem to work...I'm close to just redoing the whole page, how frustrating is this!!!

GoDaddy support advices me: "If you use the mail() function in your PHP, you do not need to specify an outgoing mail server. If you are using some other method besides mail() in your PHP code, use relay-hosting.secureserver.net for your relay server."

Although I am using mail() is there still a way of adding the relay server to the headers section, and maybe give that a go...or is there no point trying
Joycie is offline   Reply With Quote
Old 12-17-2012, 12:31 PM   PM User | #9
alemcherry
New Coder

 
Join Date: Apr 2010
Posts: 55
Thanks: 0
Thanked 4 Times in 4 Posts
alemcherry is an unknown quantity at this point
I think you can specify the smtp server like:
ini_set ( "SMTP", "relay-hosting.secureserver.net" ); but that doesn't see like the problem. 451 is internal SMTP server error. Something similar is discussed here.

http://support.jodohost.com/threads/...l-in-e-a.8774/

Replace all \n with \r\n in body and you should be okay.
__________________
Hosting Reviews and Discounts: Bluehost Coupon and Hostmonster Coupon
alemcherry is offline   Reply With Quote
Users who have thanked alemcherry for this post:
Joycie (12-17-2012)
Old 12-17-2012, 03:01 PM   PM User | #10
Joycie
New Coder

 
Join Date: Dec 2012
Location: Spain
Posts: 19
Thanks: 14
Thanked 1 Time in 1 Post
Joycie is an unknown quantity at this point
Thumbs up Big YEAH!!!

Quote:
Originally Posted by alemcherry View Post
I think you can specify the smtp server like:
ini_set ( "SMTP", "relay-hosting.secureserver.net" ); but that doesn't see like the problem. 451 is internal SMTP server error. Something similar is discussed here.

http://support.jodohost.com/threads/...l-in-e-a.8774/

Replace all \n with \r\n in body and you should be okay.
Wooohoooo you superstar!!! It is doing the job Big YEAH for those who have helped me with this!!!! If I can place a link to any of your sites on my link page, let me know

One more lil issue...my 'copy to yourself' is not working yet...any suggestions?

// Set email variables
$email_to = 'info@mail.net';
$email_subject = 'Feedback';
ini_set("SMTP","relay-hosting.secureserver.net");
ini_set("sendmail_from","info@mail.net");
$ccto = 'email';

// simple email content
foreach($_POST as $key => $value) {
if($key != 'submit') $email_content .= $key . ': ' . $value . "\r\n";
}

$ccto .= $_POST['Copy to yourself'] == "tick" ? "Copy to yourself: Yes \n" : "Copy to yourself: No \n";

// if validation passed ok then send the email
mail($email_to, $email_subject, $email_content, $sendmail_from, $ccto);
Joycie is offline   Reply With Quote
Old 12-17-2012, 03:43 PM   PM User | #11
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,648
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
This isn't right: mail($email_to, $email_subject, $email_content, $sendmail_from, $ccto); . Parameters here are to, subject, content, headers, and finally parameters. $sendmail_from may be a parameter, but if you have it working as is that indicates that its simply a header.
If you want to send a copy to yourself, $ccto is abandoned and $sendmail_from can add an additional header for 'cc:' ie: $sendmail_from .= "Cc: <an address>\r\n".
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
Joycie (12-17-2012)
Old 12-17-2012, 07:14 PM   PM User | #12
Joycie
New Coder

 
Join Date: Dec 2012
Location: Spain
Posts: 19
Thanks: 14
Thanked 1 Time in 1 Post
Joycie is an unknown quantity at this point
Question

Quote:
Originally Posted by Fou-Lu View Post
If you want to send a copy to yourself, $ccto is abandoned and $sendmail_from can add an additional header for 'cc:' ie: $sendmail_from .= "Cc: <an address>\r\n".
Thank you Fou-Lu, I have now changed the header code!
Though regarding the CC, it won't be a CC to me but to the actual sender of the message.

I have been searching of how to CC the message as an attachment to prevent mail spamming...any suggestions?
Joycie is offline   Reply With Quote
Old 12-17-2012, 09:01 PM   PM User | #13
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,648
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
You can use whatever address you want in the cc. You can also specify multiple addresses in the to header by separating them with a comma. Presumably you have a method of gathering the email address from the user (which may or may not be a real email address / belong to the original author).

I don't understand what you mean by sending the message as an attachment. Do you mean by creating a text file that they can download as an attachment from the email? Seems a little over the top to me (and would solve nothing of course); you'll need to come up with a more vigorous spam identification approach to completely discard what is identified as spam or queue it for approval.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
Joycie (12-17-2012)
Old 12-17-2012, 09:04 PM   PM User | #14
Joycie
New Coder

 
Join Date: Dec 2012
Location: Spain
Posts: 19
Thanks: 14
Thanked 1 Time in 1 Post
Joycie is an unknown quantity at this point
Hi Fou-Lu!

I've just been reading a lot but lacking the experience, therefore I'm asking around - your opinion is well appreciated!!!

As the email will be a Cc to the actual sender of the message, I don't know the Cc email direction in advance...Any suggestions of how to set the // Set email variables & // simple email content for a checkbox to attach a copy of the message to the actual sender without knowing their email?
Joycie is offline   Reply With Quote
Old 12-17-2012, 09:10 PM   PM User | #15
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,648
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
No, that's impossible. The email address must be provided to you in order to determine where to send it. You can use checkboxes to determine if it should send a copy, but the email address has to come from somewhere.
If you have a system where the user logs into, chances are they provided an email during registration. You can always retrieve that and use it.

Client side you can do this though (sorta). Using the mailto: link in HTML will open the client's standard email client. You can provide it with information within the link (limited to about 65K characters including the encoded entities) as a part of the querystring (see this wiki article for some examples: http://en.wikipedia.org/wiki/Mailto), but you won't be able to preprocess the data sent by the form. Best you can do is process the form to generate the link, and let them click on the link.
This way they send it using their mail and not yours.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
Joycie (12-17-2012)
Reply

Bookmarks

Tags
contact form, error, php, relay server, script

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:56 PM.


Advertisement
Log in to turn off these ads.