Aj is
article me ham parhen gen ke PHP me mail "PHPMailer" library ko use krte hue kis
tarah bheji jati hay aur PHP ka built-in function mail() q nh use krna chaiye.
PHP
me mail kis tarah bheje jate hain usko samajhne se pehle hamen ye samajhna hoga
ke jab ham kisi ko koi email bhjte hain to wo email kis tarah se process hokar receiver
ke pas pounchta hay.
What is SMTP:
Jab
bhi ham kisi ko koi mail bhjtey hain to receiver ke pas email ek protocol
follow krte hue ata hay, Aur is protocol ko ham SMTP (Simple Mail Transfer
Protocol) kehte hain.
Jese
hi ham kisi ko mail bhjtey hain to wo mail pehle ek server ke pas jata hay jo
is SMTP protocol ko follow krta hay isliye ham usey SMTP server kehte hain, Ye
server phir receiving end ke SMTP server ke pas mail ko bhjta hay aur phir receiver
is SMTP server se mail receive krta hay.
PHP Mail Function:
PHP me
hamare pas ek built in function hay jis ke through kisi ko bhi mail send krskte
hain, Example dekhen.
1. <?php
2. $to="abc@gmail.com";
3. $sub="This is Subject";
4. $messg="This is Messege Body";
5. mail($to,$sub,$messg);
6. ?>
Code
self explanatory hay, simply ham mail function me jis email per mail bhjna hay
uska address then subject and then apna message pass krte hain, Additionally
ham is me headers bhi pass krskte hain, Header extra information hoti hay mail
ke bare me.
Lekin
Jab is code ko execute karen gen to apke pas pehle to foran se mail nh ae ga
balke ap ko wait krna parega, Dusra mail bhi spam folder me aega, Iski waja kia
hay?
Iski
waja hay ke PHP mail function ek to apka locally SMTP server use krta hay mail
bhjne ke liye, Agar ap locally chala rhe hain apna code to Xampp me mojud local
SMTP server aur agar hosting per apka code hay to hosting ka SMTP server aur
mostly mail service locally ya hosting servers ko blacklist krdeti hay, Uski
waja hay ke SMTP server bohat sari security policies ko follow nh krta isliye
mostly mail services hamare server ki IP addres ko blacklist krdeti hain aur
phir mail function use krne se security mazeed
kharab hojati hay q ke mail function ko easily spam purpose ke liye use kia
jsakta hay.
Ab
jese hi ham code execute karen gen to pehle to hamare SMTP server se gaye mail
ko dusri mail services accept nh karen gen, response me hamara server continuously
mail send krne ki koshish kare ga aur finally dusra mail server hamari request
accept to krle ga lekin wo mail ko spam folder me dal dey ga.
Iska
ek to hal yeh hay ke ham complex headers use krke apne mail function ko kuch
had tak secure banae, Lekin developer ne iska haal pehle se hi bana kr muktalif
libraries ki surat me hame de diya.
Ek
aur waja PHP mail function ko na use krne ki ye bhi hay ke ye function hamari
ziada kuch madad nh krta agar hamen koi attachment mail ke sath bhjni hay etc.
PHP libraries
ko banane ka maqsad yehi hay k ek to security ko improve kia jae take dusri
mail services hamare mail ko accept karen, Isi tarah agar hamen mail me attachment
wagera bhjni hay to us ke liye PHP libraries ne kam asan krdiya.
PHP
se mail send krni ki bohat sari libraries hain aur sab se popular library hay “PHP
Mailer”.
PHP Mailer:
PHPMailer
php ki ek popular mail library hay jis ke through ham complex kam ko bari asani
ke sath krskte hain.
Ye
library complex headers ko backend per khud manage krti hay jis ki madad se
mail services ke hamare mail accept krne ke chance barh jate hain.
Lekin
bat yahan per khatam nh hoti, Ab jesa ke bataya hay ke security reasons ki waja
se locally ya shared hosting ke mostly SMTP servers blacklisted hote hain, To
ab ek tarika to yeh hay ke ham headers ko propely format karen, Lekin kia hi
bat ho agar ham Google ke SMTP server ko apna mail send krne ke liye use karen
!
Google
hamen ye service provide krta hay ke ham uske Gmail ke SMTP server ko use krskte
hain apna mail bhjne ke liye aur is kam ko mazeed asan krdiya hay PHPMailer
library ne.
Google
kay SMTP server ko use krne ka ye faida hoga ke Google ka SMTP for sure
blacklisted nh hay aur na hi security vulnerabilities hain, jiska matlab dusri
mail services easily hamare mail ko accept krlen gen !
To is
library ko install aur configure krne ke liye Github me iski repository me
mojud readme file me mojud instructions
ko read karen.
Ab
ham simply basic example dekhte hain ke kis tarah is library ke use krte hue
ham Google ke SMTP server ke zariye kisi bhi mail service per mail bhj skte
hain
1. <?php
2.
3. //Import PHPMailer classes into the global namespace
4. use PHPMailer\PHPMailer\PHPMailer;
5.
6. require '../vendor/autoload.php';
7.
8. //Create a new PHPMailer instance
9. $mail = new PHPMailer;
10.
11. //Tell PHPMailer to use SMTP
12. $mail->isSMTP();
13.
14. //Enable SMTP debugging
15. // 0 = off (for production use)
16. // 1 = client messages
17. // 2 = client and server messages
18. $mail->SMTPDebug = 2;
19.
20. //Set the hostname of the mail server
21. $mail->Host = 'smtp.gmail.com';
22. // use
23. // $mail->Host = gethostbyname('smtp.gmail.com');
24. // if your network does not support SMTP over IPv6
25.
26. //Set the SMTP port number -
27. $mail->Port = 587;
28.
29. //Set the encryption system to use - ssl (deprecated) or tls
30. $mail->SMTPSecure = 'tls';
31.
32. //Whether to use SMTP authentication
33. $mail->SMTPAuth = true;
34.
35. //Username to use for SMTP authentication - use full email address for gmail
36. $mail->Username = "username@gmail.com";
37.
38. //Password to use for SMTP authentication
39. $mail->Password = "yourpassword";
40.
41. //Set who the message is to be sent from
42. $mail->setFrom('from@example.com', 'First Last');
43.
44. //Set an alternative reply-to address
45. $mail->addReplyTo('replyto@example.com', 'First Last');
46.
47. //Set who the message is to be sent to
48. $mail->addAddress('whoto@example.com', 'John Doe');
49.
50. //Set the subject line
51. $mail->Subject = 'PHPMailer GMail SMTP test';
52.
53. //Mail message
54. $mail->Body = 'This is a plain-text message body';
55.
56. //send the message, check for errors
57. if (!$mail->send()) {
58. echo "Mailer Error: " . $mail->ErrorInfo;
59. } else {
60. echo "Message sent!";
61.
62.
63. }
- ?>
Mostly code self explanatory
hay, Lekin kuch cheezen hain jinka me bata deta hn.
1. $mail->Host = 'smtp.gmail.com';
Is ke through ham apni library ko bata rhe hain ke mail ko bhjne ke liye Google ka SMTP server use kia
jae.
1. //Username to use for SMTP authentication - use full email address for gmail
2. $mail->Username = "username@gmail.com";
3.
4. //Password to use for SMTP authentication
5. $mail->Password = "yourpassword";
Ab yahan uper hame apni koi gmail Id aur uska pass dena hoga, Q ke
Gmail ka SMTP server authentication ke liye ye details use kare ga, Agar ap Gmail
Id aur Password nh lagna chate then ap XOAuth
authentication bhi use krskte hain, Jis me apko client id aur client secret
likhna hoga username aur password ki jaga.
Agar apko Gmail ka SMTP server use nh krna balke khud ka locally
server use krna hay then ap wo kam bhi is library ke through krskte hain,
Github per iski muktalif examples
mojud hain.
Conclusion:
PHPMailer library ko use krne ka faida yeh hoga ke apke pas email ki
jis tarah normal delivery hoti hay wese hi hogi koi delay nh hooga, Similarly apko
complex headers likhne ki zarort nh,
Backend per ye library wo sara kam khud krdey gi.
Agar is article se apko waqayi faida hua to is article ko apni coding
community ke sath lazmi share karen q ke Sharing is Caring !
No comments:
Post a Comment