← Back to PHP Course | Chapter 16: Testing & Tools | Lesson 7 of 10

PHP Email Sending (PHPMailer)

PHP से email भेजना post office को एक letter देने जैसा है। PHPMailer एक friendly helper है जो address, subject, और attachments सही से भरता है और message deliver करवाता है।
Syntax
php
$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
<?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";
?>

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
<?php
// $mail = new PHPMailer\PHPMailer\PHPMailer();
// $mail->isSMTP();
echo "PHPMailer adds authentication, encryption, and reliable delivery over mail()";
?>

SMTP Settings Configure करना

PHPMailer को एक authenticated SMTP server (जैसे Gmail या Mailgun जैसा transactional provider) के through relay करने के लिए configure करना असल में email को spam में जाने के बजाय reliably deliver करवाता है।

उदाहरण: Configuring SMTP Settings

php
<?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";
?>

Attachments Add करना

PHPMailer का addAttachment() method किसी email के साथ files भेजने के लिए ज़रूरी encoding handle करता है, लेकिन attach करने की कोशिश करने से पहले आपको हमेशा confirm करना चाहिए कि file disk पर exist करती है।

उदाहरण: Adding Attachments

php
<?php
$file = "report.txt";
file_put_contents($file, "Report contents");
if (file_exists($file)) {
    echo "Attaching $file"; // $mail->addAttachment($file);
}
?>

HTML Email भेजना

PHPMailer पूरे HTML email bodies support करता है, आपको एक web page जैसे ही custom styling और layout इस्तेमाल करने देते हुए, जो plain mail() headers आसानी से replicate नहीं कर सकते।

उदाहरण: HTML Email Sending

php
<?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";
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. बिना एक configured mail server के mail() पर भरोसा करना, ताकि actually कोई email न भेजा जाए।
  2. SMTP passwords को सीधे version control में committed code में डालना।
  3. SMTPAuth या सही port और encryption enable न करना, ताकि PHPMailer connect न कर सके।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.