• Welcome to Valhalla Legends Archive.
 

[PHP] Auto Emailer With Attachements

Started by Insecure, May 28, 2007, 07:34 PM

Previous topic - Next topic

Insecure

I have a question that is bugging the crap out of me.  I found a source on php.net about sending emails with attachments.  I got it to work just fine and had no problems with it when I go to the file on my web server.


<?php

require_once 'vars.php';

//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash md5(date('r'time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers "From: $from\r\nReply-To: $from";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment chunk_split(base64_encode(file_get_contents($attachment)));
//define the body of the message.
ob_start(); //Turn on output buffering
?>

--PHP-mixed-<?php echo $random_hash?>
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash?>"

--PHP-alt-<?php echo $random_hash?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<?php echo $message?>

--PHP-alt-<?php echo $random_hash?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<?php echo $message?>

--PHP-alt-<?php echo $random_hash?>--

--PHP-mixed-<?php echo $random_hash?>
Content-Type: application/txt; name=<?php echo $saveas?>
Content-Transfer-Encoding: base64
Content-Disposition: attachment

<?php echo $attachment?>
--PHP-mixed-<?php echo $random_hash?>--

<?php
//copy current buffer contents into $message variable and delete current output buffer
$message ob_get_clean();
//send the email
$mail_sent = @mail($to$subject$message$headers);
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent "Mail sent" "Mail failed";
?>



BUT if I have an include or require or I put it inside a function and try calling the function or I include it in the current function... It doesn't work and it tells me mail failed.


function addtoFile() {
  $myFile = "./list/test.txt";
  $fh = fopen($myFile, 'w') or die("can't open file");

   $query = mysql_query("SELECT * FROM catrequest WHERE new = '1' ORDER BY lastname ASC, firstname");
    while($row = mysql_fetch_array($query)) {
     $stringData = $stringData . $row['lastname'] . ',' . $row['firstname'] . ',' . $row['address1'] . ',' . $row['address2'] . ',' . $row['city'] . ',' . $row['state'] . ',' . $row['zip'] . "
";
    }
    fwrite($fh, $stringData);
    include("./email.php");
}



EDIT to include vars.php.


<?php

/* Global Database Variables */
 
$db_host   'localhost';     // Database Server //
 
$db_name   'cia_****';   // Database Name //
 
$db_user   'cia_****';   // Database Username //
 
$db_pass   '****';       // Database Password //
/* ENDS Global Database Variables */


/* Global Email Variables */
 
$to '[email protected]'//define the receiver of the email
 
$from '[email protected]';
 
$subject 'Test email with attachment'//define the subject of the email
 
$message '<h2>Hello Jon!</h2><p>This is something with <b>HTML</b> formatting and an <b>attachment</b></p>';
 
$attachment './list/test.txt';
 
$saveas 'mailinglist.txt';
/* ENDS Global Email Variables */

/* Global Catalog Variables */
 
$minbatchnum 0;
/* ENDS Global Catalog Variables */
?>



Does anyone have any ideas what would cause this and possibly a solution to fix it?  As you can probably tell the main reason I would like this to work w/o having to go to the page myself would be because I would like the auto mailer to be Automatic.

Also, I have tried setting up a pipeline in Cpanel 11 and sending an email to a specific account and having it pipeline to the file and that doesn't seem to work as I don't get an email that way.

Thank you for any possible help.

rabbit

*sigh*
It helps to understand what's going on in a script before you try to use or modify it.
Grif: Yeah, and the people in the red states are mad because the people in the blue states are mean to them and want them to pay money for roads and schools instead of cool things like NASCAR and shotguns.  Also, there's something about ketchup in there.

Insecure

#2
Um... expect I know whats going on.  Its doing what I did a search for... its sending an email WITH an attachment, which there isn't a whole lot of documentation on.  I changed the source just enough to list the variables in there instead of the information being built in.   And the reason I used the source... and trust me I am around here a enough just don't post very often is because and I quote some people on the forums.  "Why reinvent the wheel."  And I do believe the question was does anyone have a solution, not have a comment to make w/o giving any ideas.  I'm sorry but there is no reason to say something unless your willing to help.

Not to mention the documentation on the source isn't that bad.  Its pretty decent to understand and my problem doesn't lie with the source ... it lies in the fact that I can't use the source in anything but a php file that you must view in the browser to get it to work, which doesn't make sense to me.

rabbit

It doesn't make sense because you have no idea what the script is DOING.  You know what the end result is, but you don't have a damned clue how it gets there.  Learn PHP, then revisit this script.
Grif: Yeah, and the people in the red states are mad because the people in the blue states are mean to them and want them to pay money for roads and schools instead of cool things like NASCAR and shotguns.  Also, there's something about ketchup in there.

Insecure

How the hell are you going to tell me that I don't know PHP?  I have only been working with it for years?  You need to keep your mouth shut when you don't know what your talking about.  Quiz me on any part of the code and I will respond when I get home  from work....

CrAzY

Insecure,

First off, your name 'Insecure' is making me afraid of dealing with you and emails, but I guess thats life ;).

Since I'm not a dick like rabbit, I think I may know your problem. 

Since you are putting this page on your website, maybe you are including this page onto another page.  For example: are you using index.php?page=emailpage?  If so, you probably are trying to include vars.php or whatever it is called more than once.  This can create complications. 

Another debugger I've had to tackle is having a function declared in two different places.  In PHP i don't believe there is any over riding, so make sure that the function or whatever you are declaring is only stated once!  Dont have something included on two pages where one of those pages includes the second page.

Sorry if this doesn't help but I'm just trying to give you some insight.

PS: rabbit, your a dick.

Tim
CrAzY

rabbit

CrAzY, I'm a dick.

That aside, he doesn't know how the script works, and he's still trying to use it.  You don't know how it works either, so stop trying to help him (your "fix" wouldn't work anyway).
Grif: Yeah, and the people in the red states are mad because the people in the blue states are mean to them and want them to pay money for roads and schools instead of cool things like NASCAR and shotguns.  Also, there's something about ketchup in there.

CrAzY

Rabbit,

I didn't even read his script because I don't want to waste my time.  I was just talking about errors that sound similar to what have happened to me in the past.  And if I'm not mistaken, I think I taught you VB or PHP 3-5 years ago.  Don't walk around acting like you own the place and thinking you are "god" of programming.  The purpose of a Forum is to share discussions about topics in order to help and inform somebody.  Not to tell people they are ignorant for trying to learn something.  Everybody has a different style of learning, that is what makes people unique.  Not everybody is as old as you and has as much experience.  Something that this kid has that you don't is maturity.

Besides, even if my answer isn't correct, it doesn't hurt to attempt to answer a question.
CrAzY

Invert


rabbit

Quote from: CrAzY on July 03, 2007, 09:57 AMAnd if I'm not mistaken, I think I taught you VB or PHP 3-5 years ago.
Rofl :P  You are mistaken.  I tought myself all the programming I know.
Grif: Yeah, and the people in the red states are mad because the people in the blue states are mean to them and want them to pay money for roads and schools instead of cool things like NASCAR and shotguns.  Also, there's something about ketchup in there.

CrAzY

Quote from: rabbit on July 03, 2007, 01:14 PM
Quote from: CrAzY on July 03, 2007, 09:57 AMAnd if I'm not mistaken, I think I taught you VB or PHP 3-5 years ago.
Rofl :P  You are mistaken.  I tought myself all the programming I know.
My point exactly.
CrAzY

rabbit

#11
How is that your point at all?  You said, and I quote
QuoteI think I <verb> you <a couple possibilities> <some indeterminate amount of time> ago maybe.
Another way of displaying what you said is
Quote<Outrageous claim that is a blatant and outright lie>.

If your point is that you're wrong, then yeah, that's your point.  Otherwise, you're wrong.

[edit]
Please stop trolling you jackass.
Grif: Yeah, and the people in the red states are mad because the people in the blue states are mean to them and want them to pay money for roads and schools instead of cool things like NASCAR and shotguns.  Also, there's something about ketchup in there.

CrAzY

#12
My point was you're immature.  Even if I was wrong about anything, doesn't change the fact that I'm not criticizing him.
CrAzY

rabbit

Quote from: CrAzY on July 16, 2007, 12:42 PM
My point was you're immature.  Even if I was wrong about anything, doesn't change the fact that I'm not criticizing him.
You never said that was your point, thus it wasn't your point.  And criticism is useful.  You're not helping him by telling him something that might fix a script, even though he doesn't know how the script works.
Grif: Yeah, and the people in the red states are mad because the people in the blue states are mean to them and want them to pay money for roads and schools instead of cool things like NASCAR and shotguns.  Also, there's something about ketchup in there.

Insecure

Rabbit, can you explain to me why you think I don't know anything about PHP?  Or are you simply stating that I don't know anything about that script?  If you are saying I don't know anything about that script you would be a genius.  Want to know why?  ... Well it might be because I openly admitted to finding the source on a website?  I have seen, I believe it was Invert state on this forum a couple times along with others (sorry if it wasn't him exactly...) "Why reinvent the wheel?"  Why do I HAVE to create something that is already out there?

If your simply just stating why ask questions unless I know the script.  Well, I know it enough to get it working, somewhat.  But why would I have a question if I knew the answer already?  In your logic there would be no room to "learn", because there would be no room to ask questions.

So, why are you criticizing me for asking a question?