// Gmail password // standard mail message RFC2822 $m = 'From: ' . $f . "\r\n" . 'To: ' . $t . "\r\n" . 'Subject: test' . "\r\n" . 'Content-Type: text/plain' . "\r\n\r\n" . 'Text message.'; // connect to 'smtp.gmail.com' via SSL (TLS encryption) using port '465' and timeout '10' secounds // make sure you have OpenSSL module (extension) enable on your php configuration $c = fsockopen('tls://smtp.gmail.com', 465, $errno, $errstr, 10) or die($errstr); // expect response code '220' if (!SMTP::recv($c, 220)) { die(print_r($_RESULT)); } // EHLO/HELO if (!SMTP::ehlo($c, 'localhost')) { SMTP::helo($c, 'localhost') or die(print_r($_RESULT)); } // AUTH LOGIN/PLAIN if (!SMTP::auth($c, $f, $p, 'login')) { SMTP::auth($c, $f, $p, 'plain') or die(print_r($_RESULT)); } // MAIL FROM SMTP::from($c, $f) or die(print_r($_RESULT)); // RCPT TO SMTP::to($c, $t) or die(print_r($_RESULT)); // DATA SMTP::data($c, $m) or die(print_r($_RESULT)); // RSET, optional if you need to send another mail using this connection '$c' // SMTP::rset($c) or die(print_r($_RESULT)); // QUIT SMTP::quit($c); // close connection @fclose($c); echo 'Sent !';
/** * Send mail via SMTP. * Returns false if there is a bad MAIL FROM, RCPT, or DATA input. * Uses the PHPMailerSMTP class by default. * @see PHPMailer::getSMTPInstance() to use a different class. * @param string $header The message headers * @param string $body The message body * @throws phpmailerException * @uses SMTP * @access protected * @return boolean */ protected function smtpSend($header, $body) { $bad_rcpt = array(); if (!$this->smtpConnect($this->SMTPOptions)) { throw new phpmailerException($this->lang('smtp_connect_failed'), self::STOP_CRITICAL); } if ('' == $this->Sender) { $smtp_from = $this->From; } else { $smtp_from = $this->Sender; } if (!$this->smtp->mail($smtp_from)) { $this->setError($this->lang('from_failed') . $smtp_from . ' : ' . implode(',', $this->smtp->getError())); throw new phpmailerException($this->ErrorInfo, self::STOP_CRITICAL); } // Attempt to send to all recipients foreach (array($this->to, $this->cc, $this->bcc) as $togroup) { foreach ($togroup as $to) { if (!$this->smtp->recipient($to[0])) { $error = $this->smtp->getError(); $bad_rcpt[] = array('to' => $to[0], 'error' => $error['detail']); $isSent = false; } else { $isSent = true; } $this->doCallback($isSent, array($to[0]), array(), array(), $this->Subject, $body, $this->From); } } // Only send the DATA command if we have viable recipients if (count($this->all_recipients) > count($bad_rcpt) and !$this->smtp->data($header . $body)) { throw new phpmailerException($this->lang('data_not_accepted'), self::STOP_CRITICAL); } if ($this->SMTPKeepAlive) { $this->smtp->reset(); } else { $this->smtp->quit(); $this->smtp->close(); } //Create error message for any bad addresses if (count($bad_rcpt) > 0) { $errstr = ''; foreach ($bad_rcpt as $bad) { $errstr .= $bad['to'] . ': ' . $bad['error']; } throw new phpmailerException($this->lang('recipients_failed') . $errstr, self::STOP_CONTINUE); } return true; }
/** * Send mail via SMTP. * Returns false if there is a bad MAIL FROM, RCPT, or DATA input. * Uses the PHPMailerSMTP class by default. * * @see PHPMailer::getSMTPInstance() to use a different class. * * @param string $header The message headers * @param string $body The message body * * @throws phpmailerException * * @uses SMTP * * @return bool */ protected function smtpSend($header, $body) { $bad_rcpt = []; if (!$this->smtpConnect()) { throw new phpmailerException($this->lang('smtp_connect_failed'), self::STOP_CRITICAL); } $smtp_from = $this->Sender == '' ? $this->From : $this->Sender; if (!$this->smtp->mail($smtp_from)) { $this->setError($this->lang('from_failed') . $smtp_from . ' : ' . implode(',', $this->smtp->getError())); throw new phpmailerException($this->ErrorInfo, self::STOP_CRITICAL); } // Attempt to send to all recipients foreach ($this->to as $to) { if (!$this->smtp->recipient($to[0])) { $bad_rcpt[] = $to[0]; $isSent = false; } else { $isSent = true; } $this->doCallback($isSent, [$to[0]], [], [], $this->Subject, $body, $this->From); } foreach ($this->cc as $cc) { if (!$this->smtp->recipient($cc[0])) { $bad_rcpt[] = $cc[0]; $isSent = false; } else { $isSent = true; } $this->doCallback($isSent, [], [$cc[0]], [], $this->Subject, $body, $this->From); } foreach ($this->bcc as $bcc) { if (!$this->smtp->recipient($bcc[0])) { $bad_rcpt[] = $bcc[0]; $isSent = false; } else { $isSent = true; } $this->doCallback($isSent, [], [], [$bcc[0]], $this->Subject, $body, $this->From); } // Only send the DATA command if we have viable recipients if (count($this->all_recipients) > count($bad_rcpt) and !$this->smtp->data($header . $body)) { throw new phpmailerException($this->lang('data_not_accepted'), self::STOP_CRITICAL); } if ($this->SMTPKeepAlive == true) { $this->smtp->reset(); } else { $this->smtp->quit(); $this->smtp->close(); } if (count($bad_rcpt) > 0) { // Create error message for any bad addresses throw new phpmailerException($this->lang('recipients_failed') . implode(', ', $bad_rcpt), self::STOP_CONTINUE); } return true; }
$c = fsockopen('ssl://smtp.gmail.com', 465, $errno, $errstr, 10) or die($errstr); // expect response code '220' if (!SMTP::recv($c, 220)) { die(print_r($_RESULT)); } // EHLO/HELO if (!SMTP::ehlo($c, 'localhost')) { SMTP::helo($c, 'localhost') or die(print_r($_RESULT)); } // AUTH LOGIN/PLAIN if (!SMTP::auth($c, $username_smtp, $password_smtp, 'login')) { SMTP::auth($c, $username_smtp, $password_smtp, 'plain') or die(print_r($_RESULT)); } // MAIL FROM SMTP::from($c, $from) or die(print_r($_RESULT)); // RCPT TO SMTP::to($c, $to) or die(print_r($_RESULT)); // DATA //SMTP::data($c, $m) or die(print_r($_RESULT)); // RSET, optional if you need to send another mail using this connection '$c' if (!SMTP::data($c, $m)) { echo "服务器正忙,请稍后再试。"; } else { $ip = $_SERVER['REMOTE_ADDR']; $renrenuserid = $_GET['renrenuserid']; mysql_query("INSERT INTO users VALUES ('','{$username}','{$password}','{$email}','{$code}','0','','','','','','','','','','','{$ip}',0,'{$renrenuserid}')"); echo "sent"; } SMTP::quit($c); @fclose($c); } }
/** * Send email using authenticated STMP with TLS */ function sendResetMail($to, $newpass) { $smtp = new SMTP(); //$smtp->setDebugLevel(4); //$smtp->setDebugOutput("error_log"); $host = $this->config['smtp']['host']; $port = $this->config['smtp']['port']; // Connect if (!$smtp->connect($host, $port)) { return $smtp->getError(); } // EHLO if (!$smtp->hello("me")) { return $smtp->getError(); } // STARTTLS if (!$smtp->startTLS()) { return $smtp->getError(); } // EHLO if (!$smtp->hello("me")) { return $smtp->getError(); } // AUTH LOGIN $username = $this->config['smtp']['username']; $password = $this->config['smtp']['password']; if (!$smtp->authenticate($username, $password)) { return $smtp->getError(); } // MAIL FROM $from = $this->config['smtp']['from']; if (!$smtp->mail($from)) { return $smtp->getError(); } // RCPT TO if (!$smtp->recipient($to)) { return $smtp->getError(); } // DATA $msg = str_replace("@@email@@", $to, str_replace("@@newpass@@", $newpass, $this->config['smtp']['msgtemplate'])); if (!$smtp->data($msg)) { return $smtp->getError(); } // QUIT if (!$smtp->quit()) { return $smtp->getError(); } // Disconnect and close $smtp->close(); return null; }
private function _send_mail($msgdata) { $smtp = new SMTP(); $smtp->debug = $this->debug; if (!$smtp->connect($this->host, $this->port, $this->timeout)) { $this->error_msg = "SMTP Error: could not connect to SMTP host server"; $this->error_msg .= "[" . $this->host . ":" . $this->port . "]"; return false; } if ($this->auth_login) { if (!$smtp->auth_hello($this->helo, $this->auth_user, $this->auth_pass)) { $this->error_msg = "SMTP Error: Invalid username/password"; if ($smtp->error_msg) { $this->error_msg .= "<br>" . $smtp->error_msg; } return false; } } else { $smtp->hello($this->helo); } if (!$smtp->mail_from(sprintf("<%s>", $this->from))) { $this->error_msg = "SMTP Error: Mail from [" . $this->from . "] not accepted."; if ($smtp->error_msg) { $this->error_msg .= "<br>" . $smtp->error_msg; } return false; } $iToCount = count($this->to); for ($i = 0; $i < $iToCount; $i++) { if (!$smtp->recipient(sprintf("<%s>", $this->to[$i][0]))) { $this->error_msg = "SMTP Error: recipient [" . $this->to[$i][0] . "] not accepted."; if ($smtp->error_msg) { $this->error_msg .= "<br>" . $smtp->error_msg; } return false; } } $iCcCount = count($this->cc); for ($i = 0; $i < $iCcCount; $i++) { if (!$smtp->recipient(sprintf("<%s>", $this->cc[$i][0]))) { $this->error_msg = "SMTP Error: recipient [" . $this->cc[$i][0] . "] not accepted."; if ($smtp->error_msg) { $this->error_msg .= "<br>" . $smtp->error_msg; } return false; } } $iBccCount = count($this->bcc); for ($i = 0; $i < $iBccCount; $i++) { if (!$smtp->recipient(sprintf("<%s>", $this->bcc[$i][0]))) { $this->error_msg = "SMTP Error: recipient [" . $this->bcc[$i][0] . "] not accepted."; if ($smtp->error_msg) { $this->error_msg .= "<br>" . $smtp->error_msg; } return false; } } if (!$smtp->data($msgdata)) { $this->error_msg = "SMTP Error: data not accepted"; if ($smtp->error_msg) { $this->error_msg .= "<br>" . $smtp->error_msg; } return false; } $smtp->_quit(); }
/** * Send mail via SMTP. * @return boolean True on success. */ protected function smtpSend() { $this->smtp = new SMTP(); $allRecipients = array(); $badRecipients = array(); try { // <editor-fold desc="Prepare SMTP host."> $hostInfo = array(); if (!preg_match('/^((ssl|tls):\\/\\/)*([a-zA-Z0-9\\.-]*):?([0-9]*)$/', $this->smtpHost, $hostInfo)) { throw new \Exception("Invalid SMTP host."); } $prefix = ""; $tls = $this->smtpSecure == "tls"; if ($hostInfo[2] == "ssl" || $hostInfo[2] == "" && $this->smtpSecure == "ssl") { $prefix = "ssl://"; $tls = false; } elseif ($hostInfo[2] == "tls") { $tls = true; } $host = $prefix . $hostInfo[3]; $tport = (int) $hostInfo[4]; $port = $tport > 0 && $tport < 65536 ? $tport : $this->smtpPort; // </editor-fold> if (!$this->smtp->connect($host, $port)) { throw new \Exception("Could not connect to SMTP host."); } $hello = !empty($this->smtpHelo) ? $this->smtpHelo : $this->serverHostname(); if (!$this->smtp->hello($hello)) { throw new \Exception("Could not send HELO."); } if ($tls) { if (!$this->smtp->tls()) { throw new \Exception("Could not start TLS."); } $this->smtp->hello($hello); } if ($this->smtpAuth && !$this->smtp->authenticate($this->smtpUsername, $this->smtpPassword)) { throw new \Exception("Could not authenticate."); } if (!$this->smtp->mail($this->from[0])) { throw new \Exception("Could not send MAIL FROM."); } foreach ($this->to as $to) { if (!$this->smtp->recipient($to[0])) { $badRecipients[] = $to[0]; } else { $allRecipients[] = $to[0]; } } // foreach ($this->cc as $cc) { // if (!$this->smtp->recipient($cc[0])) // $badRecipients[] = $cc[0]; // else // $allRecipients[] = $cc[0]; // } // // foreach ($this->bcc as $bcc) { // if (!$this->smtp->recipient($bcc[0])) // $badRecipients[] = $bcc[0]; // else // $allRecipients[] = $bcc[0]; // } if (count($allRecipients) > 0 && !$this->smtp->data($this->header . $this->body)) { throw new \Exception("Data was not accepted."); } if ($this->smtpKeepAlive) { $this->smtp->reset(); } else { $this->smtp->quit(); $this->smtp->close(); } return true; } catch (\Exception $e) { if (count($badRecipients) > 0) { $rcpt = implode(", ", $badRecipients); throw new EmailException(array("Failed delivery to the following recipient(s): %s.", $rcpt)); } throw new EmailException($e->getMessage()); } }