class Email
{
private $to;
private $from;
private $subject;
private $message;
private $headers;
public function __construct($to, $message, $from = '
[email protected]', $subject = 'No Subject', $headers = [])
{
$this->to = $to;
$this->from = $from;
$this->subject = $subject;
$this->message = $message;
$this->headers = $headers;
}
public function send()
{
// ヘッダーを構築
$formattedHeaders = "From: {$this->from}¥r¥n";
foreach ($this->headers as $key => $value) {
$formattedHeaders .= "$key: $value¥r¥n";
}
// デフォルト値が設定されているため、最小限の情報でメールを送信可能
mail($this->to, $this->subject, $this->message, $formattedHeaders);
echo "Email sent to {$this->to} with subject '{$this->subject}'¥n";
}
}