Skip to content

Instantly share code, notes, and snippets.

@richjenks
Last active April 29, 2020 23:16
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save richjenks/fa144ee7365f91c9fecb to your computer and use it in GitHub Desktop.
Save richjenks/fa144ee7365f91c9fecb to your computer and use it in GitHub Desktop.
Send email using a PHP 5.5 Generator and cURL
class Gmail {
private $mail;
private $email;
private $pass;
public function __construct($email, $pass){
$this->email = $email;
$this->pass = $pass;
}
private function mailGen(){
$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(){
$resp = $this->mail->current();
$this->mail->next();
return $resp;
}
public function send($to, $subject, $body){
$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_VERBOSE, true); // Uncomment to see the transaction
curl_setopt($ch, CURLOPT_READFUNCTION, array($this, "getLine"));
return curl_exec($ch);
}
}
require 'gmail.php';
$gmail = new Gmail('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