【问题标题】:Sending HTML email, via PHP form通过 PHP 表单发送 HTML 电子邮件
【发布时间】:2011-01-12 06:27:59
【问题描述】:

我正在尝试在我的网站访问者通过 PHP 表单邮件程序出现在我的工作室之前向他们发送一些指示和提示的电子邮件。 (我正在简化一些表单字段)

但是 HTML 格式无法正常工作....我没有正确声明字符集的 mime 类型吗?

<?php 

            if (isset($_POST['submit'])) { 

            //if (empty ($_POST['name']) || empty($_POST['email'])) 
            //{
            //echo"<div class='error'>Error<br />You did not fill in a required field, please review your form and correct the missing information. <a class='close' href='#'>close</a></div>";
            //}

            $name = $_POST['name'];
            $email = $_POST['email'];
            $email2 = $_POST['email2'];
            //A bunch of other fields are here

            //Additional Headers

            $headers  = 'MIME-Version: 1.0' . "\r\n";
            $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";


            //$yoursite = "My Site";
            //$youremail = $email;

            $subject = "Website Form";
            $message = "
            $name would like you to contact them about your service.
            Contact PH:  $phone
            Email:  $email
            Legal Guardian: $legal              
            //more stuff here
            ";


            $subject2 = "Directions and Information";

                            $message2 = "<html><head></head><body>
            $message2 .= "<h1>Directions</h1>

            <p>text</p>

            <p><a href='http://example.com/schedules'>Click here</a>

            <h2>How Do I find your Photo Studio?</h2>

            <h2>What do I have to bring with me?</h2>
            </p>";

            $message2 .= "</body></html>";



            $email3 = "me@mysite.com";
            $email4 = "mysite@gmail.com";




            //This email sends their details to me from the visitor
            mail($email3, $subject, $message, "From: $email");


                            //This email sends directions to the visitor from me
            mail($email, $subject2, $message2, "From: $email4");

            echo"<div class='thankyou'>Thank you for contacting us,<br /> we will respond as soon as we can.</div>";


            }
            ?>

【问题讨论】:

  • 您甚至没有使用 $headers 变量。另外,你有一个无与伦比的双引号 $message2 = "&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt; 我建议使用其中一个很好的免费邮件程序类,例如 PHP Mailer、Swift Mailer。

标签: php


【解决方案1】:

需要对电子邮件进行大量随机垃圾处理才能正确发送。我通常倾向于将所有责任外包给一个存在于野外的预打包类,比如http://swiftmailer.org/

也许其他人会提供更好的课程。

【讨论】:

  • 这看起来比我想要做的更易于管理。问题...在 ->setFrom(array('me@mysite.com')) 变量中,如何设置它以提取在“电子邮件”字段中输入的数据?
  • ->setFrom(array($_POST['email'])) 应该可以解决问题......除非我误解了你。也可以只做 ->setFrom($_POST['email']),而不使用数组。但是,在尝试将发布的电子邮件插入这样的对象之前,将其验证为有效的电子邮件地址可能是个好主意。
  • 我确实已经验证过了,谢谢...我会尝试两种方式,看看我得到了什么。
【解决方案2】:

我对 PEAR Mail_Mime 包发誓。它简单而强大。

PEAR: Mail_Mime

//Basic mail headers
$headers['To'] = "test@domain.com";
$headers['From'] = "sender@domain.com";
$headers['Subject'] = "Test";

//Set up the mail module
$mime = new Mail_mime("\r\n");
$mime->setTXTBody("This is a test");
$mime->setHTMLBody("<p>This is a test.</p>");
$body = $mime->get();
$headers = $mime->headers($headers);

//Send the message via SMTP
$mail_obj =& Mail::factory('smtp', array('host' => 'mail.domain.com', 'port' => 25));
$mail_obj->send($headers['To'], $headers, $body);

【讨论】:

    猜你喜欢
    • 2016-03-10
    • 1970-01-01
    • 2014-02-12
    • 2019-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-29
    相关资源
    最近更新 更多