简体   繁体   中英

Composing but not sending an email using PHP

I am writing a php application which (amongst other things) creates some reports for emailing. 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. Is the solution to have some javascript on the generated html page which fires all the mailto links on page load? 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). 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

And check out 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 .

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. Also I shall be using tinymce so that the customer can edit the pre-composed html within the email body.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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