0

This is the first time I've used PHP at all and I'm having trouble implementing a mail form of all things, I can't seem to get this working. Below is my code, I'd be most appreciative if somebody could point me in the right direction in terms of debugging.

<?php

    $job_number         = $_POST['job_number'];
    $completion_time    = $_POST['completion_time'];
    $email              = $_POST['email'];

    $formcontent        = "From: $email \n \n Job Number: $job_number \n \n Completion Time: $completion_time \n";

    $recipient          = "[email protected]";
    $subject            = "Repeat Order";
    $mailheader         = "From: $email \r\n";

    mail(
            $recipient, 
            $subject, 
            $formcontent, 
            $mailheader
    ) 
    or die(
        "
            Error!
        "
    );

    echo(   "   
                <div style='font-size:24px; margin-top: 100px; text-align: center;'>
                    Thank You!
            " 
            . " - " .   
            "       <a href='home.html' style='color: #1ca03e;'>
                        Return Home
                    </a>
                </div>
            "
    );

?>

Thank you, Cameron

edit: Some more info, the server supports PHP mail scripts as it had one on there before (according to the friend I'm building this for), the error I've had while internal testing is that the mail is being sent but without any of the '$formcontent' content... Only the titles (aka: From:, Job Number:, Completion Time:)

edit edit: if it helps here is a staging server I have it up on at the moment (don't hate me for poor web-design... it's a work in progress) http://temp.fullaf.com/cameron/rak/repeat.html

12
  • 2
    What error do you get? Commented Mar 31, 2014 at 12:00
  • 1
    Your problem is, that you are using the standard PHP mail function it has been broken for a long time and a lot of mail won't go through or partially be ignored. In this answer: stackoverflow.com/questions/22591810/… I provided a code snippet for the people didn't mind using an alternative. It's called swiftmailer and won't have problems like the mail() function PHP offers. Commented Mar 31, 2014 at 12:08
  • 1
    The name of your email field is actually email_address and not email which causes the From: to not be set which will throw Warning: mail(): "sendmail_from" not set in php.ini or custom "From:" header missing. Commented Mar 31, 2014 at 12:15
  • 1
    Add 2 extra headers: $mailheader = 'MIME-Version: 1.0' . PHP_EOL; $mailheader .= 'Content-type: text/html; charset=iso-8859-1' . PHP_EOL; Commented Mar 31, 2014 at 12:24
  • 1
    @TomKriek The reality is that Tom is correct here, the mail function is just too old and hasn't been shown any love in awhile. I would highly advise to switch to a dedicated mail library such as Swiftmailer so you stop banging your head into your desk (it's just not safe!) Commented Mar 31, 2014 at 12:29

2 Answers 2

1

You can get the swiftmailer package on their site here -> http://swiftmailer.org/

require_once 'swiftmailer/lib/swift_required.php';

function new_mail($subject, $content, $recipients, $from)
{
    // Create the message
    $message = Swift_Message::newInstance();

    // Give the message a subject
    $message->setSubject($subject);

    // Set the From address with an associative array
    $message->setFrom($from);

    // Set the To addresses with an associative array
    $message->setTo($recipients);

    // Give it a body
    $message->setBody($content);

    $transport = Swift_MailTransport::newInstance();
    $mailer = Swift_Mailer::newInstance($transport);
    $result = $mailer->send($message);

}

$job_number         = $_POST['job_number'];
$completion_time    = $_POST['completion_time'];
$email              = $_POST['email'];

$message = "From: $email \n \n Job Number: $job_number \n \n Completion Time: $completion_time \n";

$recipients         = array('[email protected]' => 'Your name of choice');
$subject            = "Repeat Order";
$from               = array($email => 'Name of choice.');

new_mail($subject, $message, $recipients, $from);

I'm currently not in the position where I can access a ftp server to test this specific snippet but try it out. If there are any problems let me know.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, just waiting for a reply from my friend to tell if he's actually received anything (I've pushed two versions to the server... one with this and one without). Will mark as answered if it works :)
1

Your code is working and sending email without any issues.

  1. Check your email mail Spam box. Some times, the email will go to Spam box.
  2. You are using this "[email protected]" email address as recipient email. So don't use the same email address for sender email address. Use another email address as sender email.
  3. Make sure, Your email address "[email protected]" is receiving emails properly, when send email from other email accounts such as yahoo and gmail.
  4. Please make sure, you have setup the mail server properly in your server.

1 Comment

I'll do a little more testing, giving my friend a call now to check his spam folder. Thank you, much appreciated. (I'll mark as answered if it's working) :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.