问题
Im really stuck with this swiftmail method of sending email with attachment. My emails never seem to be delivered. I send the email and it just ruturns without any errors but when I check my mail nothing is delivered. Please help! I troubleshooted everything and everything works except for the attach() function. I dont know whats wrong. Heres my code.
<?php
//I didnt add my validations and variables above.....
require_once('./swiftmailer/lib/swift_required.php');
$transport = Swift_SmtpTransport::newInstance('smtp.host.com', 25)
->setUsername('user')
->setPassword('pass');
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance()
->setSubject('Online Form')
->setFrom(array($from_email => $full_name))
->setTo(array('email@mail.com' => 'Jack'))
->setBody(''.$message_temp.'')
->attach(Swift_Attachment::fromPath($_FILES['attachment']['tmp_name'])
->setFilename($_FILES['attachment']['name']));
$result = $mailer->send($message);
?>
回答1:
Oops! Never mind, I solved it myslef.
I assigned $_FILES['attachment']['tmp_name'] to a temporary variable and it worked!
Dont know why but that solved it for me.
Here's my code;
// Swiftmail commands ====================================
require_once('./swiftmailer/lib/swift_required.php');
$transport = Swift_SmtpTransport::newInstance('smtp.host.com', 587)
->setUsername('email@host.com')
->setPassword('pass');
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance()
->setSubject($subject_temp)
->setFrom(array($from_email => $full_name))
->setTo(array('email@host.com' => 'Jack'))
->setBody($message_temp)
->attach(Swift_Attachment::fromPath($file_temp_name)
->setFilename($name_of_file));
$result = $mailer->send($message);
// Swiftmail commands ====================================
Where $file_temp_name = $_FILES['attachment']['tmp_name']; and $name_of_file = basename($_FILES['attachment']['name']);
来源:https://stackoverflow.com/questions/23842615/swiftmail-not-sending-attachment-files-tmp