-
-
Save tonivj5/8c6ce6c560c06c7aaab232ecb60af6f8 to your computer and use it in GitHub Desktop.
Send email using a PHP 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 $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); | |
} | |
} |
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 = new Gmail('[email protected]', 'password'); | |
$gmail->send('[email protected]', 'subject', 'body'); |
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
252 2.1.5 Send some mail, I'll try my best. So good but something missing