Amit20 Posted September 11, 2012 Share Posted September 11, 2012 Hello everyone, I am facing some unexpected problem. I have a php script that is used to send email to the recepient. My script is running fine no error occurs while executing it. The problem is, the mail is sent (mail() function returns true), but the mail is not received. Also the move_uploaded_file() functions return true but when i check the directory there is no image in it. Here's my code <?php define('UPLOAD_DIR',$_SERVER['DOCUMENT_ROOT'].'/uploads'); if($_FILES['file_upload']['name']) { $file_name = $_FILES['file_upload']['name']; $file_size = $_FILES['file_upload']['size']; $file_type = $_FILES['file_upload']['type']; $file_temp_name = $_FILES['file_upload']['tmp_name']; if( file_exists($file_temp_name) ){ $isMoved = move_uploaded_file($file_temp_name,UPLOAD_DIR.$file_name); $isSent = sendMail('recipient_id@mail.com',$file_name,UPLOAD_DIR); if($isSent && $isMoved) { echo "MAIL SENT \n"; echo $file_name."\n"; echo $file_size."\n"; echo $file_temp_name."\n"; echo $file_type."\n"; } else echo "MAIL NOT SENT SINCE IMAGE FILE WAS NOT UPLOADED"; } else { echo "NO FILE SELECTED"; } function sendMail($recepient_mail_id,$file_name,$path) { $to = $recepient_mail_id; $subject = "Confirmation"; $from = "TEAM"; $message = ' <html> <body> Dear Player,<br /> <br /> Congratulations!!!<br /> <br /> <span style="background-color:#F00; color:#FFF; padding:5px; font-weight:bold; font-size:18px;"></span><br /><br /> Welcom!!!<br /> <br /> The test mail is successfully sent. <br /> </body> </html> '; $isSuccess = mail($to,$subject,$message,$headers); return $isSuccess; } ?> ANy help will be highly appreciated! Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted September 11, 2012 Share Posted September 11, 2012 The mail() function returns true if there is a sending mail server and it accepted the email without returning an error to php. That doesn't mean that the mail was actually sent. Since you are not setting the $header variable to anything, it's likely that the sending mail server doesn't know what to do with the email and isn't actually trying to send it. I would set the $header variable with a valid from address and the necessary headers for a html email. If you look in your document_root folder, you will find files named uploadssome_file.ext, because you missing a / between the upload directory name and the file name. Quote Link to comment Share on other sites More sharing options...
Amit20 Posted September 11, 2012 Author Share Posted September 11, 2012 Thank you PFMaBiSmAd for your quick reply. I added the headers to my code as suggested by you and also changed the directory path. You were right, images were uploaded to my document root directory. Now images are being uploaded correctly. But still mailis not being sent. Could there be any reason for this, since i am relying on hosting provider's mail settings and they are not in my control. So could you please tell me if there is something that i can test whether its working properly on my provider's side. Here's my modified code <?php define('UPLOAD_DIR',$_SERVER['DOCUMENT_ROOT'].'/support/php/uploads/'); if($_FILES['file_upload']['name']) { $file_name = $_FILES['file_upload']['name']; $file_size = $_FILES['file_upload']['size']; $file_type = $_FILES['file_upload']['type']; $file_temp_name = $_FILES['file_upload']['tmp_name']; $isMoved = move_uploaded_file($file_temp_name,UPLOAD_DIR.$file_name); $isSent = sendMail('abc@gmail.com',$file_name,UPLOAD_DIR); if($isMoved) echo 'File transferred successfully'; else echo 'File transmission failed'; if($isSent) { echo "MAIL SENT \n"; echo $file_name."\n"; echo $file_size."\n"; echo $file_temp_name."\n"; echo $file_type."\n"; } else echo "MAIL NOT SENT"; } else { echo "NO FILE SELECTED"; } function sendMail($recepient_mail_id,$file_name,$path) { $to = $recepient_mail_id; $subject = "Confirmation"; $from = "TEAM"; $headers .= 'MIME-Version: 1.0' . "\r\n"; $headers = 'From: abc.co.in' . "\r\n". 'Reply-To: noreply@abc.co.in' . "\r\n" .'X-Mailer: PHP/' . phpversion(); $headers .= 'Content-type : text/html; charset=iso-8859-1' . "\r\n"; $message = ' <html> <body> Dear Player,<br /> Congratulations!!!<br /> Welcome !!!<br /> The test mail is successfully sent.<br /> </body> </html> '; $isSuccess = mail($to,$subject,$message,$headers); return $isSuccess; } ?> Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted September 11, 2012 Share Posted September 11, 2012 How do you use sendMail function ? I can not see to call anywhere in your script! Ops...... sorry I've seen it - $isSent = sendMail('abc@gmail.com',$file_name,UPLOAD_DIR);, but.... this one is completely wrong Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted September 11, 2012 Share Posted September 11, 2012 that is because he is using mail instead of his own mail function. change it to sendMail() Quote Link to comment Share on other sites More sharing options...
DavidAM Posted September 11, 2012 Share Posted September 11, 2012 $headers .= 'MIME-Version: 1.0' . "\r\n"; $headers = 'From: abc.co.in' . "\r\n". 'Reply-To: noreply@abc.co.in' . "\r\n" .'X-Mailer: PHP/' . phpversion(); $headers .= 'Content-type : text/html; charset=iso-8859-1' . "\r\n"; 1) The From header must be a valid email address. You can add a display name, if desired: From: "ABC Website" <mailbox@abc.co.in> This is the most likely reason that the message is not being sent. 2) You left out the "\r\n" after the X-Mailer header. This causes the Content-Type header to not be seen as a header but as part of the value of the X-Mailer header. This will cause the message to be improperly formatted on some (if not all) mail clients. By the way, there is no need to include the Reply-To header, unless you want replies to go to someone other than the From address. Why not just use From: "abc.co.in" <noreply@abc.co.in> Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted September 11, 2012 Share Posted September 11, 2012 @Amit20, you return a function into the function with 4 arguments. There is no logic and this one could be wrong in this particular case. If you want to return something like boolean value or text message, try this , if( mail($to,$subject,$message,$headers) { return "mail sent successfully message"; } else { return "mail message cannot be sent successfully"; } P.S When you use return statement to see the message you need to echo this function. For this case, instead of return just replace them with echo "mail sent successfully"; and for failed echo "mail message cannot be sent successfully"; exit; Use the exit function to stop the script if there is something wrong. Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted September 11, 2012 Share Posted September 11, 2012 Hm...I've made a simple test with abs() based function. May be David is right function addNum($a, $b, $c){ $d = 5; return abs($a).abs($b).abs($c).abs($d); } echo addNum(1, 2, 3); Quote Link to comment Share on other sites More sharing options...
Amit20 Posted September 13, 2012 Author Share Posted September 13, 2012 Hello again, Thank you DavidAM, my problem is solved, and thank you jazzman1 i have made the appropriate changes suggested by you and yes your advice will be beneficial. Now i tested the code it works perfectly on localhost but when i tried it with the server on which the website is hosted its not working, like the mails are not being delivered. Also i am trying to send an image as an attachment which is not being attached on localhost as well. I have never sent attachment using php code, i am new to it. The attachment code i have referred from online tutorial. Here's m modified code <?php error_reporting(E_STRICT); define('UPLOAD_DIR',$_SERVER['DOCUMENT_ROOT'].'/support/php/uploads/'); //define('UPLOAD_DIR','C:\\xampp\\htdocs\\cyu\\uploads\\'); $allowed_extensions = array('jpeg','jpg','png','gif','pjpeg','bmp'); if($_FILES['file_upload']['name']) { $extension = explode('.',$_FILES['file_upload']['name']); if($_FILES['file_upload']['size'] <= 300000 && in_array($extension[1],$allowed_extensions) ) { $file_name = $_FILES['file_upload']['name']; $file_size = $_FILES['file_upload']['size']; $file_type = $_FILES['file_upload']['type']; $file_temp_name = $_FILES['file_upload']['tmp_name']; if( file_exists($file_temp_name) ){ $isMoved = move_uploaded_file($file_temp_name,UPLOAD_DIR.$file_name); $isSent = sendMail('amitg@gmail.com',$file_name,UPLOAD_DIR); if($isMoved) echo 'file transferred successfully'; else echo UPLOAD_DIR.$file_name; if($isSent) { echo "MAIL SENT \n"; } else echo "MAIL NOT SENT"; } else echo "File does not exist"; } else echo " jpg,gif,bmp or png extensions are allowed and file size should not be more than 3 Mb"; } else { echo "NO FILE SELECTED"; } function sendMail($recepient_mail_id,$file_name,$path) { $file = $path.$file_name; $file_size = filesize($file); $handle = fopen($file,"r"); $content = fread($handle,$file_size); fclose($handle); $content = chunk_split(base64_encode($content)); $uid = md5(uniqid(time())); $name = basename($file); $to = $recepient_mail_id; $subject = "TEST EMAIL"; $from = "XYZ India"; $headers .= 'MIME-Version: 1.0'."\r\n"; $headers .= 'From:support@abc.co.in'."\r\n".'X-Mailer: PHP/'."\r\n". phpversion(); $headers .= 'Content-Type: multipart/mixed; boundary=\"".$uid.\"\r\n\r\n'; $headers .= '--'.$uid.'\r\n'; $headers .= 'Content-Transfer-Encoding: 7bit\r\n\r\n'; $headers .= 'Content-Type: application/octet-stream; name = '.$file_name.'\r\n'; $headers .= 'Content-type: text/html; charset=iso-8859-1'."\r\n"; $headers.= 'Content-Dispostion: attachment; file='.$file_name.'\r\n'; $headers .= $content.'\r\n\r\n'; $headers .= '--'.$uid.'--'; $headers .="Content-Type: image/jpg; file=".$file_name."\"\"\r\n"; $message = ' <html> <body> Dear Player,<br /><br /> This is TEST MAIL !!! Team India </body> </html>'; $isSuccess = false; if(mail($to,$subject,$message,$headers)) $isSuccess = true; else $isSuccess = false; return $isSuccess; } ?> Quote Link to comment Share on other sites More sharing options...
DavidAM Posted September 13, 2012 Share Posted September 13, 2012 First, in development and testing, you should use error_reporting(E_ALL); so you can see ALL errors and warnings. The problem with the image attachment is the result of a bad tutorial. There are several problems with that code. Here is your code, with markers for my comments: $headers .= 'MIME-Version: 1.0'."\r\n"; $headers .= 'From:support@abc.co.in'."\r\n".'X-Mailer: PHP/'."\r\n". phpversion(); /* 1 */ $headers .= 'Content-Type: multipart/mixed; boundary=\"".$uid.\"\r\n\r\n'; $headers .= '--'.$uid.'\r\n'; /* 2 */ $headers .= 'Content-Transfer-Encoding: 7bit\r\n\r\n'; $headers .= 'Content-Type: application/octet-stream; name = '.$file_name.'\r\n'; /* 4 */ $headers .= 'Content-type: text/html; charset=iso-8859-1'."\r\n"; $headers.= 'Content-Dispostion: attachment; file='.$file_name.'\r\n'; $headers .= $content.'\r\n\r\n'; $headers .= '--'.$uid.'--'; /* 3 */ $headers .="Content-Type: image/jpg; file=".$file_name."\"\"\r\n"; 1) This is the last entry that is a valid mail header. Every thing below this is part of the MIME message body. The person who wrote this tutorial is trying to force that by putting the two CRLF's at the end of the line. In raw email text, this indicates the end of the headers. However, putting the message body in the headers is bad practice (IMHO). The specification says that headers can appear in any order, so the php mail() function (and underlying system sendmail function) is free to rearrange the stuff that you tell it is headers. Also, the function may or may not try to clean up your headers by removing extra line-endings -- testing on my current version of PHP indicates that it does not, but I don't know about other past or future versions. Oh yeah, and the 'Date:' header is going to have to be added somewhere, so what if it sticks it in the middle of your stuff? 2) Here you indicate that the (image) data is encoded in 7-bit ASCII. In fact, you encoded it with base64_encode() earlier in your code. This line should read: Content-Transfer-Encoding: base64 3) This MIME header is trying to specify the filename for the following data. However, it appears after the closing boundary and is therefore not part of the message at all. Note, also, that your message body ($message in your code) will appear after this line, again, after the closing boundary so it will not be part of the MIME message either. 3) Also, you embedded two double-quotes immediately after the filename. I don't know why, unless you were trying to put the filename inside double quotes, in which case, you need to move one of them. 4) This line indicates you are about to send content that is HTML, however it is contradicting the line immediately above it that says you are about to send an octet-stream. Wikipedia has a good example of what Mulipart Messages should look like. Here it is: Note: the #-1-# are NOT part of the message I added them for discussion below: #-1-# MIME-Version: 1.0 Content-Type: multipart/mixed; boundary=frontier This is a message with multiple parts in MIME format. #-2-# --frontier #-3-# Content-Type: text/plain This is the body of the message. --frontier #-3-# Content-Type: application/octet-stream Content-Transfer-Encoding: base64 PGh0bWw+CiAgPGhlYWQ+CiAgPC9oZWFkPgogIDxib2R5PgogICAgPHA+VGhpcyBpcyB0aGUg Ym9keSBvZiB0aGUgbWVzc2FnZS48L3A+CiAgPC9ib2R5Pgo8L2h0bWw+Cg= --frontier-- #-3-# 1) The following two lines are HEADER lines and are followed by a blank line. 2) This is the first part of the body. This plain text line is not strictly required, but gives users a plain text message if their mail client cannot decode the MIME content. 3) This is the boundary between the multiple parts of the MIME message. You will note that the last one has an extra "--" at the end, which indicates that it is the end of the MIME message. Basically, each part of a Multipart MIME Message is formatted link a "micro-email" (if I may coin a term). There is a boundary line that indicates, well, a boundary between the multiple parts. Then there are some headers indicating the content type, encoding, disposition, etc. Then a blank line indicating the end of the headers; and finally, the content in whatever encoding you specified. The last part is followed by another boundary, with the extra "--" to indicate that the MIME is finished. Quote Link to comment Share on other sites More sharing options...
Amit20 Posted September 14, 2012 Author Share Posted September 14, 2012 Thank you very very very much DavidAM for your wonderful and generous reply. It allowed me to understand the things pretty clearly regarding emails. It was a good lesson to learn. Now the issue is only one thing, my code is working perfect on my localhost, but when i upload it on live server, it is not working; the email is shooted but i am not receiving it. I have checked the "From" email address; its valid since i am able to send it through a mailer. Also there's no error/warning returned by the mail function. Can you suggest what could be the probable cause for this behavior. Here's my working code, <?php error_reporting(E_ALL); define('UPLOAD_DIR',$_SERVER['DOCUMENT_ROOT'].'/support/php/uploads/'); //define('UPLOAD_DIR','C:\\xampp\\htdocs\\cyou\\uploads\\'); $allowed_extensions = array('jpeg','jpg','png','gif','pjpeg','bmp'); if($_FILES['file_upload']['name']) { $extension = explode('.',$_FILES['file_upload']['name']); if($_FILES['file_upload']['size'] <= 20971520 && in_array($extension[1],$allowed_extensions) ) { $file_name = $_FILES['file_upload']['name']; $file_size = $_FILES['file_upload']['size']; $file_type = $_FILES['file_upload']['type']; $file_temp_name = $_FILES['file_upload']['tmp_name']; if( file_exists($file_temp_name) ){ $isMoved = move_uploaded_file($file_temp_name,UPLOAD_DIR.$file_name); $isSent = sendMail('amit@gmail.com',$file_name,UPLOAD_DIR); if($isSent) echo "MAIL SENT \n"; else echo "MAIL NOT SENT"; } else echo "File does not exist"; } else echo " jpg,gif,bmp or png extensions are allowed. File size should not be more than 3 Mb"; } else { echo "NO FILE SELECTED"; } function sendMail($recepient_mail_id,$file_name,$path) { $file = $path.$file_name; $file_size = filesize($file); $handle = fopen($file,"r"); $content = fread($handle,$file_size); fclose($handle); $content = chunk_split(base64_encode($content)); $uid = md5(uniqid(time())); $name = basename($file); $to = $recepient_mail_id; $subject = "TEST EMAIL"; $from = "XYZ India"; $headers = 'From:support@abc.co.in'."\r\n".'X-Mailer: PHP/'."\r\n". phpversion(); $headers .= 'MIME-Version: 1.0'."\r\n"; $headers .= 'Content-Type: multipart/mixed; boundary=\"'.$uid."\"\r\n\r\n"; $message = 'This is a multi-part message in MIME format.'."\r\n"; $message.= "--".$uid."\r\n"; $message .= 'Content-type:text/html; charset=iso-8859-1'."\r\n"; $message .= 'Content-Transfer-Encoding: 7bit'."\r\n\r\n"; $message .= '<html><body>Dear Player, <br/> This is a Test Email !!!<br /> Team</body></html>'."\r\n"; $message .= "--".$uid."\r\n"; $message .= 'Content-Type: application/octet-stream;file='.$path.$file_name."; name=\"".$file_name."\"\r\n"; //$message .= "Content-Type: image/jpg; file=".$path.$file_name."\"\"\r\n"; $message .= 'Content-Transfer-Encoding: base64'."\r\n"; $message .= 'Content-Disposition: attachment;filename='.$file_name."\r\n\r\n"; $message .= $content."\r\n\r\n"; $message .= "--".$uid."--"; $isSuccess = false; if(mail($to,$subject,$message,$headers)) $isSuccess = true; else $isSuccess = false; return $isSuccess; } ?> Quote Link to comment Share on other sites More sharing options...
Christian F. Posted September 15, 2012 Share Posted September 15, 2012 Id the site hosted on the domain abc.co.in, and are you sure you don't need to use the proper mail server to send out mails from that domain? There's been quite a few topic on just this kind of problem the last couple of weeks, so a search of the forum should prove quite beneficial. Quote Link to comment Share on other sites More sharing options...
Amit20 Posted September 28, 2012 Author Share Posted September 28, 2012 Hello again, Thank you everyone for your replies. I did tried modifying the code but alas, none seems to work. So what i did is, downloaded PHPMailer and tried using that too, but that too works on my localhost and a server that is in located in my country but not on the server on which i wanted to run the code. It is a remote server located in another country. I cannot make it out the cause of this. So can anyone tell the possible cause, maybe it will help me and others getting more knowledge about it. Cheers!!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.