Cameron Guthrie
Cameron Guthrie

Reputation: 141

PHP Mail script problems

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

Upvotes: 0

Views: 114

Answers (2)

Rimble
Rimble

Reputation: 893

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.

Upvotes: 1

user2944050
user2944050

Reputation:

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.

Upvotes: 1

Related Questions