使用 swiftmailer 并发送附件 [英] Using swiftmailer and sending attachments

查看:40
本文介绍了使用 swiftmailer 并发送附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试修改脚本以发送带有附件的电子邮件.它现在可以工作,但所有字段都是必需的,我想知道我缺少什么来制作它,因此不需要这些字段.

I am trying to modify a script to send a email with an attachment. It works right now but all the fields are required and I was wondering what I am missing to make it so the fields are not required.

<?php
if (($_POST)) {

$success = $error = false;

$post = new stdClass;

foreach ($_POST as $key => $val)
    $post->$key = trim(strip_tags($_POST[$key]));

    $dir = dirname(__FILE__);

    ob_start();
    require_once($dir.'/html.php');
    $html_message = ob_get_contents();
    ob_end_clean();

    require_once($dir.'/swift/swift_required.php');

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

    $message = Swift_Message::newInstance()
                   ->setSubject('Imperial Order') // Message subject
                   ->setTo(array($post->email => $post->name, 'ehilse@paifashion.com' => 'Janet McCauley')) // Array of people to send to
                   ->setFrom(array('noreply@paifashion.com' => 'Imperial Order'))
                   ->setBody($html_message, 'text/html') // Attach that HTML message from earlier
                   ->attach(Swift_Attachment::fromPath($_FILES['attachment']['tmp_name'])->setFilename($_FILES['attachment']['name']));

    // Send the email, and show user message
    if ($mailer->send($message))
        $success = true;
    else
        $error = true;      
}
?>

我相信它与 foreach 有关,但如果我尝试将其删除,它会破坏整个代码.如果有人可以提供帮助,那就太好了.我取消验证的原因是因为我想在客户端而不是服务器端进行验证.

I believe it has something to do with the foreach but if I try taking it out it breaks the whole code. If anyone can help that would be great. The reason I am taking out the validation is because i'm wanting to do it on the client side instead of server side.

推荐答案

您可以删除 for each ,但随后您必须单独请求每个发布的字段并一次处理/验证它们,而不是像这样循环循环将所有后变量拉到本地变量.

you can remove the for each but then you'll have to request each posted field individually and process/validate them one at a time instead of in a loop as the loop is pulling all the post vars to local vars.

此外,您不需要将 swift mailer 包含在循环内,它们可以移动到循环外,$mailer = new transport start line 也可以移动到循环外

Also you don't need to require the swift mailer includes etc inside of the loop they can be moved outside of the loop as can the $mailer = new transport start line that can all go outside of the loop

使用示例编辑

if (isset($_POST)) { //BAD BAD BAD way of checking there's better ways

$success = $error = false;

$input1 = $_POST['input1'];
$input2 = $_POST['input2'];
$input3 = $_POST['input3'];
$input4 = $_POST['input4'];

    $dir = dirname(__FILE__);
    // THIS BIT IS RETARDED BUT LEAVING IT IN FOR NOW
    ob_start();
    require_once($dir.'/html.php');
    $html_message = ob_get_contents();
    ob_end_clean();
    // END RETARDED BIT
    require_once($dir.'/swift/swift_required.php');
    $mailer = new Swift_Mailer(new Swift_MailTransport());
    if (isset($_FILES) && strlen($_FILES['attachment']['name']) > 0) { //here we're checking that there is attachments if there are then we're going to do the attach sw code
        $message = Swift_Message::newInstance()
                   ->setSubject('Imperial Order') // Message subject
                   ->setTo(array($input1 => $input2, 'ehilse@paifashion.com' => 'Janet McCauley')) // Array of people to send to
                   ->setFrom(array('noreply@paifashion.com' => 'Imperial Order'))
                   ->setBody($html_message, 'text/html') // Attach that HTML message from earlier
                   ->attach(Swift_Attachment::fromPath($_FILES['attachment']['tmp_name'])->setFilename($_FILES['attachment']['name']));
    } else {
        //non attach sw code
        $message = Swift_Message::newInstance()
                   ->setSubject('Imperial Order') // Message subject
                   ->setTo(array($input1 => $input2, 'ehilse@paifashion.com' => 'Janet McCauley')) // Array of people to send to
                   ->setFrom(array('noreply@paifashion.com' => 'Imperial Order'))
                   ->setBody($html_message, 'text/html') // Attach that HTML message from earlier
    }
    //there's better ways of doing the above but as an example this will suffice.
    // Send the email, and show user message
    if ($mailer->send($message)) {
        $success = true;
    } else {
        $error = true;
    }
}
?>

这篇关于使用 swiftmailer 并发送附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆