I am trying to email a text file as an attachment from a PHP script using the code from here: http://webcheatsheet.com/php/send_email_text_html_attachment.php#attachment
<?
$subject = 'Requested File';
$random_hash = md5(date('r', time()));
$headers = "From: email@email.com\r\nReply-To: email@email.com";
$headers .= "\r\nContent-Type: myltipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
$attachment = chunk_split(base64_encode(file_get_contents('path/test.txt')));
ob_start();
?>
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Hello World!!!
This is simple text email message.
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>
--PHP-alt-<?php echo $random_hash; ?>--
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: application/zip; name="test.txt"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--
<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
I'm not a PHP developer and have very limited experience using it, I am not getting any errors when this script is executed but I am also not receiving the email...any ideas why? Or what I should be looking at specifically?
Thank you for any tips!
EDIT: As per the comments, I tried using SwiftMailer but I cannot get it to work using this code: $message = Swift_Message::newInstance();
// Give the message a subject
$message->setSubject('Your subject');
// Set the From address with an associative array
$message->setFrom(array('email@email.com' => 'From Name'));
// Set the To addresses with an associative array
$message->setTo(array('email@email.com', 'email@email.com' => 'Name'));
// Give it a body
$message->setBody('Here is the message itself');
// And optionally an alternative body
$message->addPart('<q>Here is the message itself</q>', 'text/html');
// Optionally add any attachments
$message->attach(Swift_Attachment::fromPath('path/test.csv'));
Again, this code executes without any errors but no email is sent...what am I missing?