通过PHPMailer发送PHP生成的HTML

时间:2015-12-02 16:46:16

标签: php html email phpmailer

我试图设置PHPMailer来发送主要由PHP生成的HTML组成的电子邮件(即用来自数据库,内部函数甚至不同类的数据创建的动态表)。但是,我见过的大多数示例都只有这种方法可以向电子邮件正文添加内容:

$mail->Body = $body;

对于简单的内容和/或字符串而不是动态的PHP内容似乎很容易实现。

是否有某种方法可以通过块或整个PHP页面向电子邮件正文添加内容?这似乎是一个非常愚蠢的问题,但我无法找到办法......

1 个答案:

答案 0 :(得分:1)

您可以生成内容并将其放入缓冲区字符串,然后,您可以使用此设置 PHPMailer 正文

这是一个例子:

package com.test.controller;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.test.forms.AddNewItemForm;
import com.test.forms.SearchForm;
import com.test.models.items.ItemIF;
import com.test.transaction.TransactionFactory;

/**
 * Handles requests for Admin View page
 * @author Trung
 *
 */
@Controller
@RequestMapping (value = "/AdminView")
public class AdminViewController {

    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView adminForm(Model model){
        SearchForm searchForm = new SearchForm();
        model.addAttribute("searchForm", searchForm);

        AddNewItemForm addItemForm = new AddNewItemForm();
        model.addAttribute("addItemForm", addItemForm);

        return new ModelAndView("AdminView");
    }

    @RequestMapping (params = "search", method = RequestMethod.POST)
    public String processingSearchStore(@ModelAttribute("searchForm") SearchForm searchForm, Model model){
        List<ItemIF> relatedItems = null;
        TransactionFactory transaction = new TransactionFactory();
        relatedItems = transaction.retrieveItemByName(searchForm.getKeyWordSearch());

        if (relatedItems.isEmpty()){
            System.out.println("okay, there isn't any item that is matched your criteria");
        } else {
            model.addAttribute("itemList", relatedItems);
        }

        return "AdminView";
    }

    @RequestMapping(params = "add", method = RequestMethod.POST)
    public ModelAndView addNewItemForm(@ModelAttribute("addItemForm") AddNewItemForm addItemForm){
        TransactionFactory transaction = new TransactionFactory();
        String itemName = addItemForm.getItemName();
        String itemLocation = addItemForm.getItemLocation();
        String itemDescription = addItemForm.getItemDescription();

        transaction.insertItem(itemName, itemLocation, itemDescription);

        return new ModelAndView("AdminView");
    }
}