sendmail.php failure to send to email address

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mandaclarke
Forum Newbie
Posts: 3
Joined: Mon Sep 13, 2010 3:18 pm

sendmail.php failure to send to email address

Post by mandaclarke »

Please can you help!

I have a sendform.php which should send contact form information to an email address. I have tested using several email addresses and it works perfectly. When I add in the customers address the form does not send the email to the required address.

Is there something that I can do to fix this:

Code: Select all

<?php

if($_POST) {
    if($_POST['firstname'] && $_POST['lastname']
        && $_POST['email'] && $_POST['message'] && $_POST['to']) {
        
        //define the receiver of the email
        $to = $_POST['to'];
        
        //define the subject of the email
        $subject = 'BP Strategies Contact Form';
        
        //define the message to be sent. Each line should be separated with \n
        $message = "First name: ".$_POST['firstname'];
        $message .= "\nLast name: ".$_POST['lastname'];
        $message .= "\nPhone: ".$_POST['phone'];
        $message .= "\nEmail: ".$_POST['email'];
        
        if($_POST['contact']) {
            $message .= "\n\nMessage: ".$_POST['message'];
            
            //define the headers we want passed. Note that they are separated with \r\n
            $headers = "From: registerform@bpstrategies.net\r\nReply-To: registerform@bpstrategies.net";
        } else {
            //define the headers we want passed. Note that they are separated with \r\n
            $headers = "From: contactform@bpstrategies.net\r\nReply-To: contactform@bpstrategies.net";
        }
        
        //send the email
        $mail_sent = @mail( $to, $subject, $message, $headers );
        
        //if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
        echo $mail_sent ? "Mail sent succesfully!" : "Mail failed! Please try again.";
    }
}
?>
Form Code:
<input type="text" name="firstname" value="First Name..." id="firstname" />
<input type="text" name="lastname" value="Last Name..." id="lastname" />
<input type="text" name="phone" value="Phone Number..." id="phone" />
<input type="text" name="email" value="Email Address..." id="email" />
<textarea name="message" style="height: 80px;" id="message" >Your Message...</textarea>
<input type="hidden" name="to" value="sarah@bpstrategies.net" />
<input type="hidden" name="contact" value="true" />
<input type="submit" value="SUBMIT" />
</form>

I would really appreciate some help with this :banghead:

Amanda :D
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: sendmail.php failure to send to email address

Post by social_experiment »

Are you sending this to one email address everytime? Im thinking yes because you have hard-coded the value of the email address into the form. You can place it inside the php instead :

Code: Select all

<?php $to = 'your@mail.address'; ?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
mandaclarke
Forum Newbie
Posts: 3
Joined: Mon Sep 13, 2010 3:18 pm

Re: sendmail.php failure to send to email address

Post by mandaclarke »

OK, that sounds like it would work.... can you help me with what I would need to remove to make that work please?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: sendmail.php failure to send to email address

Post by social_experiment »

Code: Select all

<?php
if($_POST) {
    if($_POST['firstname'] && $_POST['lastname']
        && $_POST['email'] && $_POST['message'] && $_POST['to']) {
        
        //define the receiver of the email
        // your address here [alteration]
        $to = 'your@mail.address';

        //define the subject of the email
        $subject = 'BP Strategies Contact Form';
        
        //define the message to be sent. Each line should be separated with \n
        $message = "First name: ".$_POST['firstname'];
        $message .= "\nLast name: ".$_POST['lastname'];
        $message .= "\nPhone: ".$_POST['phone'];
        $message .= "\nEmail: ".$_POST['email'];
        
        if($_POST['contact']) {
            $message .= "\n\nMessage: ".$_POST['message'];
            
            //define the headers we want passed. Note that they are separated with \r\n
            $headers = "From: <!-- e --><a href=\"mailto:registerform@bpstrategies.net\">registerform@bpstrategies.net</a>
			<!-- e -->\r\nReply-To: <!-- e --><a href=\"mailto:registerform@bpstrategies.net\">registerform@bpstrategies.net</a><!-- e -->";
        } else {
            //define the headers we want passed. Note that they are separated with \r\n
            $headers = "From: <!-- e --><a href=\"mailto:contactform@bpstrategies.net\">contactform@bpstrategies.net</a>
			<!-- e -->\r\nReply-To: <!-- e --><a href=\"mailto:contactform@bpstrategies.net\">contactform@bpstrategies.net</a><!-- e -->";
        }
    
        //send the email
        $mail_sent = @mail( $to, $subject, $message, $headers );
        
        //if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
        echo $mail_sent ? "Mail sent succesfully!" : "Mail failed! Please try again.";
		
    }
}
?>
The code above includes the alteration. 2 other things : 1) The quotation marks in the <a> tags that contains the href values (mailto:) had to be escaped because they caused syntax errors and secondly the headers seem a bit weird but they could work. Im not familiar with them though. Hth.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
mandaclarke
Forum Newbie
Posts: 3
Joined: Mon Sep 13, 2010 3:18 pm

Re: sendmail.php failure to send to email address

Post by mandaclarke »

Still don't seem to have it figured out :cry:
What headers did you think were weird? How would you suggest I fix it. I really do appreciate your help on this!
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: sendmail.php failure to send to email address

Post by social_experiment »

What type of error message (if any) are you receiving?
mandaclarke wrote:I have tested using several email addresses and it works perfectly
How did you achieve this? And what type of error message are you receiving? For production purposes, remove the '@' infront of the mail() function so that the error (if any) will be displayed.

Below is an example of headers that i use for the mail function.

Code: Select all

<?php
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: noreply@website.example' . "\r\n";
 ?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply