为thunderbird / Outlook /准备电子邮件

Is it possible to prepare an email (From, To, Subject, Body, Attachements) in PHP and, instead of directly sending it with PHP, open it with the client's email program (Thunderbird / Outlook / ...)?

My context is :

  • in a form, the user select the "To" contact and a file to send (the body is a predefined text)
  • on submit, I would like the prepared email to be displayed in his favorite mail client, so that he can modify it (add CC, modify body, ...)

If possible, how to do that?

Thanks all for your answers.

Unfortunately (and actually quite unsurprisingly), it is not possible to put attachement data in a mailto link (duh)!

2 solutions then :

  1. If you really need to open the client's email software, then prepare a mailto with a direct link to the file in the message body. For that, you need to store the file in a place accessible by the recipient vie that direct link.
  2. If you can't store the file in a place accessible by the recipient (for instance your app is a intranet app behind a firewall), then the only solution is to send the email via PHP directly, without using the client's software.
  3. Or maybe you could send the mail to the sender first via PHP, and he would forward it to the recipient himself, so he could modify the message at will. But that's a bit confusing for non-trained users...

Cheers!

The mailto: URL scheme shows how you can create a link to a new email. The browser/OS will open this email in the email client.

http://tools.ietf.org/html/rfc2368

I would do something like this:

  • Gather the information from the form
  • Handle the data when the form is submitted
  • Present the user with a link, and add the variables using the mailto: scheme.

For example, something like this in PHP:

if(isset($_POST['send_mail']))
{
    $to = $_POST['email_to'];
    $subject = $_POST['email_subject'];
    $body = 'This would be your defined body...';

    // Now prepare the URL and present it to the user:
    $url = 'mailto:'.$to.'?subject='.rawurlencode($subject).'&body='.rawurlencode($body);
    echo '<a href="'.$url.'" title="Send Email Now">Send Email</a>';

    // A boolean value to hide the form
    // Necessary logic would need to be implemented on the page for this
    $show_form = false;
}

Your form might look like this:

<form method="post">
    <label for="select_email_to">Recipient:</label>
    <select name="email_to" id="select_email_to">
        <option value="someone@example.com">John Doe</option>
        <option value="someone.else@example.com">Jane Doe</option>
        <otion value="a.n.other@example.com">Foo Bar</otion>
    </select>
    <label for="input_subject">Subject</label>
    <input type="text" name="subject" id="input_subject" />

    <input type="submit" name="send_mail" value="Prepare Email" />
</form>

If you need to include a file, I'd upload it to the server when the form is submitted, and then include a link to the uploaded file in the body of the email.