user1340749
user1340749

Reputation: 11

PHP mail form failed to send

I am new in PHP programming. I tried to make a form that includes file attachment. As I tested it, it failed to send the form to my email. Please help me look at my PHP codes, if I have missed anything. Thank you in advance for your guidance. I referred this coding from http://www.shotdev.com/php/php-mail/php-send-email-upload-form-attachment-file/

The error message shows: Warning: mail() [function.mail]: Bad parameters to mail() function, mail not sent. It is pointing to the line "$sendMail = mail($sendTo, $mailSubject, null, $header);"

Below is my PHP code:

    <?php

/*Retrive inputs from the form */
//Required fields
$fullName = $_POST["Fullname"];
$gender = $_POST["genderRadioGroup"];
$DOB = $_POST["DOB"];
$address = $_POST["Address"];
$state = $_POST["State"];
$zipCode = $_POST["Zipcode"];
$contactNo = $_POST["Contact"];
$emailAdd = $_POST["Email"];


//Optional fields
// If applicant filled the field, take it. Else, make it " - ".
$nickName = $_POST["Nickname"];
if($nickName!="")
{
    $nickName = $nickName;
}
else
{
    $nickName = " - ";
}

$description = $_POST["Description"];
if($description!="")
{
    $description = $description;
}
else
{
    $description = " - ";
}

//Message content
$message = <<<EOD
<br><hr><br>
Full Name: $fullName <br>
Nickname: $nickName <br>
Gender: $gender <br>
Date of Birth: $DOB <br>
Address: $address <br>
State: $state <br>
Zip Code: $zipCode <br>
Contact Number: $contactNo <br>
E-mail address: $emailAdd <br>
Description: $description <br>
EOD;

/* Uniqid Session */
$sessionID = md5(uniqid(time()));

/* Header */
//Sender
$header = "From: $fullName <$emailAdd> \n";
$header .= "Reply-To: $emailAdd \n";
//MIME
$header .= "MIME-Version: 1.0 \n";
$header .= "Content-type: multipart/mixed; boundary=\"".$sessionID."\"\n\n";
$header .= "This is a multi-part message in MIME format. \n";

$header .= "-- $sessionID \n";
$header .= "Content-type: text/html; charset=utf-8 \n";
$header .= "Content-transfer-encoding: 7bit \n\n";
$header .= "$message \n\n";

/* Attachment */
if($_FILES["Attachment"]["name"] != "") //If there is attachment
{
    //Variables
    $fileName = $_FILES["Attachment"]["name"];
    $fileContent = chunk_split(base64_encode(file_get_contents($_FILES["Attachment"]["tmp_name"])));

    //Adding attachment to header
    $header .= "-- $sessionID \n\n";
    $header .= "Content-Type: application/octet-stream; name=\"".$fileName."\"\n";
    $header .= "Content-Transfer-Encoding: base64 \n";
    $header .= "Content-Disposition: attachment; filename=\"".$fileName."\"\n\n";
    $header .= $fileContent."\n\n";
}

/* Send Mail */
// Recipient
$sendTo = "[email protected]";
$mailSubject = "The Form - $fullName";
$sendMail = mail($sendTo, $mailSubject, null, $header);

if($sendMail)
{
    echo "Thank you!";
}
else
{
    echo "An error occured.";
}
?>

Upvotes: 0

Views: 788

Answers (2)

F3rr31r4
F3rr31r4

Reputation: 268

@F3rr31r4 It displays Warning: mail() [function.mail]: Bad parameters to mail() function, mail not sent. It points to the line $sendMail = mail($sendTo, $mailSubject, null, $header); also the text "An error occurred.", the one that I had put in the IF statement, at the end of the code. – user1340749

 1. Check your PHP version
 2. print("<pre>" . $header . "</pre>");  and check your msg

Upvotes: 0

F3rr31r4
F3rr31r4

Reputation: 268

i didn't check all you code.. but...

    mail(  $to,         $subject,      $message,     $headers);
    mail(  $sendTo,    $mailSubject,     null,       $header);

http://php.net/manual/en/function.mail.php

Upvotes: 1

Related Questions