PHP Email Sending (PHPMailer)
In this page:
$sent = mail($to, $subject, $message, $headers);
$mail = new PHPMailer\PHPMailer\PHPMailer(true);
$mail->isSMTP();
$mail->Host = "smtp.example.com";
$mail->send();
mail() से Mail भेजना
PHP का built-in mail() function सीधे basic email भेज सकता है, आपको sender address, subject, और content type के लिए headers set करने देते हुए -- लेकिन यह पूरी तरह इस पर depend करता है कि server पर एक working mail transport configured हो।
उदाहरण: Sending Mail with mail()
<?php
// Declare `$to`, set to "[email protected]"
$to = "[email protected]";
// Declare `$subject`, set to "Welcome"
$subject = "Welcome";
// Declare `$headers`, set to "From: [email protected]"
$headers = "From: [email protected]";
// Declare `$sent`, set to `mail($to, $subject, "Hello!", $headers)`
$sent = mail($to, $subject, "Hello!", $headers);
// Print `$sent ? "Sent" : "mail() requires a configured mail transport"` to the output
echo $sent ? "Sent" : "mail() requires a configured mail transport";
?>
Login to try C/C++/Java/PHP code in the editor
PHPMailer का Introduction
mail() कोई authentication या encryption offer नहीं करता और अक्सर modern mail providers द्वारा blocked या spam flag किया जाता है; PHPMailer proper SMTP support, attachments, और HTML formatting को एक कहीं ज़्यादा reliable package में wrap करता है।
उदाहरण: Introduction to PHPMailer
<?php
// $mail = new PHPMailer\PHPMailer\PHPMailer();
// $mail->isSMTP();
echo "PHPMailer adds authentication, encryption, and reliable delivery over mail()";
?>
Login to try C/C++/Java/PHP code in the editor
SMTP Settings Configure करना
PHPMailer को एक authenticated SMTP server (जैसे Gmail या Mailgun जैसा transactional provider) के through relay करने के लिए configure करना असल में email को spam में जाने के बजाय reliably deliver करवाता है।
उदाहरण: Configuring SMTP Settings
<?php
// $mail->Host = 'smtp.gmail.com';
// $mail->SMTPAuth = true;
// $mail->Username = '[email protected]';
// $mail->Password = 'app-password';
echo "Relaying through authenticated SMTP avoids the spam folder";
?>
Login to try C/C++/Java/PHP code in the editor
Attachments Add करना
PHPMailer का addAttachment() method किसी email के साथ files भेजने के लिए ज़रूरी encoding handle करता है, लेकिन attach करने की कोशिश करने से पहले आपको हमेशा confirm करना चाहिए कि file disk पर exist करती है।
उदाहरण: Adding Attachments
<?php
$file = "report.txt";
file_put_contents($file, "Report contents");
if (file_exists($file)) {
echo "Attaching $file"; // $mail->addAttachment($file);
}
?>
Login to try C/C++/Java/PHP code in the editor
HTML Email भेजना
PHPMailer पूरे HTML email bodies support करता है, आपको एक web page जैसे ही custom styling और layout इस्तेमाल करने देते हुए, जो plain mail() headers आसानी से replicate नहीं कर सकते।
उदाहरण: HTML Email Sending
<?php
// $mail->isHTML(true);
// $mail->Body = "<h1>Welcome</h1><p>Thanks for signing up!</p>";
echo "PHPMailer bodies can use full HTML, just like a web page";
?>
Login to try C/C++/Java/PHP code in the editor
- बिना एक configured mail server के
mail()पर भरोसा करना, ताकि actually कोई email न भेजा जाए। - SMTP passwords को सीधे version control में committed code में डालना।
SMTPAuthया सही port और encryption enable न करना, ताकि PHPMailer connect न कर सके।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: