-
-
Save rmontoyacordero/7b505e959386344d5701e7bb65d5bb79 to your computer and use it in GitHub Desktop.
Send email using a PHP 5.5 Generator and cURL
This file contains 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
class Gmail | |
{ | |
private static $instance; | |
private $mail, $email, $pass; | |
private function __construct($email, $pass) | |
{ | |
$this->email = $email; | |
$this->pass = $pass; | |
} | |
public function __clone() | |
{ | |
trigger_error("Invalid Operation: Dont clone one instance of " . __CLASS__ . " class.", E_USER_ERROR); | |
} | |
public static function getInstance(string $email, string $passwd): self | |
{ | |
if (!self::$instance instanceof self) { | |
self::$instance = new static($email, $passwd); | |
} | |
return self::$instance; | |
} | |
private function mailGen(): iterable | |
{ | |
$from = yield; | |
$to = yield; | |
$subject = yield; | |
$body = yield; | |
yield "FROM: <" . $from . ">\n"; | |
yield "To: <" . $to . ">\n"; | |
yield "Date: " . date("r") . "\n"; | |
yield "Subject: " . $subject . "\n"; | |
yield "\n"; | |
yield $body; | |
yield ""; | |
} | |
public function getLine(): string | |
{ | |
$resp = $this->mail->current(); | |
$this->mail->next(); | |
return $resp; | |
} | |
public function send(string $to, string $subject, string $body): string | |
{ | |
$this->mail = $this->mailGen(); | |
$this->mail->send($this->email); | |
$this->mail->send($to); | |
$this->mail->send($subject); | |
$this->mail->send($body); | |
$ch = curl_init("smtps://smtp.gmail.com:465"); | |
curl_setopt($ch, CURLOPT_MAIL_FROM, "<" . $this->email . ">"); | |
curl_setopt($ch, CURLOPT_MAIL_RCPT, array("<" . $to . ">")); | |
curl_setopt($ch, CURLOPT_USERNAME, $this->email); | |
curl_setopt($ch, CURLOPT_PASSWORD, $this->pass); | |
curl_setopt($ch, CURLOPT_USE_SSL, CURLUSESSL_ALL); | |
curl_setopt($ch, CURLOPT_PUT, 1); // If this option is not activated, $this->getLine will not be executed never | |
curl_setopt($ch, CURLOPT_VERBOSE, true); // Uncomment to see the transaction | |
curl_setopt($ch, CURLOPT_READFUNCTION, array($this, "getLine")); | |
return curl_exec($ch); | |
} | |
} |
This file contains 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
require 'gmail.php'; | |
$gmail= Hotmail::getInstance('from@gmail.com', 'password'); | |
$gmail->send('to@email.com', 'subject', 'body'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment