برای استفاده از کلاس phpmailer اول دستور زیر رو وارد می کنیم
composer require phpmailer/phpmailer
بعد از وارد کردن این دستور در کامند ویندوز یا لینوکس این کلاس نصب میشه و میتونیم از اون استفاده کنیم
برای استفاده از این کلاس از کد زیز استفاده می کنیم
<?php
require_once "vendor/autoload.php";
//PHPMailer Object
$mail = new PHPMailer;
//From email address and name
$mail->From = "[email protected]";
$mail->FromName = "Full Name";
//To address and name
$mail->addAddress("[email protected]", "Recepient Name");
$mail->addAddress("[email protected]"); //Recipient name is optional
//Address to which recipient will reply
$mail->addReplyTo("[email protected]", "Reply");
//CC and BCC
$mail->addCC("[email protected]");
$mail->addBCC("[email protected]");
//Send HTML or Plain Text email
$mail->isHTML(true);
$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->AltBody = "This is the plain text version of the email content";
if(!$mail->send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent successfully";
}
اگر نیاز بود که فایل ضمیمه هم توی ارسال ایمیل هاتون باشه از کد زیر استفاده کنید
<?php
require_once "vendor/autoload.php";
$mail = new PHPMailer;
$mail->From = "[email protected]";
$mail->FromName = "Full Name";
$mail->addAddress("[email protected]", "Recipient Name");
//Provide file path and name of the attachments
$mail->addAttachment("file.txt", "File.txt");
$mail->addAttachment("images/profile.png"); //Filename is optional
$mail->isHTML(true);
$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->AltBody = "This is the plain text version of the email content";
if(!$mail->send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent successfully";
}
و اگر خواستید از اکانت جیمیلتون برای ارسال ایمیل استفاده کنید از کد زیر کمک بگیرید
<?php
require_once "vendor/autoload.php";
$mail = new PHPMailer;
//Enable SMTP debugging.
$mail->SMTPDebug = 3;
//Set PHPMailer to use SMTP.
$mail->isSMTP();
//Set SMTP host name
$mail->Host = "smtp.gmail.com";
//Set this to true if SMTP host requires authentication to send email
$mail->SMTPAuth = true;
//Provide username and password
$mail->Username = "[email protected]";
$mail->Password = "super_secret_password";
//If SMTP requires TLS encryption then set it
$mail->SMTPSecure = "tls";
//Set TCP port to connect to
$mail->Port = 587;
$mail->From = "[email protected]";
$mail->FromName = "Full Name";
$mail->addAddress("[email protected]", "Recepient Name");
$mail->isHTML(true);
$mail->Subject = "Subject Text";
$mail->Body = "<i>Mail body in HTML</i>";
$mail->AltBody = "This is the plain text version of the email content";
if(!$mail->send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent successfully";
}
خوب دوستان این هم کد ارسال ایمیل با کلاس خوب phpmailer لطفا اگر نظری داشتید با ما در میون بذارید با تشکر