View Full Version : PHP form details not sent to email

01-29-2010, 11:51 AM

I hav these two files on a Linux server which insists on smtp authentication. I get a 'your message has been sent' message, but the email never arrives.

This is the HTML form:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<body bgcolor="#ffffff" text="#000000">
<form method="post" enctype="multipart/form-data" name="send" action="formprocess.php">
<table border="0" bgcolor="#ececec" cellspacing="5">
<tr><td>Name</td><td><input type="text" size="30" name="name"></td></tr>
<tr><td>Email address</td><td><input type="text" size="30" name="emailaddr"></td></tr>
<tr><td>Message</td><td><textarea name="message" rows="6" cols="30"></textarea></td></tr>
<tr><td>Attachment</td><td><input type="file" name="attachment" size="50" /><br /></td></tr>
<tr><td>&nbsp;</td><td><input type="submit" value="Send"></td></tr>

And this is the PHP script behind it:

$fileatt = ""; // Path to the file
$fileatt_type = "application/octet-stream"; // File Type
$fileatt_name = ""; // Filename that will be used for the file as the attachment
$email_from = ""; // Who the email is from
$email_subject = ""; // The Subject of the email
$email_txt = ""; // Message that the email has in it
$email_to = "myEmail@yahoo.com"; // Who the email is to
$headers = "From: ".$email_from;
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$email_message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$email_message . "\n\n";
$data = chunk_split(base64_encode($data));
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
$ok = @mail($email_to, $email_subject, $email_message, $headers);
if($ok) {
echo "The file was successfully sent!";
} else {
die("Sorry but the email could not be sent. Please go back and try again!");

$host = "smtp.mySite.com";
$username = "info@mySite.com";
$password = "myPassword";


Any help appreciated.



01-29-2010, 12:51 PM
$ok = @mail($email_to, $email_subject, $email_message, $headers); Try removing the error suppression operator (@) from there.

01-29-2010, 02:06 PM
Thanks for your reply.

I have removed the @ and I get a 'The file was successfully sent!' message but, again, the email is not arriving.

Does this:

$host = "smtp.mySite.com";
$username = "info@mySite.com";
$password = "myPassword";

need to be in any particular place in the script or can I leave it where I wish?



01-29-2010, 03:17 PM
That host/username/password combo isn't doing anything down there, and it won't help if you move it. Your form handler is just using PHP's mail() function and that doesn't handle authentication.

Many people have been recommending the phpmailer (http://phpmailer.worxware.com/) package for something like that. It's a good mailer with ample documentation (http://phpmailer.worxware.com/index.php?pg=tutorial) and examples (http://phpmailer.worxware.com/index.php?pg=examples).

01-29-2010, 03:43 PM
Hello tomws

Oh, right. I didn't know that, thanks.

So I have to use delete the smtp details in my original PHP script and use the following, presumably called class.phpmail.php?

//include("class.smtp.php"); // optional

$mail = new PHPMailer();
$body = file_get_contents('contents.html');
$body = eregi_replace("[\]",'',$body);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "smtp.mySite.com"; // sets the SMTP server
$mail->Port = 26; // set the SMTP port for the GMAIL server
$mail->Username = "info@mySite"; // SMTP account username
$mail->Password = "myPassword"; // SMTP account password
$mail->SetFrom('info@mySite.com', 'First Last');
$mail->AddReplyTo("info@mySite.com","First Last");
$mail->Subject = "my Subject";
$mail->AltBody = "Use an HTML compatible email viewer!"; // optional
$address = "whoto@otherdomain.com";
$mail->AddAddress($address, "John Doe");
$mail->AddAttachment("images/phpmailer.gif"); // attachment
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {

echo "Message sent!";

I am not quite sure what it is expecting here:

$address = "whoto@otherdomain.com";
$mail->AddAddress($address, "John Doe");

and how would I reference this class.phpmail.php in my original script?



01-29-2010, 04:11 PM
Read the documentation and the readme.

01-29-2010, 04:12 PM
The documentation, tutorials, and examples (links provided earlier) should provide enough information to get you set up with phpmailer.

That code pasted from one of the examples is not class.phpmailer.php. That filename is the class file to include/require from the phpmailer package. The example code is for replacing the mail() function - this one specifically appears to pull the message body from a file, so it's not exactly the one you want. Modify as needed.

The addAddress method does just that: it adds an address (a To address), as described in the documentation (http://phpmailer.worxware.com/index.php?pg=methods).