Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Contact form doesnt send complete email

  • This topic is empty.
Viewing 15 posts - 1 through 15 (of 16 total)
  • Author
    Posts
  • #167193
    romullus
    Participant

    0 down vote favorite

    I managed to make my contact form work but somehow i cant make it send the message structured how i want…

    My code is this:

    <?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $subject = $_POST['subject'];
    $text = $_POST['text'];
    
    $formcontent=' From: $name \n E-mail: $email \n Subject: $subject \n Text: $text';
    
    $to = "[email protected]";
    $subject = 'Message from a site visitor ' . $name;
    
    $message .= 'From: ' . $name . "\n";
    $message .= 'E-mail: ' . $email . "\n";
    $message .= 'Subject: ' . $subject . "\n";
    $message .= 'Text: ' . $text;
    
    $headers = 'From: ' . $email . "\r\n";
    $headers .= 'Reply-To: ' . $email . "\r\n";
    
    $send_contact = mail($to, $subject, $email, $text);
    
    if($send_contact) 
    {
        echo "Thank you!";
    }
    else 
    {
         echo "ERROR";
    }
    ?>
    

    I receive a mail having the subject that i want but doesn’t write the sender’s e-mail, or in the body of mail it doesn’t arrange the message by Subject, E-mail and Message…

    Please help…
    btw im a begginer in this.

    #167243
    __
    Participant
    $text = $_POST['text'];
    //  ...
    $send_contact = mail($to, $subject, $email, $text);
    

    Never do this. The fourth parameter of the mail function (where you are putting $text) is the headers parameter. By putting user-submitted text there, you are going to cause lots of errors (BEST case), or allow someone to use your contact form as their own personal spam service (likely).

    In general, Never Trust User Input. Always assume that it is wrong and/or an attack. You should never use anything that the user gives you until you have 1) checked it to make sure it is what you expected, and 2) made sure that any special characters are escaped so it has no unintended effects.

    1) is called validation. 2) is called sanitization.

    doesn’t write the sender’s e-mail

    Are you sure the sender submitted an email? and that you’re using the correct form field names?

    it doesn’t arrange the message by Subject, E-mail and Message…

    Your code is building the message in the order “from”, “email”, “subject”, “text”.

    Here’s some notes. Please look it over and post again with any questions.

    #167245
    romullus
    Participant

    wow…

    i have no words to describe how much i appreciate your effort and that you made such a “for-dummies” explanation:) perfect for me…

    i just must tell you i have no php.ini file… :|

    #167246
    __
    Participant

    No problem; I’m happy to help.

    i just must tell you i have no php.ini file… :|

    Well, yes, you do; though your host might not make it accessible to you. It would probably be worth your while to ask. But if they won’t let you touch it, you can set some options at runtime:

    <?php
    error_reporting( -1 );
    ini_set( 'display_errors',1 );
    

    This is less effective (you still won’t see any parse errors, for example, just a blank page), but it’s better than nothing.

    Let me know if you have any questions.

    #167248
    romullus
    Participant

    ok:)

    i made the changes. but i have no idea why ONLY if i put away $headers from mail() i receive the email. If $headers is inside the brackets than i dont get it…

    what could be the problem?

    #167250
    __
    Participant

    Can you show the actual code you are using to define $headers?

    You can use var_dump to see the value you actually end up with and make sure it is correct, before you send the email:

    print "<pre>"; 
    var_dump( $headers );
    
    #167251
    romullus
    Participant
    <?php
    error_reporting(-1);
    ini_set('display_errors',1);
    
    $name = empty( $_POST['name'] )? 
    null: 
    $_POST["name"]; 
    
    $email = empty( $_POST['email'] ) || ! filter_var( $_POST["email"],FILTER_VALIDATE_EMAIL )?
    null:
    $_POST["email"];
    
    $subject = empty( $_POST['subject'] )?
    null:
    $_POST['subject'];
    
    $text = empty( $_POST['text'] )?
    null:
    $_POST['text'];
    
    if( $name === null || $email === null || $subject === null || $text === null ){
    // error!
    print "Error!";
    exit;
    }
    
    $to = "[email protected]";
    $subject = 'Message from a site visitor ' .$name;
    
    $message = "From: $name
    E-mail: $email
    Subject: $subject
    Message: $text";
    
    $headers = "From: $email\r\n";
    $headers .= "Reply-to: $email\r\n";
    $headers .= "Sender: [email protected]";
    
    $send_contact = mail($to, $subject, $message ,$headers);
    
    if($send_contact){
    echo "Thank you!";
    }
    else{
    echo "ERROR";
    }
    

    with $headers i dont get the mail. without it yes…

    i will try now var_dump

    #167253
    romullus
    Participant

    i put var_dump how you said and i get this:

    string(84) “From: [email protected]
    Reply-to: [email protected]
    Sender: [email protected]
    Thank you!

    #167254
    __
    Participant

    I assume “[email protected]” is your actual email address. Does the domain name (myhosting.net) match the domain name of your website? Ask your host what domain name the mail is actually sent from. (When you leave the $headers out, what From address shows up on the email?)

    (To clarify, this doesn’t need to be a real email address. It can be a “noreply” address. It’s more important that it matches the domain name of the mail server that sends your mail. Usually, using your website’s domain name works just fine.)

    You could also try putting your email address in the From header, and leaving the Sender header off.

    #167255
    romullus
    Participant

    Yes, the Sender address i copied from the Sender of the mail it has the domain of my hosting…

    Do you think it might interpret wrong the variables?

    Because i get this in my mail:

    From: hajaha
    E-mail: [email protected]
    Subject: Message from a site visitor hajaha
    Message: Ajjajajaffdd

    • and as i see the Subject that should be the one submited by the user in the contact form is the one that is also found in the subject of the email received.

    So i get the e mail From: (and here its an email address with my domain name in it) and the Subject Message from a site visitor hajaha.

    hmmm, did i understand smth wrong?

    #167263
    __
    Participant

    Do you think it might interpret wrong the variables?

    No.

    did i understand smth wrong?

    Don’t think so. But this sort of thing is really difficult to troubleshoot over a discussion thread. There are quite a few places where things might (emphasize might) be going wrong. I’m not really sure what else to suggest.

    What email client do you use (thunderbird, outlook, gmail, etc.)? Do you know how to view the raw email headers?

    #167272
    romullus
    Participant

    i am using gmail…

    no unfortunately i dont know how to view raw headers…

    #167276
    __
    Participant

    viewing email headers in gmail:

    • Log in to Gmail
    • Open the message you’d like to view headers for.
    • Click the down arrow next to Reply, at the top of the message pane.
    • Select Show Original. The full headers will appear in a new window.

    post back here (you can ******** any personal/sensitive information).

    #167279
    romullus
    Participant

    this is the raw email that i get…

    Delivered-To: *******@gmail.com
    Received: by 10.170.205.** with SMTP id w63csp196707yke;
    Tue, 1 Apr 2014 00:51:24 -0700 (PDT)
    X-Received: by 10.180.92.*** with SMTP id co4mr17983882wib.50.1396338684019;
    Tue, 01 Apr 2014 00:51:24 -0700 (PDT)
    Return-Path: <****@ls14..net>
    Received: from ls14.globe
    .net (ls14.globe.net. [94.177.99.])
    by mx.google.com with ESMTPS id 45si26785910eeh.33.2014.04.01.00.51.23
    for <*****@gmail.com>
    (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);
    Tue, 01 Apr 2014 00:51:23 -0700 (PDT)
    Received-SPF: pass (google.com: best guess record for domain of ****@ls14.globe.net designates 94.177.99. as permitted sender) client-ip=94.177.99.***;
    Authentication-Results: mx.google.com;
    spf=pass (google.com: best guess record for domain of ****@ls14.globe.net designates 94.177.99. as permitted sender) smtp.mail=****@ls14.globe.net
    Received: from **** by ls14.globe
    .net with local (Exim 4.82)
    (envelope-from <****@ls14.globe.net>)
    id 1WUtTe-001EOF-3c
    for ********@gmail.com; Tue, 01 Apr 2014 10:51:22 +0300
    To: *******@gmail.com
    Subject: Message from a site visitor name
    X-PHP-Script: *******.com/mobile/mail.php for 77.180.146.244
    Message-Id: <[email protected]
    .net>
    From: ****@ls14.globe.net
    Date: Tue, 01 Apr 2014 10:51:22 +0300
    X-AntiAbuse: This header was added to track abuse, please include it with any abuse report
    X-AntiAbuse: Primary Hostname – ls14.globe
    .net
    X-AntiAbuse: Original Domain – gmail.com
    X-AntiAbuse: Originator/Caller UID/GID – [620 500] / [47 12]
    X-AntiAbuse: Sender Address Domain – ls14.globe.net
    X-Get-Message-Sender-Via: ls14.globe
    .net: authenticated_id: theballr/primary_hostname/system user
    X-Source:
    X-Source-Args: /usr/sbin/proxyexec -q -d -s /var/run/proxyexec/cagefs.sock/socket /bin/cagefs.server
    X-Source-Dir: ********.com:/public_html/mobile

    From: name
    E-mail: [email protected]
    Subject: Message from a site visitor name
    Message: Messagefgfggggg

    ***i checked the spam folder and when i have $headers in the mail() function it sends the mail there. but at the senders name it writest (unknown sender)

    and the message that i get in the mail is like this:

    From: [email protected]
    Reply-to: [email protected]
    Sender: ***@ls14.globe***.net

    #167345
    __
    Participant

    All those X-AntiAbuse headers mean that various MTA’s along the delivery path thought the message was suspicious, for one reason or another. I would contact your web host and ask what headers+values they would suggest sending for best results. Who knows if it would help, though—some hosts simply don’t have a good mailing reputation, and aren’t motivated to maintain one.

    Only other suggestion I might make would be to try putting your domain’s email address in the From header, and just leaving the user’s email in the Reply-to header. But that’s just a guess… sorry.

Viewing 15 posts - 1 through 15 (of 16 total)
  • The forum ‘Back End’ is closed to new topics and replies.