tags:

views:

86

answers:

4

I'm trying to generate email messages. The recipient's email address and name are specified by a user. What is the correct way to do this with PHP:

$to_header = "To: $name <$email>" # incorrect!

I already use a decent method for validating the email addressess (let's not go to that now...), but how do I properly encode $name with eg. QP when needed? For example, if the recipient is called "Foo Bär", I should produce (eg.) something like:

To: =?utf-8?Q?Foo_B=C3=A4r?= <[email protected]>

Update: Earlier I wasn't using a ready-made mailer such as PHPMailer for other reasons (we already had an external queue system). Anyway, now I'm ending up using SwiftMailer. Thanks for all the answers!

+1  A: 

You can use imap_rfc822_write_address if it’s available.

Gumbo
That function seems to take care of quoting (when there is a comma in the name), but not encoding high-bit characters.
nbr
+1  A: 

You should try to use a third party libraby such as PhpMailer or Zend_Mail. They provide simple methods to set up all these parameters. They take care of encoding too, and enable some powerful control over sending/authenticating

Otherwise, try going the hardway using the different imap methods : http://fr2.php.net/manual/fr/book.imap.php

Rodolphe
A: 

Here is how I do it (with a bit of overkill):

function check_referrer($referrers) {
    if (count($referrers)) {
        $found = false;
        $temp = explode("/",getenv("HTTP_referrer"));
        $referrer = $temp[2];
            if ($referrer == "") {
            $referrer = $_SERVER['HTTP_referrer'];
            list($remove, $stuff) = split('//', $referrer, 2);
            list($home, $stuff) = split('/', $stuff, 2);
            $referrer = $home;
        }
            for ($x = 0; $x < count($referrers); $x++) {
            if (eregi ($referrers[$x], $referrer)) {
                $found = true;
            }
        }
        if ($referrer == "") {
            $found = false;
        }
        if (!$found){
            error_log("[Store Checkout] Illegal Referrer. (".getenv("HTTP_referrer").")", 0);
            return "<div id='error'><p>You are coming from an <strong>unauthorized domain.</strong></p>\r\n"."\t<ul>\r\n".$error_list."\t</ul>\r\n"."</div>\r\n";
        }
        return $found;
    } else {
    return "<div id='error'><p>You are coming from an <strong>unauthorized domain.</strong></p>\r\n"."\t<ul>\r\n".$error_list."\t</ul>\r\n"."</div>\r\n";
    }
} /* end function check_referrer */

function mail_it($content, $subject, $sender, $recipient) {
    $referrers = array("example.com");

    $authorizedDomain = check_referrer($referrers);
    if($authorizedDomain === FALSE) {
        return $authorizedDomain;
    }
    $sender = remove_headers($sender);
    $recipient = remove_headers($recipient);

    if($content !== FALSE && $subject !== FALSE && $sender !== FALSE && $recipient !== FALSE) {
        $headers = "from: ".$sender."\r\n"; 
        mail($recipient, $subject, $content, $headers);
    }
    return;
} /* end function mail_it */

$content = "email body content";
$subject = "email subject";
$sender = "Your Name <[email protected]>";
$recipient = $name . "<" . $email . ">";


mail_it($content, $subject, $sender, $recipient);
kingjeffrey
I can't remember where I found the `check_referrer` code, but it works well.
kingjeffrey
Sorry, but this all seems quite unrelated, and doesn't answer my question.
nbr
If I understand your question, the line at the bottom addresses your question (the rest is needed to send the email securely): `$recipient = $name . "<" . $email . ">";`. But then again, trying to read between the lines – if you are really asking about character encoding, and not formatting the email address with a name – then no, I did not address that issue (as the question does not seem to directly ask for that information).
kingjeffrey
+1  A: 

I've used iconv_mime_encode to encode the Subject -header. I presume the same could be used for encoding the name in the To -header as well.

I do, however, as others have, recommend using an existing library or package to handle the encoding for you.

I've used Mail mime available from PEAR. Not perhaps the best out there, but as an alternative to the other ones suggested.

var_dump(
    iconv_set_encoding('output_encoding', 'UTF-8'),
    iconv_set_encoding('internal_encoding', 'UTF-8'),
    iconv_set_encoding('input_encoding', 'UTF-8'),
    iconv_mime_encode('To', 'Bäråör Zückefém') .  " <[email protected]>"
);
nikc
I finally used a combination of iconv_mime_encode() and imap_rfc822_write_address().
nbr