端くれプログラマの備忘録 PHP [PHP] メールを送信する

[PHP] メールを送信する

メールを送信するサンプルコード。

$to     = "hanako@example.com";
$from   = "taro@example.com";
$bcc    = "takeo@example.com";
$return = "support@example.com";

$subject = "Greetings";

$body  = "Hello\n";
$body .= "How are you doing?\n";

mb_language("ja");
mb_internal_encoding("UTF-8");

$subject = mb_convert_encoding($subject, "ISO-2022-JP", "AUTO");
$subject = mb_encode_mimeheader($subject);
    
$headers = "MIME-Version: 1.0 \n";
$headers .= "From: " . $from . "\n";
$headers .= "Bcc: " . $bcc . "\n";

$parameters = "-f " . $return;
    
if (!mb_send_mail($to, $subject, $body, $headers, $parameters)) {
    echo "mb_send_mail() failed.\n";
    return false;
}
return true;