简体   繁体   English

使用PHP编写但不发送电子邮件

[英]Composing but not sending an email using PHP

I am writing a php application which (amongst other things) creates some reports for emailing. 我正在编写一个php应用程序,它(除其他功能外)会创建一些用于发送电子邮件的报告。 I want to enable my customer to click on a "create emails" button which will cause a number of email client windows to open pre filled with data, so that my customer can check the content before sending the emails. 我想让我的客户单击“创建电子邮件”按钮,这将导致打开许多装有数据的电子邮件客户端窗口,以便我的客户可以在发送电子邮件之前检查内容。

I could generate an html page with a number of mailto tags on, which the customer then clicks on to open the emails (for checking) before sending, but this is obviously labour intensive and part of the reason I'm writing this php application. 我可以生成带有许多mailto标记的html页面,然后客户在发送之前单击以打开电子邮件(用于检查),但这显然是劳动密集型的,也是我编写此php应用程序的部分原因。 Is the solution to have some javascript on the generated html page which fires all the mailto links on page load? 解决方案是否在生成的html页面上具有一些javascript,从而触发页面加载时的所有mailto链接? Or is there a better way? 或者,还有更好的方法?

ADDITIONAL EDIT: The key point is that the customer needs to manually check the content before the emails are sent - sorry I have not been clear. 其他编辑:关键是客户需要在发送电子邮件之前手动检查内容-抱歉,我不清楚。 The customer has since specified that he wants the emails to appear as draft pre-composed emails in his email client rather than appearing on a web page. 此后,客户已指定他希望这些电子邮件在其电子邮件客户端中显示为草稿的预先编写的电子邮件,而不是显示在网页上。

Thanks 谢谢

It would be much more versatile and effective to send the mail on the server side, rather than asking the client to rely on their own mail client (when someone clicks a mailto link in the browser, the browser attempts to open a mail client like Outlook or Thunderbird, which may or may not be configured). 在服务器端发送邮件将更加通用和有效,而不是要求客户端依赖自己的邮件客户端(当有人单击浏览器中的mailto链接时,浏览器会尝试打开类似Outlook的邮件客户端或可能未配置的Thunderbird)。 You can print various forms on the page which include subject, to address and message inputs, ingest them via post (ie the user submits the form), and then send the emails from the server directly in response to the form submission. 您可以在页面上打印各种表单,包括主题,地址和消息输入,通过邮寄(例如,用户提交表单)将其提取,然后响应表单提交而直接从服务器发送电子邮件。 Start with this: 从此开始:

http://us3.php.net/manual/en/function.mail.php http://us3.php.net/manual/en/function.mail.php

And check out PHPMailer: 并查看PHPMailer:

https://github.com/PHPMailer/PHPMailer https://github.com/PHPMailer/PHPMailer

Also if you're working with a framework, they've likely got their own utilities for sending mail, which is preferable to the above 2 approaches. 另外,如果您使用的是框架,他们可能会拥有自己的实用程序来发送邮件,这比上述两种方法更可取。 A very simple example of what I'm talking about might look something like this: 我正在谈论的一个非常简单的示例可能看起来像这样:

<form action='/' method='post'>
  To Address: <input type='text' name='to_addr' /> <br/>
  Subject: <input type='text' name='subject' /> <br/>
  Message: <textarea name='to_addr'></textarea> <br/><br/>
  <input type='submit' name='send' value='Send Message' />
</form>
<?php

if(!empty($_POST['send']))
  mail( $_POST['to_addr'] , $_POST['subject'] , $_POST['message'] , 'From: no-reply@somedomain.com');

You can use this piece of code. 您可以使用这段代码。

    $subject = 'Your mail subject';
    $message = 'Your message.';
    $header  = "From: webmaster@yourwebsite.com"."\r\n";
    $header .= "Reply-To: webmaster@yourwebsite.com"."\r\n";
    $header .= "MIME-Version: 1.0"."\r\n";
    $header .= "Content-Type: text/plain; charset=utf-8"."\r\n";
    $header .= "Content-Transfer-Encoding: 8bit"."\r\n";
    $header .= "X-Mailer: PHP v".phpversion();
    mail($newemail, $subject, $message, $header);
    header('Location: '.$success_page);
    exit;

After further investigation, it appears I can't use mailto html tag, as it will not accept html tags within the a href (eg no formatted tables) and is restricted in the number of characters . 经过进一步调查后,我似乎无法使用mailto html标记,因为它不会接受a href中的html标记 (例如,无格式的表格),并且字符数受到限制

So the solution for now is to return to use a php mail form as suggested above, with a cc to the customers mail account so they can store a copy of the email in their email account as they have requested. 因此,目前的解决方案是返回使用上面建议的php邮件表单,并在客户的邮件帐户中使用抄送,以便他们可以根据要求将电子邮件副本存储在其电子邮件帐户中。 Also I shall be using tinymce so that the customer can edit the pre-composed html within the email body. 另外,我将使用tinymce,以便客户可以在电子邮件正文中编辑预先编写的html。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM