SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Apr 2007
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    attachment not working

    Hello,
    I am just learning PHP and I need to send an email with a graphic attachment. I have spent hours reading all the posts in the Advanced email in PHP forum and cannot figure out how to solve my problem.
    My users will need to select one of three graphics. Then they will fill out a form with a name and email address. I want the email to be sent with a personal message and the graphic attachment. Everything is working just fine except that the attachment is not showing up.
    The message appears in the email and then an icon of an empty sheet of paper with the words "mime-attachment" below it.
    Somehow, I think my graphic is not getting attached, but the attachment is actually recognized.
    I have done some debugging and the files are getting assigned properly in the switch statement, but they don't seem to be making it to the email.
    I greatly appreciate any help you can give me.

    I'm not sure where the problem is but here is the code for my form:
    Code:
    function inviteForm() {
    print "
    <form method=post action='invite.php?action=check' enctype='multipart/form-data' >
    <h2>Invite a Friend to Fellowship Bible Church</h2>
    <h4>Invite a friend to Fellowship Bible Church!  Choose the invitation you want to send, then fill out the form at the bottom of the page. Be sure to include your personal note in the text box.<br> <br> The graphic and the text to the right of the graphic will be included in the body of your e-mail.</h4>
    	<table width='100%' border=0 cellpadding='0' cellspacing='0'>
    		<tr>
    			<td colspan='6'><strong><big>Please select the E-invitation you would like to send.</big></strong></td>
    		</tr>
    		<tr>
    			<td colspan='4'>&nbsp;</td>
    		</tr>
    		<tr>
    			<td width='5%'><input type='radio' name='card_id' value='1'";if($_POST[card_id]=='1') { print " checked";} print "></td>
    			<td width='30%'><img src='images/bandgraphic.jpg'></td>
    			<td width='5%'>&nbsp;</td>
    			<td width='50%' valign='top'><span class='infoboxheader'>Do you like a drum beat with your worship?<br>
    			</span><span class='text'>Insert text here</span></td>
    		</tr>
    		<tr>
    			<td width='5%'><input type='radio' name='card_id' value='2'";if($_POST[card_id]=='2') { print " checked";} print "></td>
    			<td width='30%'><img src='downloads/teengraphic.jpg'></td>
    			<td width='5%'>&nbsp;</td>
    			<td width='50%' valign='top'><span class='infoboxheader'>Are you passionate about worship?<br>
    			</span><span class='text'>Insert text here</span></td>
    		</tr>
    		<tr>
    			<td width='5%'><input type='radio' name='card_id' value='3'";if($_POST[card_id]=='3') { print " checked";} print "></td>
    			<td width='30%'><img src='downloads/smallgroupgraphic.jpg'></td>
    			<td width='5%'>&nbsp;</td>
    			<td width='50%' valign='top'><span class='infoboxheader'>Are you passionate about worship?<br>
    			</span><span class='text'>Insert text here</span></td>
    		</tr>
    	</table>
    	
    
    	<table id='inviteform' border=0>
    		<tr>
    			<td>Enter your friend's name:<br>
    			<input type=text name='toname' value='$_POST[toname]' tabindex='2' size=25></td>
    		</tr>
    		<tr>
    			<td><br>Enter your friend's e-mail address:<br>
    			<input type=text name='tomail' value='$_POST[tomail]' tabindex='3' size=50></td>
    		</tr>
    		<tr>
    			<td><br><br>Enter your name:<br>
    			<input type=text name='fromname' value='$_POST[fromname]' tabindex='4' size=25></td>
    		</tr>
    		<tr>
    			<td><br>Enter your e-mail address:<br>
    			<input type=text name='frommail' value='$_POST[frommail]' tabindex='5' size=50></td>
    		</tr>
    		<tr>
    			<td><br>Include your personal message.<br>
    			<textarea name='message'  tabindex='6' rows='6' cols='50'>$_POST[message]</textarea></td>
    		</tr>
    		<tr>
    			<td><input type='submit' value='Send E-mail!' tabindex='7'></td>
    		</tr>
    
    	</table>
    </form>";
    }
    Here is the code for the email:

    Code:
    function selectGraphic() {
    	global $invite;
    	switch($_POST[card_id]) {
    		case "1":
    			$invite = 'downloads/bandgraphic.jpg';
    		break;
    		case "2":
    			$invite = 'downloads/teengraphic.jpg';		
    		break;
    		case "3":
    			$invite = 'downloads/smallgroupgraphic.jpg';
    		break;
    		default:
    			$invite = 'downloads/invite1.jpg';
    		break;
    	}
    }
    
    function sendEmail() {
    // Read POST request params into global vars
    $to = $_POST['tomail'];
    $from = $_POST['frommail'];
    $subject = "Invitation to Fellowship Bible Church";
    $message = stripslashes($_POST['message']) . "\nHere is the link to view the Fellowship Bible Church Website. \nhttp://www.fellowship-katy.org";
    
    //Obtain file upload vars
    $fileatt = $_FILES['/home/katy/public_html/$invite']['tmp_name'];
    $fileatt_type = $_FILES['image/jpg']['type'];
    $fileatt_name = $_FILES['$invite']['name'];
    
    
    $headers = "From: ".$from;
    
    //Check the $fileatt variable for path and filename to the attached file.
    if (is_uploaded_file($fileatt)) {
    	//Read the file to be attached ('rb' = read binary)
    	$file = fopen($fileatt, 'rb');
    	$data = fread($file,filesize($fileatt));
    	fclose($file);
    }
    //Now the data for the file attachment is read in so set up message headers for multipart/mixed message
    //Generate a boundary string
    $semi_rand = md5(time());
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
    
    //Add the headers for a file attachment
    $headers .= "\nMIME-Version: 1.0\n" .
    			"Content-Type: multipart/mixed;\n" .
    			" boundary=\"{$mime_boundary}\"";
    
    // Add a multipart boundary above the plain message 
     $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";
    
    //Base64 encode the file data
    $data = chunk_split(base64_encode($data));
    
    //Add file attachment to the message
    $message .= "--{$mime_boundary}\n" .
    			"Content-Type: {$fileatt_type};\n" .
    			" name=\"{$fileatt_name}\"\n" .
    			"Content_Disposition: attachment;\n" .
    			" filename=\"{$fileatt_name}\"\n" .
    			"Content-Transfer-Encoding: base64\n\n" .
    			$data . "\n\n" .
    			"--{$mime_boundary}--\n";
    
    
    //Send the message
    $ok = @mail($to, $subject, $message, $headers);
    if ($ok) {
    	print "<br><font size='medium'><b><em>Thank you.</em>  Your E-mail has been sent!</b></p>
    				<a href='../invite.php'><br><p><input type='submit' value='Send another invitation'></a></p>";
    } else {
    	print "<br><font color='red' font size='medium'><b>Mail could not be sent. Sorry! Please try again. </b><br></font><font size='medium' <a href='../invite.php'><br><p>Return to invitation page</a></p></font>";
    }
    }
    ?>
    Thank you so much

  2. #2
    Resident Code Monkey Chris Corbyn's Avatar
    Join Date
    Nov 2005
    Location
    Melbourne, Australia
    Posts
    713
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there a reason you're going down this incredibly complicated route when you could more reliably use Swift Mailer ?

  3. #3
    SitePoint Member
    Join Date
    Apr 2007
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have never heard of Swift. I had looked at PHPMailer and decided against it. I didn't know there was another option. I will check it out. Thank you for suggesting it.

  4. #4
    SitePoint Member
    Join Date
    Apr 2007
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK...
    I have spent some time looking at other options for sending email attachments, including SwiftMailer. However, I am really trying to learn how PHP works and the only way I will actually learn anything and be able to use it again is if I do it "the incredibly complicated way" and actually learn how all the PHP functions work.
    So... I am still trying to get my attachment to attach and have something in it. The code I have works perfectly if I give it the exact path name and file name in the "fileatt" variable. However, I won't know which file my users will pick. There will be 3 or 4 options. I am using a switch statement to assign the correct graphic to the variable that I pass into the "$fileatt" variable. However, when the "$fileatt" variable gets passed into the functions to open and read the file, I get all kinds of error messages. Can anyone help me understand what is happening?
    Here is the code:
    Code:
    function selectGraphic() {
    global $invite;
    
    	switch($_POST[card_id]) {
    		case "1":
    			$invite = '/home/katy/public_html/downloads/bandgraphic.jpg';
    		break;
    		case "2":
    			$invite = '/home/katy/public_html/downloads/teengraphic.jpg';		
    		break;
    		case "3":
    			$invite = '/home/katy/public_html/downloads/invite3.jpg';
    		break;
    		default:
    			$invite = '/home/katy/public_html/downloads/invite1.jpg';
    		break;
    	}
    }This switch statement returns the correct graphic based on what the user has chosen.
    
    function sendEmail() {
    // Read POST request params into global vars
    $to = $_POST['tomail'];
    $from = $_POST['frommail'];
    $subject = "Invitation to Fellowship Bible Church";
    $message = $_POST['message'] 
    The above array gets all the right values.
    Evrything from here on does not work quite right unless I pass the complete path and file name into $fileatt, but I won't know what filename to use here.  It is user determined.
    //Obtain file upload vars
    $fileatt = substr($invite, 32);
    echo $fileatt;
    $fileatt_type = 'image/jpg';
    $fileatt_name = 'invite_graphic.jpg';
    
    $headers = "From: ".$from;
    
    //Check the $fileatt variable for path and filename to the attached file.
    if (is_uploaded_file($fileatt)) {
    	//Read the file to be attached ('rb' = read binary)
    	$file = fopen($fileatt,'rb');
    	$data = fread($file,filesize($fileatt));
    	fclose($file);
    }
    I put the "echo $fileatt" in because I was trying to see what was actually getting assigned to the $fileatt variable. I don't think anything is actually getting assigned because nothing is echoed back. So somewhere, my $invite variable is not making it to $fileatt.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •