ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: On my PC I use RHEL, at office AIX, Solaris, HP-UX, RHEL.
Posts: 254
Rep:
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!");
}
?>
Last edited by Hi_This_is_Dev; 02-13-2010 at 03:47 PM.
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.
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--
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
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.
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.
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
Distribution: On my PC I use RHEL, at office AIX, Solaris, HP-UX, RHEL.
Posts: 254
Original Poster
Rep:
Quote:
Originally Posted by nadroj
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.
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
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.
Last edited by Hi_This_is_Dev; 02-13-2010 at 03:47 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.
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.
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.
Distribution: On my PC I use RHEL, at office AIX, Solaris, HP-UX, RHEL.
Posts: 254
Original Poster
Rep:
Quote:
Originally Posted by nadroj
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.
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.
Distribution: On my PC I use RHEL, at office AIX, Solaris, HP-UX, RHEL.
Posts: 254
Original Poster
Rep:
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.