Author |
Message |
benny_tllh
Hangin' Around

Joined: Dec 29, 2006
Posts: 31
|
Posted:
Mon Aug 17, 2009 12:35 am |
|
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 ? |
|
|
|
 |
gazj
Worker


Joined: Apr 28, 2006
Posts: 152
Location: doncaster england
|
Posted:
Tue Aug 18, 2009 9:32 am |
|
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: webmaster@example.com\r\nReply-To: webmaster@example.com";
//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";
?>
|
|
|
|
|
 |
benny_tllh

|
Posted:
Thu Aug 20, 2009 12:16 am |
|
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
|
Posted:
Thu Aug 20, 2009 6:00 am |
|
The owner of the above site is a Mod here under the username Jestrella so hopefully he will see this post and help.  |
|
|
|
 |
benny_tllh

|
Posted:
Thu Aug 20, 2009 6:59 am |
|
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

|
Posted:
Thu Aug 20, 2009 4:01 pm |
|
it was the first basic example i found i thought you could fill in the blanks for what you need glad it helped  |
|
|
|
 |
benny_tllh

|
Posted:
Sun Aug 23, 2009 2:48 pm |
|
Hmm. something must be missing, mail failed.
and i tried google, the script is not slaytanic but standard php http://www.webcheatsheet.com/PHP/send_email_text_html_attachment.php
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: webmaster@example.com\r\nReply-To: webmaster@example.com";
//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

|
Posted:
Sun Aug 23, 2009 7:11 pm |
|
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

|
Posted:
Mon Aug 24, 2009 1:29 pm |
|
Thanks gazj , 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  |
|
|
|
 |
eldorado
Involved


Joined: Sep 10, 2008
Posts: 424
Location: France,Translator
|
Posted:
Mon Aug 24, 2009 2:39 pm |
|
@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! |
|
|
 |
|