Ravens PHP Scripts: Forums
 

 

View next topic
View previous topic
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> Modules
Author Message
benny_tllh
Hangin' Around


Joined: Dec 29, 2006
Posts: 31

PostPosted: Mon Aug 17, 2009 12:35 am Reply with quote

Im using phpnuke as intranet, and has verified it can send mails. My problem is how to send a mail with attachments as should be possible with phpnukemailer.

Anyone have a "small" example of how it's done ?
 
View user's profile Send private message
gazj
Worker
Worker


Joined: Apr 28, 2006
Posts: 152
Location: doncaster england

PostPosted: Tue Aug 18, 2009 9:32 am Reply with quote

heres an example i pull from the net i have no idea if it works

Code:
<?php

//define the receiver of the email
$to = 'youraddress@example.com';
//define the subject of the email
$subject = 'Test email with attachment';
//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: Only registered users can see links on this board! Get registered or login!\r\nReply-To: Only registered users can see links on this board! Get registered or login!";
//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.zip')));
//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

Hello World!!!
This is simple text email message.

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

<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>

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

--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: application/zip; name="attachment.zip"
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";
?>
 
View user's profile Send private message Visit poster's website
benny_tllh
PostPosted: Thu Aug 20, 2009 12:16 am Reply with quote

Thanks for the suggestion, have tried some googled examples, but I couldn't get them to work. Suspect i might miss something basic, and seeing build-in mail works, thought it was easier to get the slaytanic phpnukemailer (built-in mailer) to work, than a full script I would have to adapt to nuke. (notes in the phpnukemailer suggests it can work with attachments but no example and the slaytanic site is kind of forum dead)

Will give your example a try in the weekend.
 
jakec
Site Admin


Joined: Feb 06, 2006
Posts: 3048
Location: United Kingdom

PostPosted: Thu Aug 20, 2009 6:00 am Reply with quote

The owner of the above site is a Mod here under the username Jestrella so hopefully he will see this post and help. Wink
 
View user's profile Send private message
benny_tllh
PostPosted: Thu Aug 20, 2009 6:59 am Reply with quote

Very Happy Have looked it over and seems like its what I need to figure it out, a simple example prefilled and ready to send. (only need to mod. too and from + supply a test attachment.zip)

Think I went wrong in the mime / encode stuff on my prev. attempts.
 
gazj
PostPosted: Thu Aug 20, 2009 4:01 pm Reply with quote

it was the first basic example i found i thought you could fill in the blanks for what you need glad it helped Very Happy
 
benny_tllh
PostPosted: Sun Aug 23, 2009 2:48 pm Reply with quote

Hmm. something must be missing, mail failed.Rolling Eyes

and i tried google, the script is not slaytanic but standard php Only registered users can see links on this board! Get registered or login!

Even their simple form fails. have tried with phpnukemailer active and deactivated.

Code:
<?php

//define the receiver of the email
$to = 'youraddress@example.com';
//define the subject of the email
$subject = 'Test email';
//define the message to be sent. Each line should be separated with \n
$message = "Hello World!\n\nThis is my first mail.";
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: Only registered users can see links on this board! Get registered or login!\r\nReply-To: Only registered users can see links on this board! Get registered or login!";
//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";
?>
 
gazj
PostPosted: Sun Aug 23, 2009 7:11 pm Reply with quote

well i have just tested this as a standalone and it works fine just edit the code to include the passed variables from the contact form you cant upload with it its only the sending of the email not the form its self

Code:
<?php

// array with filenames to be sent as attachment
$files = array("example_please_change.zip");
// email fields: to, from, subject, and so on
$to = "me@you.com";
$from = "name";
$subject ="TEST";
$message = "My message";
$headers = "From: $from";
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// multipart boundary
$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
$message .= "--{$mime_boundary}\n";
// preparing attachments
for($x=0;$x<count($files);$x++){
$file = fopen($files[$x],"rb");
$data = fread($file,filesize($files[$x]));
fclose($file);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$message .= "--{$mime_boundary}\n";
}
// send
$ok = @mail($to, $subject, $message, $headers);
if ($ok) {
echo "<p>mail sent to $to!</p>";
} else {
echo "<p>mail could not be sent!</p>";
}
?>
 
benny_tllh
PostPosted: Mon Aug 24, 2009 1:29 pm Reply with quote

Thanks gazj worship , by providing a tested script i could rule-out coding and found php.ini wasn't set. I missed that because html_newsletter could send mails, so i thought server was not the problem.

Took some time to realise php.ini also exists in apache/bin and needs til be edited there not in the php folder. (why 2 php.ini ?)

ps. good advice is
1: turn error reporting on in config.php (doh !)
2: use simple : $mail_sent = mail($to , $subject , $message );echo $mail_sent ? "Mail sent" : "Mail failed";

only this simple form yielded an error all other larger "attachment" scripts just failed w/o explanation


yay this problem has driven me insane over a period of time, and then it was so simple. Now i just need to clean up the bombed php file i filled with tries Smile
 
eldorado
Involved
Involved


Joined: Sep 10, 2008
Posts: 424
Location: France,Translator

PostPosted: Mon Aug 24, 2009 2:39 pm Reply with quote

@Offtopic .. just a stupid question , but whats the point of using a "ternaire" condition check when an if and elseif can do the trick? (which incidently makes the code readable). Aren't php authors suppose to remove redundant functions?

_________________
Only registered users can see links on this board! Get registered or login! (My RN site)- Only registered users can see links on this board! Get registered or login!(cod4 clan) - Only registered users can see links on this board! Get registered or login! 
View user's profile Send private message Visit poster's website MSN Messenger
Display posts from previous:       
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> Modules

View next topic
View previous topic
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB © 2001-2007 phpBB Group
All times are GMT - 6 Hours
 
Forums ©