PHP Tutorial: Email File Attachments [part 02]

Post here is you are having problems with any of the tutorials.
Post Reply
benjaminitk
Posts: 5
Joined: Mon Mar 12, 2012 7:14 pm

PHP Tutorial: Email File Attachments [part 02]

Post 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!
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

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

Post 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 )
Image
benjaminitk
Posts: 5
Joined: Mon Mar 12, 2012 7:14 pm

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

Post 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));
}

?>
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

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

Post by Temor »

Try adding semicolons next to from, like this:
 "From: {$from}",
benjaminitk
Posts: 5
Joined: Mon Mar 12, 2012 7:14 pm

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

Post 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! : @!
benjaminitk
Posts: 5
Joined: Mon Mar 12, 2012 7:14 pm

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

Post 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?
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

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

Post 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.
Image
Post Reply