PHP mail(), , . .
mail()
mail() :
mail($to, $subject, $message, $headers, $additional_parameters);
$to: .
$subject: .
$message: .
$headers: .
$additional_parameters: , .
$to = "recipient@example.com";
$subject = " ";
$message = " ";
$headers = "From: sender@example.com\r\n";
$headers .= "Reply-To: replyto@example.com\r\n";
$headers .= "CC: cc@example.com\r\n";
$additional_parameters = "-f sender@example.com";
mail($to, $subject, $message, $headers, $additional_parameters);
:
"-f sender@example.com"-
-fenvelope sender
:
// envelope sender
$additional_parameters = "-f sender@example.com";
// Return-Path
$additional_parameters = "-r sender@example.com";
//
$additional_parameters = "-f sender@example.com -r bounce@example.com";
:
function sendEmail($to, $subject, $message, $from) {
// email
if (!filter_var($to, FILTER_VALIDATE_EMAIL) || !filter_var($from, FILTER_VALIDATE_EMAIL)) {
return false;
}
$headers = "From: $from\r\n";
$headers .= "Reply-To: $from\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
$additional_parameters = "-f $from";
return mail($to, $subject, $message, $headers, $additional_parameters);
}
//
$result = sendEmail(
"client@example.com",
" ",
"<h1> !</h1><p> .</p>",
"noreply@mysite.com"
);
: - , -.
, :
- -
-f
:
- Content-Type HTML
mail() PHP, :
PHPMailer
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
$mail = new PHPMailer(true);
try {
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'user@example.com';
$mail->Password = 'password';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('to@example.com');
$mail->isHTML(true);
$mail->Subject = ' ';
$mail->Body = 'HTML ';
$mail->send();
echo ' ';
} catch (Exception $e) {
echo " : {$mail->ErrorInfo}";
}
Swift Mailer
require_once '/path/to/vendor/autoload.php';
$transport = (new Swift_SmtpTransport('smtp.example.com', 587))
->setUsername('your_username')
->setPassword('your_password');
$mailer = new Swift_Mailer($transport);
$message = (new Swift_Message(' '))
->setFrom(['john@doe.com' => 'John Doe'])
->setTo(['receiver@domain.org', 'other@domain.org' => 'A name'])
->setBody(' ');
$result = $mailer->send($message);
:
- SMTP
- HTML
- injection
WordPress)
