douqiandai4327 2014-01-24 19:15
浏览 39

无法使用SwiftMailer发送附件

I'm using swiftmailer to send emails. User is able to pick an attachment. The problem is that even with 1KB attachment, the script is running for more than 30 seconds which causes fatal error. I'm using XAMPP and smtp.gmail.com to send messages. tmp_path seems correct. Here's my code:

require_once '../mailer/lib/swift_required.php';

                        sendMessage();

                        function validate($data)
                        {
                            $data = trim($data);
                            $data = stripslashes($data);
                            $data = htmlspecialchars($data);
                            return $data;
                        }

                        function sendMessage()
                        {
                            $SEND_MAIL_TO = "jaroslawwatroba@wp.pl";

                            $MAILER_FROM = "jarekwatroba@gmail.com";
                            $MAILER_USERNAME = "jarekwatroba@gmail.com";
                            $MAILER_PASSWORD = "***";

                            $MAILER_SMTP = "smtp.gmail.com";
                            $MAILER_SMTP_PORT = 465;
                            $MAILER_SMTP_USE_SSL = true;

                            if (!isset($_POST["sent"]))
                                return;

                            $name = validate($_POST["name_text"]);
                            $email = validate($_POST["email_text"]);
                            $phone = validate($_POST["phone_text"]);
                            $interest = validate($_POST["interest_text"]);
                            $language = validate($_POST["language_text"]);
                            $message = validate($_POST["message_text"]);

                            if (empty($name) || empty($email)) 
                            {
                                echo "<p><strong style=\"color: red\">Please fill in the required fields!</strong></p>";
                                return;
                            }

                            if (!filter_var($email, FILTER_VALIDATE_EMAIL))
                            {
                                echo "<p><strong style=\"color: red\">Please fill in a correct email address!</strong></p>";
                                return;
                            }

                            $message = Swift_Message::newInstance()
                                ->setSubject("Contact by form: ".$name." ".$interest." ".$language)
                                ->setFrom(array($MAILER_FROM))
                                ->setTo(array($SEND_MAIL_TO))
                                ->setBody($message);

                            echo "Path: ".$_FILES["attachment_text"]["tmp_name"];
                            $attachement = Swift_Attachment::fromPath($_FILES["attachment_text"]["tmp_name"], $_FILES["attachment_text"]["type"]);
                            $attachement->setFilename($_FILES["attachment_text"]["name"]);

                            $message->attach($attachement); 

                            if (!$MAILER_SMTP_USE_SSL)
                                $transport = Swift_SmtpTransport::newInstance($MAILER_SMTP, $MAILER_SMTP_PORT);
                            else
                                $transport = Swift_SmtpTransport::newInstance($MAILER_SMTP, $MAILER_SMTP_PORT, 'ssl');

                            $transport->setUsername($MAILER_USERNAME);
                            $transport->setPassword($MAILER_PASSWORD);

                            $mailer = Swift_Mailer::newInstance($transport);
                            $result = $mailer->send($message);

                            if ($result == 1)
                                echo "<p><strong style=\"color: green\">Your message is sent!</strong></p>";
                            else
                                echo "<p><strong style=\"color: green\">Your message wasn't successfully sent...</strong></p>";
                        }
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
    • ¥15 如何处理复杂数据表格的除法运算
    • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
    • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
    • ¥200 uniapp长期运行卡死问题解决
    • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
    • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
    • ¥15 乘性高斯噪声在深度学习网络中的应用
    • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
    • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集