Created
September 14, 2018 11:24
-
-
Save dura0ok/71c426834a436d4b91d5471979ba0100 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
#Library for send messages for emails with files | |
namespace EmailSender; | |
require 'vendor/autoload.php'; | |
use PHPMailer\PHPMailer\PHPMailer; | |
class EmailSender | |
{ | |
public function __construct(string $login, string $password, $name) | |
{ | |
$this->login = $login; | |
$this->password = $password; | |
$this->name = $name; | |
$this->mail = new PHPMailer(); | |
} | |
private function load() | |
{ | |
$massiv = file('mail.txt'); | |
$string = implode(" ", $massiv); | |
preg_match_all('/([a-z0-9_\.\-])+\@(([a-z0-9\-])+\.)+([a-zа-я0-9]{2,4})+/i', $string, $matches); | |
return $matches; | |
} | |
public function send() | |
{ | |
$matches = $this->load(); | |
foreach ($matches[0] as $match) { | |
$this->mail->isSMTP(); // Set mailer to use SMTP | |
$this->mail->Host = 'smtp.mail.ru'; // Specify main and backup SMTP servers | |
$this->mail->SMTPAuth = true; // Enable SMTP authentication | |
$this->mail->Username = $this->login; // SMTP username | |
$this->mail->CharSet = "UTF-8"; | |
$this->mail->Password = $this->password; // SMTP password | |
$this->mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted | |
$this->mail->Port = 587; // TCP port to connect to | |
//Recipients | |
$this->mail->setFrom($this->login, $this->name); | |
$this->mail->addAddress($match); // Add a recipient | |
//Attachments | |
$this->mail->addAttachment('2.jpg', 'new.jpg'); // Optional name | |
//Content | |
$this->mail->isHTML(true); // Set email format to HTML | |
$this->mail->Subject = 'Предложение к сельхоз производителям'; | |
$this->mail->Body = '123 321233213121'; | |
$this->mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; | |
$this->mail->send(); | |
echo 'Message has been sent <br>'; | |
sleep(2); | |
} | |
} | |
} | |
$test = new EmailSender('[email protected]', 'secret', 'Stepan'); | |
$test->send(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment