Skip to content

Instantly share code, notes, and snippets.

@tonivj5
Forked from richjenks/gmail.php
Last active June 2, 2021 12:03
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tonivj5/8c6ce6c560c06c7aaab232ecb60af6f8 to your computer and use it in GitHub Desktop.
Save tonivj5/8c6ce6c560c06c7aaab232ecb60af6f8 to your computer and use it in GitHub Desktop.
Send email using a PHP 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_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);
}
}
require 'gmail.php';
$gmail = new Gmail('from@gmail.com', 'password');
$gmail->send('to@email.com', 'subject', 'body');
@nik0s100
Copy link

252 2.1.5 Send some mail, I'll try my best. So good but something missing

@JeK2a
Copy link

JeK2a commented Mar 21, 2021

Try curl_setopt($ch, CURLOPT_UPLOAD, 1);

252 2.1.5 Send some mail, I'll try my best. So good but something missing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment