Skip to content

Instantly share code, notes, and snippets.

@faridjame
Created September 18, 2012 18:04
Show Gist options
  • Save faridjame/3744706 to your computer and use it in GitHub Desktop.
Save faridjame/3744706 to your computer and use it in GitHub Desktop.
PHP: send multi attachment emails with swiftmailer
<?php
require_once('swift/lib/swift_required.php');
$email = $_POST['email'];
$subject = "my subject";
$user_message = "<b>message body</b>";
$mailer = new Swift_Mailer(new Swift_MailTransport());
$message = Swift_Message::newInstance();
$message->setSubject($subject);
$message->setFrom(array($email));
$message->setTo(array('[email protected]'));
$message->setCc(array('[email protected]'));
$message->setBody($user_message, 'text/html');
for ($i=0; $i < count($_FILES['attachment']); $i++) {
$target_path = "uploads/" . basename($_FILES['attachment']['name'][$i]);
if( move_uploaded_file($_FILES['attachment']['tmp_name'][$i], $target_path) ) {
$message->attach(Swift_Attachment::fromPath($target_path));
//if we don't want to keep the image
unlink($target_path);
}
}
$mailer->send($message);
header("location:thank_you.html");
?>
@torressam333
Copy link

Is there a way to select one image/file at a time in Swiftmailer? i.e. Open the File Upload/Windows Explorer and double click the file then re-open the File Upload/Windows Explorer and select another file, thus appending to the array of attachments?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment