Mail is not sending with attachment

Hi, I have created a form, it works fine if i not attach a file in the same same script. It is not sending mail with attachment.

with attaching. the html code i used is:

<form id="imObjectForm_1" action="/imEmailForm.php" method="post" enctype="multipart/form-data" style="width: 464px; margin: 0; padding: 0; text-align: left;">
						
							<fieldset class="first">
								<div>
									<div><label for="imObjectForm_1_1"> Name: </label><input type="text" class="" id="imObjectForm_1_1" name="imObjectForm_1_1" /></div>
									
								<div></div>
									<div><label for="imObjectForm_1_2" style="width: 92px;">Designation: </label><input type="text" class="" id="imObjectForm_1_2" name="imObjectForm_1_2" /></div>
								<div></div>
									<div><label for="imObjectForm_1_3" style="width: 92px;">Email: </label><input type="text" class="valEmail" id="imObjectForm_1_3" name="imObjectForm_1_3" /></div>
								<div></div>
									<div><label for="imObjectForm_1_4" style="width: 92px;">Subject: </label><input type="text" class="" id="imObjectForm_1_4" name="imObjectForm_1_4" /></div>
								<div></div>
									<div><label for="imObjectForm_1_5" style="width: 92px;">Message: </label><textarea class="" id="imObjectForm_1_5" name="imObjectForm_1_5"></textarea></div>
								<div></div>
									<div>
										<span>
											<span class="checkbox"><input type="checkbox" class="" style="vertical-align: middle;" id="imObjectForm_1_6_0" name="imObjectForm_1_6[]" value="Acetone" />&nbsp;Acetone</span><br />
											<span class="checkbox"><input type="checkbox" class="" style="vertical-align: middle;" id="imObjectForm_1_6_1" name="imObjectForm_1_6[]" value="Acrylonitrile" />&nbsp;Acrylonitrile</span><br />
											<span class="checkbox"><input type="checkbox" class="" style="vertical-align: middle;" id="imObjectForm_1_6_2" name="imObjectForm_1_6[]" value="Cyclohenanone" />&nbsp;Cyclohenanone</span><br />
										</span>
									</div>
						<div></div>
									<div><label for="imObjectForm_1_7">Attachement</label><input type="file" class="" id="imObjectForm_1_7" name="imObjectForm_1_7" /></div>
								</div>
							</fieldset>
							
							<div style="width: 464px; text-align: center;">
								<input type="submit" value="Send" />
								<input type="reset" value="Reset" />
							</div>
						</form>

Validation code is:

<?php

	class imEMail {
		var $from;
		var $to;
		var $subject;
		var $charset;
		var $text;
		var $html;
		
		var $attachments;
		
		function imEMail($from,$to,$subject,$charset) {
			$this->from = $from;
			$this->to = $to;
			$this->subject = $subject;
			$this->charset = $charset;
		}
		
		function setFrom($from) {
			$this->from = $from;
		}
		
		function setTo($to) {
			$this->to = $to;
		}
		
		function setSubject($subject) {
			$this->subject = $subject;
		}
		
		function setCharset($charset) {
			$this->charset = $charset;
		}
		
		function setText($text) {
			$this->text = $text;
		}
		
		function setHTML($html) {
			$this->html = $html;
		}
		
		function attachFile($name,$content,$mime_type) {
			$attachment['name'] = $name;
			$attachment['content'] = base64_encode($content);
			$attachment['mime_type'] = $mime_type;
			$this->attachments[] = $attachment;
		}
		
		function send() {
			$headers = "";
			$msg = "";

			if($this->from == "" || $this->to == "" || ($this->text == "" && $this->html == ""))
				return false;
			
			$boundary_file = md5(time() . "_attachment");
			$boundary_alt = md5(time() . "_alternative");
			
			$headers .= "From: " . $this->from . "\r\n";
			$headers .= "Message-ID: <" . time() . rand(0,9) . rand(0,9) . "@websitex5.users>\r\n";
			$headers .= "X-Mailer: WebSiteX5 Mailer\r\n";
			$headers .= "MIME-Version: 1.0\r\n";

			if(is_array($this->attachments)) {
				$headers .= "Content-Type: multipart/mixed; boundary=\"" . $boundary_file . "\"\r\n\r\n";
				$headers .= "--" . $boundary_file . "\r\n";
			}
			
			if($this->html == "") {
				$headers .= "Content-Type: text/plain; charset=" . strtoupper($this->charset) . "\r\n";
				if (strtolower($this->charset) != "utf-8")
					$headers .= "Content-Transfer-Encoding: 7bit\r\n";
				else
				  $headers .= "Content-Transfer-Encoding: 8bit\r\n";
				$msg .= $this->text . "\r\n\r\n";
			}
			else if($this->text == "") {
				$headers .= "Content-Type: text/html; charset=" . strtoupper($this->charset) . "\r\n";
    		if (strtolower($this->charset) != "utf-8")
					$headers .= "Content-Transfer-Encoding: 7bit\r\n";
        else
				  $headers .= "Content-Transfer-Encoding: 8bit\r\n";
				$msg .= $this->html . "\r\n\r\n";
			}
			else {
				$headers .= "Content-Type: multipart/alternative; boundary=\"" . $boundary_alt . "\"\r\n";
				
				$msg .= "--" .$boundary_alt . "\r\n";
				$msg .= "Content-Type: text/plain; charset=" . strtoupper($this->charset) . "\r\n";
				if (strtolower($this->charset) != "utf-8")
    			$msg .= "Content-Transfer-Encoding: 7bit\r\n";
        else
				  $msg .= "Content-Transfer-Encoding: 8bit\r\n";
				$msg .= "\r\n";
				$msg .= $this->text . "\r\n\r\n";
				
				$msg .= "--" . $boundary_alt . "\r\n";
			  $msg .= "Content-Type: text/html; charset=" . strtoupper($this->charset) . "\r\n";
			  if (strtolower($this->charset) != "utf-8")
					$msg .= "Content-Transfer-Encoding: 7bit\r\n";
        else
				  $msg .= "Content-Transfer-Encoding: 8bit\r\n";
				$msg .= "\r\n";
				$msg .= $this->html . "\r\n\r\n";
				
				$msg .= "--" . $boundary_alt . "--\r\n\r\n";
			}
			
			if(is_array($this->attachments)) {
				foreach($this->attachments as $attachment) {
					$msg .= "--" . $boundary_file . "\r\n";
					$msg .= "Content-Type: " . $attachment["mime_type"] . "; name=\"" . $attachment["name"] . "\"\r\n";
					$msg .= "Content-Transfer-Encoding: base64\r\n";
					$msg .= "Content-Disposition: attachment; filename=\"" . $attachment["name"] . "\"\r\n\r\n";
					$msg .= chunk_split($attachment["content"]) . "\r\n\r\n";
				}
			
				$msg .= "--" . $boundary_file . "--\r\n\r\n";
			}
			
			@ini_set("sendmail_from", $this->from);
			
			$r = @mail($this->to, $this->subject, $msg, $headers, "-f" . $this->from);
			if(!$r) {
				$headers = "To: " . $this->to . "\r\n" . $headers;
				$r = @mail($this->to, $this->subject, $msg, $headers);
			}
			return $r;		
		}
	}

php code for sending mail is:

<?php
	$settings['imEmailForm_0_1'] = array(
		"owner_email_from" => "Email",
		"owner_email_to" => "elango@chemicalweekly.com",
		"customer_email_from" => "elango@chemicalweekly.com",
		"customer_email_to" => "Email",
		"owner_message" => "This is the test mail",
		"customer_message" => "You mail has send successfully",
		"owner_subject" => "Test mail from register using evlo9",
		"customer_subject" => "Success mail",
		"owner_csv" => False,
		"customer_csv" => False,
		"confirmation_page" => "../index.html"
	);

	if(substr(basename($_SERVER['PHP_SELF']), 0, 11) == "imEmailForm") {
		include "../res/x5engine.php";

		$answers = array(
		);

		$form_data = array(
			"Name" => $_POST['imObjectForm_1_1'],
			"Description" => $_POST['imObjectForm_1_2'],
			"Email" => $_POST['imObjectForm_1_3'],
			"Subject" => $_POST['imObjectForm_1_4'],
			"Message" => $_POST['imObjectForm_1_5'],
			"" => $_POST['imObjectForm_1_6']
		);

		$files_data = array(
			"Attachement" => $_FILES['imObjectForm_1_7']
		);

		if(@$_POST['action'] != "check_answer") {
			if(!isset($_POST['imJsCheck']) || $_POST['imJsCheck'] != "jsactive")
				die(imPrintJsError());
			if(isset($_POST['imSpProt']) && $_POST['imSpProt'] != "")
				die(imPrintJsError());
			$email = new imSendEmail();
			$email->sendFormEmail($settings['imEmailForm_0_1'], $form_data, $files_data);
			@header('Location: ' . $settings['imEmailForm_0_1']['confirmation_page']);
		} else {
			if(@$_POST['id'] == "" || @$_POST['answer'] == "" || strtolower(trim($answers[@$_POST['id']])) != strtolower(trim(@$_POST['answer'])))
				echo "0";
			else
				echo "1";
		}
	}

can you guide me where is the problem? and how to solve it.

Regards
Elango

better use an existing mailing library (such as SwiftMailer), they’re battle tested and proven to work (and they come with documentation and examples).

2 Likes

This code

$email = new imSendEmail();
$email->sendFormEmail($settings['imEmailForm_0_1'], $form_data, $files_data);

doesn’t seem to match up with the “validation” code or class definition that you posted.

class imEMail {

and I can’t see any code for the sendFormEmail() function so it’s impossible to trace through to see what might be going wrong. I did wonder if this section

$files_data = array(
  "Attachement" => $_FILES['imObjectForm_1_7']
  );

with the different spelling of “attachment” might be causing the problem.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.