LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Email Body is not being Sent (https://www.linuxquestions.org/questions/programming-9/email-body-is-not-being-sent-788519/)

Hi_This_is_Dev 02-11-2010 03:21 PM

Email Body is not being Sent
 
Hi,


Can anybody tell me why the Message-Body is not shown when email is recieved using the PHP Script as given below?

However, I can see the Attachment and the Subject Line properly. If I send an email to my Internal ID then everything is fine. But when the same email is sent to an external id such @someOtherCompany then the Message in the Body is not sent.

Can you show me how to seperate the $message varibale from being used in the $header part abd form it in HTML in a way that the Attachment part, and the subject part are not disturbed?

Code:

<?php
 
#  .....................................................................................
#  This section attaches a specified .txt to the body of an email and sends it.
#  .....................................................................................
 
#  .....................................................................................
#          Attachment and Body of the Message Packing
#  .....................................................................................
 
 
    $message =  '<html><body><p>Hi,</p><br />' .
            '<p>Attached is the Report of the Media Tapes for the ' . $subjLine .
            '</p></body></html>';
 
 
    $file = "Uploads/" . $ticketID . ".txt";
    $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);
 
    $header = "MIME-Version: 1.0\r\n";
    $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
    $header .= "This is a multi-part message in MIME format.\r\n";
    $header .= "--".$uid."\r\n";
 
    # These two lines to avoid SPAM:
    $header .= "Message-ID: <192.168.xx.xx.@company.com:>";
    $header .= "X-Mailer: PHP v".phpversion()."\r\n";
 
    $header .= "Content-type:text/html; charset=iso-8859-1\r\n\r\n";
    $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $header .= "".$message."\n";
 
    $header .= "--".$uid."\r\n";
    $header .= "Content-Type: application/octet-stream; name=\"".$name."\"\r\n";
    $header .= "Content-Transfer-Encoding: base64\r\n";
    $header .= "Content-Disposition: attachment; filename=\"".$name."\"\r\n\r\n";
    $header .= $content."\r\n\r\n";
    $header .= "--".$uid."--";
 
   
 
#  .....................................................................................
#      Subject Line of the Email Message
#  .....................................................................................
 
    $subjLine .= ' [Ticket# ';
    $subjLine .= $ticketID;
    $subjLine .= ']';
#  --------------------------------------------------------------------------------
 
 
    $status = @mail("User@Notes.SomeCompany.com", $subjLine, $message, $header);
 
 
    if($status) {
 
        echo "<font face=verdana size=2>The file was successfully sent!</font>";
    }
 
    else {
 
        die("Sorry but the email could not be sent. Please go back and try again!");
    }
 
?>


nadroj 02-11-2010 03:53 PM

After a quick scan, try and move the "message" declaration line after the "subjLine" declaration.

Hi_This_is_Dev 02-12-2010 03:21 PM

Quote:

Originally Posted by nadroj (Post 3860733)
After a quick scan, try and move the "message" declaration line after the "subjLine" declaration.

Okay, so I did what you suggested:



Code:

$subjLine .= 'Subject Goes Here...!';
$message =        'Message Goes Here...!';


I cut the message declaration part and pasted it right after the subject declaration part.

Outputs:

I got the Attachment along with the Subject Line on on my Internam Domain ID. But NO MESSAGE BODY!

I got the Subject Line on on my External Domain ID. No attachment! And this Greek in the body of the message:

Code:

Content-Type: application/octet-stream; name="DUMMY-IGNORE00000000.txt" Content-Disposition: attachment; filename="DUMMY-IGNORE00000000.txt" Content-Transfer-Encoding: base64 TWVkaWFJRKCgoKCgTGlicmFyeU5hbWWgoKCgoFJldHVybkRhdGWgoKCgoFBvbGljeQ0KTWVkaWEw oKCgoKBHb29zZaCgoKCgoKCgoKCgoKCgoDEwLzEwLzIwMTCgoKCgoFVuZGZpbmVk

nadroj 02-12-2010 03:56 PM

I tried the example here http://www.finalwebsites.com/forums/...achment-script. Two tests with a .txt and .jpg file worked fine. It appears this may be where you got your code from initially, too.

I have no clue what you mean by "Internal/External ID"s. Are you writing a program to receive the mail also? The output you've shown above seems like the message was sent but the attachment was not decoded and put into a separate file. Can you test your script (or the raw one from the link above) and read the message from an actual mail client (i.e. webmail like Hotmail, GMail, etc, or a client program like Outlook, Thunderbird, etc). I imagine it will work completely fine in one of these clients. If thats the case, the problem is obviously with whatever you are currently reading the mail with.

Hi_This_is_Dev 02-12-2010 04:00 PM

Code:

$header = "MIME-Version: 1.0\r\n";
        $header .= "Content-type:text/html; charset=iso-8859-1\r\n\r\n";
        $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";       
        $header .= "" . $message . "\r\n\r\n";

        $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";

If I put the text/html part first and the mulitpart/mixed after it then attachment is not sent and the following message is added after the message of the body of the email:





Code:

Content-Type: multipart/mixed; boundary="31ac74374fa12fde577ebbaaff50cd49" From: Media Replenishment Report ; This is a multi-part message in MIME format. --31ac74374fa12fde577ebbaaff50cd49 --31ac74374fa12fde577ebbaaff50cd49 Content-Type: application/octet-stream; name="HD000000HD000000.txt" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="HD000000HD000000.txt" TWVkaWFJRKCgoKCgTGlicmFyeU5hbWWgoKCgoFJldHVybkRhdGWgoKCgoFBvbGljeQ0KZ2ZkoKCg oKBHb29zZaCgoKCgoKCgoKCgoKCgoGZkZ6CgoKCgZmRnZmRn --31ac74374fa12fde577ebbaaff50cd49--

Hi_This_is_Dev 02-12-2010 04:08 PM

Quote:

Originally Posted by nadroj (Post 3861901)
I tried the example here http://www.finalwebsites.com/forums/...achment-script. Two tests with a .txt and .jpg file worked fine. It appears this may be where you got your code from initially, too.

I have no clue what you mean by "Internal/External ID"s. Are you writing a program to receive the mail also? The output you've shown above seems like the message was sent but the attachment was not decoded and put into a separate file. Can you test your script (or the raw one from the link above) and read the message from an actual mail client (i.e. webmail like Hotmail, GMail, etc, or a client program like Outlook, Thunderbird, etc). I imagine it will work completely fine in one of these clients. If thats the case, the problem is obviously with whatever you are currently reading the mail with.



Hi,


Actually, my Webserver is on Windows 2003 Box. Whereas the SMTP Server is a UNIX Box which I am using to foward a Text File (Report) as an Attachment along with a Message (to be shown in the body of the email, of course).

By Internal ID I mean an Email Address which I use to send an email to and the ID belongs to the Intranet. Example: User@MyCompany.com

By External ID I mean an Email Address which I use to send an email to and the ID belongs to some other domain such as: User@XYZCompany.com, User@Yahoo.com, user@ServiceProvider.com, etc.

The curious thing is that we both (our company and the service provider) are using Lotus Notes as the Email Client. I am able to get the Message in the Body of the Email and the Attachment (.txt File) without any problem, whereas the Service Provider (I tested on some other domains such as yahoo.com also) is not getting any message in the body of the email.

nadroj 02-12-2010 04:30 PM

I know you've modified your code, but make sure you fix any warnings or errors its giving you. Your initial code has a number of errors, besides the one I pointed out. Make sure you're using variables after they've been declared.

Next,
Code:

        $header .= "Content-type:text/html; charset=iso-8859-1\r\n\r\n";
When I tested yours (after fixing the use of non-declared variables), this line seemed to be causing a problem, and the next line ("Content-Transfer-Encoding: 7bit") was showing up in the body of the email. Remove the trailing "\r\n" so that it becomes
Code:

        $header .= "Content-type:text/html; charset=iso-8859-1\r\n";
Also look for other places where you may have done this, and let me know if it fixes it. If not, post all of your complete, updated code.

Hi_This_is_Dev 02-13-2010 01:07 PM

Quote:

Originally Posted by nadroj (Post 3861930)
I know you've modified your code, but make sure you fix any warnings or errors its giving you. Your initial code has a number of errors, besides the one I pointed out. Make sure you're using variables after they've been declared.

Next,
Code:

        $header .= "Content-type:text/html; charset=iso-8859-1\r\n\r\n";
When I tested yours (after fixing the use of non-declared variables), this line seemed to be causing a problem, and the next line ("Content-Transfer-Encoding: 7bit") was showing up in the body of the email. Remove the trailing "\r\n" so that it becomes
Code:

        $header .= "Content-type:text/html; charset=iso-8859-1\r\n";
Also look for other places where you may have done this, and let me know if it fixes it. If not, post all of your complete, updated code.



Hi,


I have made the changes you suggested. I still get the same output. Just to repeat:

on my Internal Email ID (incisde my company or Intranet/LAN/WAN - whatever) I get everything fine, whereas on the other Email IDs (Users@SomeOtherCompany.com) only the Attachment and the Subject Line are fine. The Body of the Message is not shown at all.


Here's the complete listing of the codes I am using. [I have deliberatly removed the Real Email IDs -you can guess why ;)]


Code:


# Attach a specified .txt to the body of an email & sends it.

$message = "";
$header = "";
$content = "";
$subjLine = "";
       
$message = '<html><body><p>Message Goes Here</p></body></html>';

$file = "Uploads/sample.txt";
$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);

$header = "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n";
$header .= "From: Media Report <Admin@Notes.Company.com>;" . "\r\n";
$header .= 'Cc: HelpDesk@Notes.MyCompany.com' . "\r\n";

$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$uid."\r\n";

$header .= "Message-ID: <192.168.xx.xx@company.com:>";
$header .= "X-Mailer: PHP v".phpversion()."\r\n";

$header .= "Content-type:text/html; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n";
$header .= "".$message."\n";

$header .= "--".$uid."\r\n";
$header .= "Content-Type: application/octet-stream; name=\"".$name."\"\r\n";
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$name."\"\r\n";
$header .= $content."\r\n";
$header .= "--".$uid."--";       

$subjLine = 'Subject Goes Here';

$status = @mail("Users@notes.MyCompany.com,Services@SeomeOtherCompany1.in,Services@SeomeOtherCompany2.Com", $subjLine, "", $header);

if($status) {
  echo "The Report has been Successfully Emailed!";
}

else {
  echo "Please go back and try again!";
}

?>




Please Note:

All I want is:

Attach a Predefined Existing Text File (.Txt)
Set the Subject Line
Set the Message to be Displayed in the Body of the Email
Send the Email to people whose Email Addresses are both: Internal and External, i.e., they could belong to my company and to some other company. Both the parties (on their Email IDs)should get the same output.

nadroj 02-13-2010 01:14 PM

Yes, I already understand what the problem your having is. It is strange that it works on the internal mail system but not the external one. This is only "strange" since, as you said, they are using the same mail server. When I was playing around with writing a SMTP mail server in Java a while back, it would properly receive messages from one of the two domains I was testing, but not the other (the other being Hotmail).

Since everything else is the same except the OS, maybe it is just some delimiter problem (i.e. "\r", vs. "\n", and the various combinations of it). However, if you're following the standard of the protocol, then it should work. I believe quite strongly that it is a delimiter problem, actually.

I'll try your updated code on two different servers to see if I get the exact same output. Though I think I'm out of ideas. Maybe try asking tech support/admin of the other (external) server.

nadroj 02-13-2010 01:31 PM

The code you posted is missing the opening "<?php". Is there anything else it is missing? I tried it, after adding that line, and it doesn't show the body for either of my domains. I'll keep looking.

nadroj 02-13-2010 01:45 PM

I dont understand why you dont just use the code in the link I sent you. Have you tested that on both servers? It should work fine.

In your code you removed all "\r\n\r\n", which is not correct. You still need these, in the appropriate places. If you dont understand what they mean then look it up... you cant just throw stuff together and hope it complies with the protocol.

Also, move the From/To/CC, etc. at the start of the header. Here is the code after I've updated it.
Code:

<?php
# Attach a specified .txt to the body of an email & sends it.

$message = "";
$header = "";
$content = "";
$subjLine = "";
       
$message = '<html><body><p>Message Goes Here</p></body></html>';

$file = "test.txt";
$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);

$header = "From: Hi Name <youremail@host.com>;" . "\r\n";
$header .= 'Cc: youremail@host.com' . "\r\n";

$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";


$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$uid."\r\n";

$header .= "Message-ID: <hostname:>";
$header .= "X-Mailer: PHP v5\r\n";

$header .= "Content-type:text/html; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= "".$message."\r\n\r\n";

$header .= "--".$uid."\r\n";
$header .= "Content-Type: application/octet-stream; name=\"".$name."\"\r\n";
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$name."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--".$uid."--";       

$subjLine = 'Subject Goes Here';

$status = @mail("youremail@host.com", $subjLine, "", $header);

if($status) {
  echo "The Report has been Successfully Emailed!";
}

else {
  echo "Please go back and try again!";
}

?>

Try it for both servers, making sure to change the emails and attachments. It should work fine.

Hi_This_is_Dev 02-13-2010 03:35 PM

Quote:

Originally Posted by nadroj (Post 3862713)
The code you posted is missing the opening "<?php". Is there anything else it is missing? I tried it, after adding that line, and it doesn't show the body for either of my domains. I'll keep looking.



Sorry, I missed that
Code:

<?php
only here while posting. It is there in my php file.

Hi_This_is_Dev 02-13-2010 05:13 PM

Hi,


I have tried the modified version, as done by you, and I am getting the same output. :(


Can we do one thing: Take out the $message part from the $header and use it directly in the mail() function wherein we have used "" instead?

How to seperate message body from the header in a multipart/mixed content-type?

Here's a similar tutorial:
http://www.texelate.co.uk/blog/send-...ment-with-php/

I am now going to make changes in my PHP file accordingly. Let me see if it can help.

nadroj 02-13-2010 05:27 PM

I've edited the post. Note that in your code there is a semicolon in the address for "Message-ID", which maybe you had because you meant to put a port number in it? I'd remove the semicolon as it certainly isnt helping anything.

I dont know what else to tell you about the script. It works fine for me, for a number of different mail servers. As I suggested above, you'd probably have to talk to the IT/admin people of the problem server directly. Its probably a problem on their end or configuration.

Hi_This_is_Dev 02-13-2010 06:51 PM

Thanks for that!

Nope, removing the : didn't make any difference. However, thanks again for your help. I will let you know when I succeed. Now, I am leaving my office. It is morning and my shift is over. :)


All times are GMT -5. The time now is 04:29 AM.