Page 1 of 1

PHP Tutorial: Email File Attachments [part 02]

Posted: Mon Mar 12, 2012 7:32 pm
by benjaminitk
I'd like to start just by saying thanks for making the tutorials, they're so easy to follow.

Whilst attempting this tutorial, I've got to the end and I have the following error message on the mail.inc.php script :
Parse error: syntax error, unexpected '(', expecting ')' in C:\xampp\htdocs\core\inc\mail.inc.php on line 11

The code I've wrote is as follows;
<?php

//sends the given message to the emil with fiel attatchement
function mail_file($to, $from, $subject, $body, $file){
	
	$boundary = md5(rand());	
	
	$headers = array(
	'MIME-VERSION: 1.0',
	"From {$from}",
	"Content-Type: multipart/mixed; boundary="($boundary)" ");
	
	// 8bit works better? 
	$message = array(
	"--{$boundary}",
	"Content Type: text/plain: charset utf-8",
	"Content-Transfer-Encoding: 7bit",
	"",
	chunk_split($body),
	
	"--{$boundary}",
	"Content-Type: {$file['type']}; name="{$file['name']}"; ",
	"Content-Disposition: attachment; filename="{$file['name']}"; ",
	"Content-Transfer-Encoding: base64",
	"",
	chunk_split(base64_encode(file_get_contents($file['path']))), 		
	"--{$boundary}--",		

	);
	
	return mail($to,$subject, $message, implode("\r\n", $message), implode("\r\n", $headers));
}

?>

Help would be greatly appreciated!

Re: PHP Tutorial: Email File Attachments [part 02]

Posted: Mon Mar 12, 2012 8:16 pm
by jacek
On this line
Content-Type: multipart/mixed; boundary="($boundary)" ");
The quotes for the boundary need to b e escaped, so it should be
Content-Type: multipart/mixed; boundary=\"($boundary)\" ");
Also the brackets around the variable should be { and } not ( and )

Re: PHP Tutorial: Email File Attachments [part 02]

Posted: Mon Mar 12, 2012 9:19 pm
by benjaminitk
Thanks for the quick reply, I was just looking into all sorts of solutions on the net, some awkward techniques using PEAR.

That's gotten rid of the error, thanks there's one more still..
Warning: mail() [function.mail]: "sendmail_from" not set in php.ini or custom "From:" header missing in C:\xampp\htdocs\core\inc\mail.inc.php on line 31
I've changed the php.ini file to my address, do i need to restart my server to check this works ok?

The mail.inc.php code is as follows;

<?php

//sends the given message to the emil with fiel attatchement
function mail_file($to, $from, $subject, $body, $file){
	
	$boundary = md5(rand());	
	
	$headers = array(
	'MIME-VERSION: 1.0',
	"From {$from}",
	"Content-Type: multipart/mixed; boundary=\"{$boundary}\" ");
	
	// 8bit works better? 
	$message = array(
	"--{$boundary}",
	"Content Type: text/plain: charset utf-8",
	"Content-Transfer-Encoding: 7bit",
	"",
	chunk_split($body),
	
	"--{$boundary}",
	"Content-Type: {$file['type']}; name=\"{$file['name']}\"; ",
	"Content-Disposition: attachment; filename=\"{$file['name']}\"; ",
	"Content-Transfer-Encoding: base64",
	"",
	chunk_split(base64_encode(file_get_contents($file['path']))), 		
	"--{$boundary}--",		

	);
	
	return mail($to, $subject, implode("\r\n", $message), implode("\r\n", $headers));
}

?>

Re: PHP Tutorial: Email File Attachments [part 02]

Posted: Mon Mar 12, 2012 9:50 pm
by Temor
Try adding semicolons next to from, like this:
 "From: {$from}",

Re: PHP Tutorial: Email File Attachments [part 02]

Posted: Mon Mar 12, 2012 9:54 pm
by benjaminitk
Ahh thanks. It looks like I need to set my "SMTP settings in my ini file -
Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\xampp\htdocs\core\inc\mail.inc.php on line 31
having a nightmare coding today! : @!

Re: PHP Tutorial: Email File Attachments [part 02]

Posted: Tue Mar 13, 2012 12:10 am
by benjaminitk
OK now I'm really lost and confused. I've downloaded XAMPP package for windows, so now using this tutorial I should be able to email people using these scripts. I can use my localhost to set up and send emails to different accounts can't I? Or am I breaking down : @?

I have Mercury, this will allow me to send emails to hotmail and google mail?

Re: PHP Tutorial: Email File Attachments [part 02]

Posted: Wed Mar 14, 2012 5:04 pm
by jacek
benjaminitk wrote:I have Mercury, this will allow me to send emails to hotmail and google mail?
They will probably block it since the host of the sender would be a residential IP and not be fully set up to respond to the verification they will try to do.