Trouble getting my file attachment to work with Swiftmailer

Swift Mailer is a fantastic library for sending email with php. Discuss this library or ask any questions about it here.

Moderators: Chris Corbyn, General Moderators

Post Reply
Forum Newbie
Posts: 3
Joined: Thu Dec 29, 2011 9:54 am

Trouble getting my file attachment to work with Swiftmailer

Post by erhilse »

I just started using Swift Mailer and I've ran into a problem that I can't seem to figure out so maybe someone can help. I have a form that takes all the info and puts it into a pdf and then uses swift mailer to attach the pdf and email it. I got that part to work. I needed to add a upload button to my form so the user could upload either a jpeg, png, pdf, doc, or gif and I just want to attach that to the email with the pdf. I can get it to attach but everytime I get the email It says its corrupted. I read somewhere that something called magicquotes could be causing it but I dont know what that is. Can anyone help?

Code: Select all

if (!empty($_POST)) {

	$success = $error = false;

	$post = new stdClass;
	$file = $_FILES["attachment"]["tmp_name"]; //"attachment" is the name of your input field, "tmp_name" gets the temporary path to the uploaded file.
	$filename = $_FILES["attachment"]["name"]; //"name" gets the filename of the uploaded file.			
	foreach ($_POST as $key => $val)
		$post->$key = trim(strip_tags($_POST[$key]));
	if (empty($post->name) OR empty($post->email))
		$error = true;
	else {
		$dir = dirname(__FILE__);
		$pdf_html = ob_get_contents();
		$dompdf = new DOMPDF();
		$pdf_content = $dompdf->output();
		$html_message = ob_get_contents();

		$mailer = new Swift_Mailer(new Swift_MailTransport());

		$message = Swift_Message::newInstance()
				       ->setSubject('Order Entry') // Message subject
					   ->setTo(array('' => 'Eric Hilse')) // Array of people to send to
					   ->setFrom(array('' => 'PAi Order Entry'))
					   ->setBody($html_message, 'text/html') // Attach that HTML message from earlier
					   ->attach(Swift_Attachment::newInstance($pdf_content, 'design.pdf', 'application/pdf')) // Attach the generated PDF from earlier
					   ->attach(Swift_Attachment::newInstance ($file, $filename));
		// Send the email, and show user message
		if ($mailer->send($message))
			$success = true;
			$error = true;


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
	<title>PAi Cap & Tee Art Pack Order Form</title>
	<link href="css/style.css" rel="stylesheet" type="text/css" />


	<div id="container">
				<div id="logo">
					<img src="images/pailogo.png" id="logo" />
			<h1><abbr title="Paramount Apparel International">PAi</abbr> Cap & Tee Art Pack Order Form</h1>
			<?php if ($success) { ?>
				<div class="message success">
					<h4>Congratulations! It worked! Your order has been sent.</h4>
			<?php } elseif ($error) { ?>
				<div class="message error">
					<h4>Sorry, an error occurred. Try again!</h4>
			<?php } ?>
			<form method="post" action="" enctype="multipart/form-data">
					<legend>Please fill in the following form:</legend>
						<label for="name">Your Name:<span class="required">*</span></label>
						<input type="text" name="name" id="name" class="input" />
						<label for="email">Your Email:<span class="required">*</span></label>
						<input type="text" name="email" id="email" class="input" />		
			<br />
						<label for="artpacknumber">Art Pack Number:<span class="required">*</span></label>
						<input type="text" name="artpacknumber" id="artpacknumber" class="input" />			
						<label for="language">New Or Revise?<span class="required">*</span></label>
						<select name="language" id="language">
							<option value="NEW">New</option>
							<option value="REVISE">Revise</option>
			<br />	
						<label for="duedate">Due Date:<span class="required">*</span></label>
						<input type="text" name="duedate" id="duedate" class="input" />
						<label for="dateentered">Date Entered:<span class="required">*</span></label>
						<input type="text" name="dateentered" id="dateentered" class="input" />
			<br />
						<label for="designname">Design Name:<span class="required">*</span></label>
						<input type="text" name="designname" id="designname" class="input" />
			<br />
						<label for="customer">Customer:<span class="required">*</span></label>
						<input type="text" name="customer" id="customer" class="input" />
						<label for="account">Account:<span class="required">*</span></label>
						<input type="text" name="account" id="account" class="input" />
			<br />
						<label for="salesrep">Sales Rep:<span class="required">*</span></label>
						<input type="text" name="salesrep" id="salesrep" class="input" />
						<label for="extension">Extension:<span class="required">*</span></label>
						<input type="text" name="extension" id="extension" class="input" />
			<br />
						<label class="commentslabel" for="comments">Comments:<span class="required">*</span></label>
						<textarea name="comments" id="comments" rows="4" cols="40"></textarea>
			<br />
						<label for="attachment">File Upload<br />.</label>
						<input name="attachment" id="attachment" type="file">
			<br />			
						<input type="submit" class="submit" id="submit" value="Submit" />

Post Reply