function Mail($to, $to_name, $from, $from_name, $subject, $text, $type = 'text') { //---- include_once 'lib/PEAR/Net/SMTP.php'; //---- $to_name = $this->MailEsc($to_name); $from_name = $this->MailEsc($from_name); $subject = $this->MailEsc($subject); //---- $headers = "MIME-Version: 1.0\r\n" . "Return-Path: {$from}\r\n" . "X-Mailer: MQL4 site\r\n" . "Date: " . date('r') . "\r\n" . "Message-Id: " . time() . '.' . preg_replace('/[^0-9a-z-]+/iu', '-', $to) . "@wr.com\r\n"; //---- if ($type == 'text') { $headers .= "Content-type: text/plain; charset=UTF-8\r\n"; } else { $headers .= "Content-type: text/html; charset=UTF-8\r\n"; } //---- if ($to_name != '') { $headers .= "To: {$to_name} <{$to}>\r\n"; } else { $headers .= "To: {$to}\r\n"; } if ($from_name != '') { $headers .= "From: {$from_name} <{$from}>\r\n"; } else { $headers .= "From: {$from}\r\n"; } //---- $smtp = new Net_SMTP(T_SMTP_SERVER, 25, T_SMTP_LOGIN); $smtp->connect(); //---- if (PEAR::isError($e = $smtp->auth(T_SMTP_LOGIN, T_SMTP_PASSWORD))) { $smtp->disconnect(); return false; } //---- if (PEAR::isError($smtp->mailFrom($from))) { $smtp->disconnect(); return false; } //---- if (PEAR::isError($smtp->rcptTo($to))) { $smtp->disconnect(); return false; } //---- if (PEAR::isError($smtp->data("Subject: {$subject}\r\n" . $headers . "\r\n" . $text))) { $smtp->disconnect(); return false; } //---- $smtp->disconnect(); //---- return true; }
/** * Applies action * * @return boolean success */ protected function main() { $recipient = Nexista_Path::parseInlineFlow($this->params['recipient']); $sender = Nexista_Path::parseInlineFlow($this->params['sender']); $subject = "Subject: " . Nexista_Path::parseInlineFlow($this->params['subject']) . "\n"; $body = Nexista_Flow::getbypath($this->params['body']); $host = Nexista_Path::parseInlineFlow($this->params['host']); if (require 'Net/SMTP.php') { $smtp = new Net_SMTP($host); $e = $smtp->connect(); $smtp->mailFrom($sender); $disclosed_recipients = "To: "; if (is_array($recipient)) { foreach ($recipient as $to) { if (PEAR::isError($res = $smtp->rcptTo($to))) { die("Unable to add recipients {$to}: " . $res->getMessage() . "\n"); } $disclosed_recipients .= $to; } } else { if (PEAR::isError($res = $smtp->rcptTo($recipient))) { die("Unable to add single recipient {$recipient} for {$host}: " . $res->getMessage() . "\n"); } $disclosed_recipients .= $recipient; } $disclosed_recipients .= "\n"; $headers = $disclosed_recipients . $subject; $smtp->data($headers . "\r\n" . $body); $smtp->disconnect(); } else { // try using mail() } }
/** * Disconnect and destroy the current SMTP connection. * * @return boolean True if the SMTP connection no longer exists. */ public function disconnect() { /* If we have an SMTP object, disconnect and destroy it. */ if (is_object($this->_smtp) && $this->_smtp->disconnect()) { $this->_smtp = null; } /* We are disconnected if we no longer have an SMTP object. */ return $this->_smtp === null; }
public function deliver(&$Mailer, $settings = array()) { $Message =& $Mailer->Message; $SmtpClient =& Mail::factory('smtp', $settings); include_once 'Net/SMTP.php'; if (!($smtp = new Net_SMTP($SmtpClient->host, $SmtpClient->port, $SmtpClient->localhost))) { return PEAR::raiseError('unable to instantiate Net_SMTP object'); } if ($SmtpClient->debug) { $smtp->setDebug(true); } if (PEAR::isError($smtp->connect($SmtpClient->timeout))) { trigger_error('unable to connect to smtp server '.$SmtpClient->host.':'.$SmtpClient->port, E_USER_NOTICE); return false; } if ($SmtpClient->auth) { $method = is_string($SmtpClient->auth) ? $SmtpClient->auth : ''; if (PEAR::isError($smtp->auth($SmtpClient->username, $SmtpClient->password, $method))) { trigger_error('unable to authenticate to smtp server', E_USER_ERROR); } } $from = is_array($Message->from) ? array_shift(array_values($Message->from)) : $Message->from; if (PEAR::isError($smtp->mailFrom($from))) { trigger_error('unable to set sender to [' . $from . ']', E_USER_ERROR); } $recipients = $SmtpClient->parseRecipients($Message->getRecipients()); if (PEAR::isError($recipients)) { return $recipients; } foreach ($recipients as $recipient) { if (PEAR::isError($res = $smtp->rcptTo($recipient))) { return PEAR::raiseError('unable to add recipient [' . $recipient . ']: ' . $res->getMessage()); } } if (PEAR::isError($smtp->data($Mailer->getRawMessage()))) { return PEAR::raiseError('unable to send data'); } $smtp->disconnect(); }
public function post(RFC5322Message $message) { $message->getHeader()->setValue("To", $this->recipient); $conn = new Net_SMTP($this->host->getHost(), $this->host->getPort()); $conn->connect(); $conn->mailFrom($this->bounceaddress); $conn->rcptTo($this->recipient); $conn->data($message->getPlain()); $conn->disconnect(); return true; }
function send_email($U) { #send_email is just a function that takes an email input, together with a host and send #INPUTS: $U = array($email, $subject, $message) =>EMAIL CAN BE AN ARRAY OF RECIPIENTS :-) extract($U); require_once S3DB_SERVER_ROOT . '/pearlib/Net/SMTP.php'; $host = $GLOBALS['s3db_info']['server']['email_host']; $from = '*****@*****.**'; if (!is_array($email)) { $email = array($email); } $rcpt = $email; $subj = 'Subject: ' . $subject; $body = $message; /* Create a new Net_SMTP object. */ if (!($smtp = new Net_SMTP($host))) { die("Unable to instantiate Net_SMTP object\n"); } /* Connect to the SMTP server. */ if (PEAR::isError($e = $smtp->connect())) { die($e->getMessage() . "\n"); } /* Send the 'MAIL FROM:' SMTP command. */ if (PEAR::isError($smtp->mailFrom($from))) { die("Unable to set sender to <{$from}>\n"); } /* Address the message to each of the recipients. */ foreach ($rcpt as $to) { if (PEAR::isError($res = $smtp->rcptTo($to))) { die("Unable to add recipient <{$to}>: " . $res->getMessage() . "\n"); } } /* Set the body of the message. */ if (PEAR::isError($smtp->data($subj . "\r\n" . $body))) { die("Unable to send data\n"); } /* Disconnect from the SMTP server. */ $smtp->disconnect(); }
/** */ public function send($recipients, array $headers, $body) { $headers = $this->_sanitizeHeaders($headers); // Prepare headers list($from, $textHeaders) = $this->prepareHeaders($headers); try { $from = $this->_getFrom($from, $headers); } catch (Horde_Mail_Exception $e) { $this->_error('no_from'); } // Prepare recipients foreach ($this->parseRecipients($recipients) as $rcpt) { list(, $host) = explode('@', $rcpt); $mx = $this->_getMx($host); if (!$mx) { $this->_error('no_mx', array('rcpt' => $rcpt)); } $connected = false; foreach (array_keys($mx) as $mserver) { $this->_smtp = new Net_SMTP($mserver, $this->_params['port'], $this->_params['mailname']); // configure the SMTP connection. if ($this->_params['debug']) { $this->_smtp->setDebug(true); } // attempt to connect to the configured SMTP server. $res = $this->_smtp->connect($this->_params['timeout']); if ($res instanceof PEAR_Error) { $this->_smtp = null; continue; } // connection established if ($res) { $connected = true; break; } } if (!$connected) { $this->_error('not_connected', array('host' => implode(', ', array_keys($mx)), 'port' => $this->_params['port'], 'rcpt' => $rcpt)); } // Verify recipient if ($this->_params['vrfy']) { $res = $this->_smtp->vrfy($rcpt); if ($res instanceof PEAR_Error) { $this->_error('failed_vrfy_rcpt', array('rcpt' => $rcpt)); } } // mail from: $args['verp'] = $this->_params['verp']; $res = $this->_smtp->mailFrom($from, $args); if ($res instanceof PEAR_Error) { $this->_error('failed_set_from', array('from' => $from)); } // rcpt to: $res = $this->_smtp->rcptTo($rcpt); if ($res instanceof PEAR_Error) { $this->_error('failed_set_rcpt', array('rcpt' => $rcpt)); } // Don't send anything in test mode if ($this->_params['test']) { $res = $this->_smtp->rset(); if ($res instanceof PEAR_Error) { $this->_error('failed_rset'); } $this->_smtp->disconnect(); $this->_smtp = null; return; } // Send data. Net_SMTP does necessary EOL conversions. $res = $this->_smtp->data($body, $textHeaders); if ($res instanceof PEAR_Error) { $this->_error('failed_send_data', array('rcpt' => $rcpt)); } $this->_smtp->disconnect(); $this->_smtp = null; } }
$host = 'mail.example.com'; $from = '*****@*****.**'; $rcpt = array('*****@*****.**', '*****@*****.**'); $subj = "Subject: Test Message\n"; $body = "Body Line 1\nBody Line 2"; /* Create a new Net_SMTP object. */ if (!($smtp = new Net_SMTP($host))) { die("Unable to instantiate Net_SMTP object\n"); } /* Connect to the SMTP server. */ if (PEAR::isError($e = $smtp->connect())) { die($e->getMessage() . "\n"); } $smtp->auth('username', 'password'); /* Send the 'MAIL FROM:' SMTP command. */ if (PEAR::isError($smtp->mailFrom($from))) { die("Unable to set sender to <{$from}>\n"); } /* Address the message to each of the recipients. */ foreach ($rcpt as $to) { if (PEAR::isError($res = $smtp->rcptTo($to))) { die("Unable to add recipient <{$to}>: " . $res->getMessage() . "\n"); } } /* Set the body of the message. */ if (PEAR::isError($smtp->data($subj . "\r\n" . $body))) { die("Unable to send data\n"); } /* Disconnect from the SMTP server. */ $smtp->disconnect();
function smtp_send($from, $recips, $body, $ehlo = "maia") { global $dbh; global $lang; $select = "SELECT smtp_server, smtp_port FROM maia_config WHERE id = 0"; $sth = $dbh->query($select); if ($row = $sth->fetchrow()) { $host = $row["smtp_server"]; $port = $row["smtp_port"]; } $sth->free(); $rcpt = explode(" ", $recips); /* Create a new Net_SMTP object. */ if (!($smtp = new Net_SMTP($host, $port, $ehlo))) { $smtp->disconnect(); return "550 " . $lang['error_connect'] . ": " . "Unable to instantiate Net_SMTP object\n"; } /* Connect to the SMTP server. */ if (PEAR::isError($e = $smtp->connect())) { $smtp->disconnect(); return "550 " . $lang['error_connect'] . ": " . $e->getMessage() . "\n"; } /* Send the 'MAIL FROM:' SMTP command. */ if (PEAR::isError($smtp->mailFrom($from))) { return "550 " . $lang['error_mail']; } /* Address the message to each of the recipients. */ foreach ($rcpt as $to) { if (PEAR::isError($res = $smtp->rcptTo($to))) { $smtp->disconnect(); return "550 " . $lang['error_rcpt'] . ": " . $res->getMessage() . "\n"; } } /* Set the body of the message. */ if (PEAR::isError($smtp->data($body))) { $smtp->disconnect(); return "550 " . $lang['error_body']; } $response = $smtp->getResponse(); /* Disconnect from the SMTP server. */ $smtp->disconnect(); return join(" ", $response); }
/** * Implements Mail::send() function using SMTP. * * @param mixed $recipients Either a comma-seperated list of recipients * (RFC822 compliant), or an array of recipients, * each RFC822 valid. This may contain recipients not * specified in the headers, for Bcc:, resending * messages, etc. * * @param array $headers The array of headers to send with the mail, in an * associative array, where the array key is the * header name (ie, 'Subject'), and the array value * is the header value (ie, 'test'). The header * produced from those values would be 'Subject: * test'. * * @param string $body The full text of the message body, including any * Mime parts, etc. * * @return mixed Returns true on success, or a PEAR_Error * containing a descriptive error message on * failure. * @access public */ function send($recipients, $headers, $body) { include_once 'Net/SMTP.php'; if (!($smtp = new Net_SMTP($this->host, $this->port))) { return new PEAR_Error('unable to instantiate Net_SMTP object'); } if (PEAR::isError($smtp->connect())) { return new PEAR_Error('unable to connect to smtp server ' . $this->host . ':' . $this->port); } if ($this->auth) { if (PEAR::isError($smtp->auth($this->username, $this->password))) { return new PEAR_Error('unable to authenticate to smtp server'); } if (PEAR::isError($smtp->identifySender())) { return new PEAR_Error('unable to identify smtp server'); } } list($from, $text_headers) = $this->prepareHeaders($headers); if (!isset($from)) { return new PEAR_Error('No from address given'); } if (PEAR::isError($smtp->mailFrom($from))) { return new PEAR_Error('unable to set sender to [' . $from . ']'); } $recipients = $this->parseRecipients($recipients); foreach ($recipients as $recipient) { if (PEAR::isError($res = $smtp->rcptTo($recipient))) { return new PEAR_Error('unable to add recipient [' . $recipient . ']: ' . $res->getMessage()); } } if (PEAR::isError($smtp->data($text_headers . "\n" . $body))) { return new PEAR_Error('unable to send data'); } $smtp->disconnect(); return true; }
/** * Implements Mail::send() function using SMTP. * * @param mixed $recipients Either a comma-seperated list of recipients * (RFC822 compliant), or an array of recipients, * each RFC822 valid. This may contain recipients not * specified in the headers, for Bcc:, resending * messages, etc. * * @param array $headers The array of headers to send with the mail, in an * associative array, where the array key is the * header name (e.g., 'Subject'), and the array value * is the header value (e.g., 'test'). The header * produced from those values would be 'Subject: * test'. * * @param string $body The full text of the message body, including any * Mime parts, etc. * * @return mixed Returns true on success, or a PEAR_Error * containing a descriptive error message on * failure. * @access public */ function send($recipients, $headers, $body) { include_once 'Net/SMTP.php'; if (!($smtp = new Net_SMTP($this->host, $this->port, $this->localhost))) { return new PEAR_Error('unable to instantiate Net_SMTP object'); } if (PEAR::isError($smtp->connect())) { return new PEAR_Error('unable to connect to smtp server ' . $this->host . ':' . $this->port); } if ($this->auth) { $method = is_string($this->auth) ? $this->auth : ''; if (PEAR::isError($smtp->auth($this->username, $this->password, $method))) { return new PEAR_Error('unable to authenticate to smtp server'); } } list($from, $text_headers) = $this->prepareHeaders($headers); /* * Since few MTAs are going to allow this header to be forged unless * it's in the MAIL FROM: exchange, we'll use Return-Path instead of * From: if it's set. */ if (!empty($headers['Return-Path'])) { $from = $headers['Return-Path']; } if (!isset($from)) { return new PEAR_Error('No from address given'); } if (PEAR::isError($smtp->mailFrom($from))) { return new PEAR_Error('unable to set sender to [' . $from . ']'); } $recipients = $this->parseRecipients($recipients); foreach ($recipients as $recipient) { if (PEAR::isError($res = $smtp->rcptTo($recipient))) { return new PEAR_Error('unable to add recipient [' . $recipient . ']: ' . $res->getMessage()); } } if (PEAR::isError($smtp->data($text_headers . "\r\n" . $body))) { return new PEAR_Error('unable to send data'); } $smtp->disconnect(); return true; }
function open_smtp(&$arr_mail_account, &$smtp, $account_no, $debug_flag) { // account list array, from config.php global $arrAccountsSmtp; // $account_noの範囲チェック if ($account_no <= 0 || $account_no > count($arrAccountsSmtp)) { printf("<p>アカウント指定Noが範囲外です<br/>account=%d</p>\n", $account_no); return false; } // メールアカウント情報(サーバ名、ユーザ名、パスワード等)を得る $arr_mail_account = GetMailAccount($arrAccountsSmtp[$account_no - 1][1], 'smtp'); if (!isset($arr_mail_account['server']) || !isset($arr_mail_account['user']) || !isset($arr_mail_account['password']) || strcmp($arr_mail_account['protocol'], 'smtp')) { print "<p>メールアカウント管理エラー(server/user/password値が得られないか、protocolがsmtpでない)</p>\n"; return false; } // 新しい Net_SMTP オブジェクトを作成します if (!($smtp = new Net_SMTP($arr_mail_account['server'], $arr_mail_account['port']))) { print "<p>ネットワーク異常(SMTPオブジェクト作成不能)</p>"; return false; } // デバッグ出力を、stdoutに送る if ($debug_flag) { $smtp->setDebug(true); } // SMTP サーバに接続します if (PEAR::isError($e = $smtp->connect())) { print "<p>ネットワーク異常(SMTPでコネクション不能)</p>"; return false; } // ユーザ名、パスワードを用いてSMTPログオンします // (Digest-MD5, CRAMMD5, LOGIN, PLAINの順に暗号強度の高いもので接続します) if (PEAR::isError($e = $smtp->auth($arr_mail_account['user'], $arr_mail_account['password']))) { $smtp->disconnect(); print "<p>SMTPサーバの認証に失敗</p>"; return false; } printf("<p>接続:%s@%s:%s</p>\n", $arr_mail_account['user'], $arr_mail_account['server'], $arr_mail_account['port']); return true; }
function deliver($catch_smtp_error = true) { global $pref, $atmail, $domains; $rcpt = array(); // Make an array with the email addresses to deliver the // message to /* $rcpt = array_merge($rcpt, preg_split('/;|,/', $this->EmailTo)); if (!empty($this->EmailCC)) $rcpt = array_merge($rcpt, preg_split('/;|,/', $this->EmailCC)); if (!empty($this->EmailBCC)) $rcpt = array_merge($rcpt, preg_split('/;|,/', $this->EmailBCC)); */ $rcpt = $this->EmailTo; if (!empty($this->EmailCC)) { $rcpt .= ",{$this->EmailCC}"; } if (!empty($this->EmailBCC)) { $rcpt .= ",{$this->EmailBCC}"; } // Remove personal part - it is not needed for send RCPT cmd // and just causes problems for the address parser if it contains // certain utf-8 chars (e.g. Japanese) $rcpt = preg_replace('/".+?"/', '', $rcpt); $rcpt = Mail_RFC822::parseAddressList($rcpt, null, false); $mail = array(); $groupmails = 0; // Loop through the non-existant recipients foreach ($rcpt as $entry) { // The entry is a group email-address if ($entry->host == 'Group') { $users = $this->abook->getgroup($entry->mailbox); foreach ($users as $user) { // Skip emailing the selected user, if local, and the user does not exist any longer if ($this->rcptOK($user)) { $groupmails++; array_push($mail, $user); } } } elseif ($entry->host == 'SharedGroup') { $users = $this->abook->getsharedgroup($entry->mailbox); foreach ($users as $user) { // Skip emailing the selected user, if local, and the user does not exist any longer if ($this->rcptOK($user)) { $groupmails++; array_push($mail, $user); } } } else { array_push($mail, stripslashes("{$entry->mailbox}@{$entry->host}")); } } // Block more then X outgoing recipients at a time ( useful to prevent spammers/bots abusing the service ) if (count($mail) - $groupmails > $pref['max_recipients_per_msg']) { $this->smtperror("Cannot send to more than {$pref['max_recipients_per_msg']} users per email message."); } require_once 'Net/SMTP.php'; $smtp = new Net_SMTP($pref['smtphost'], null, $_SERVER['HTTP_HOST']); //$smtp->setDebug(true); if ($smtp->connect(30) !== true) { if ($catch_smtp_error) { $this->smtperror("Error connecting to {$pref['smtphost']} - Check the hostname resolves, accepts incoming SMTP connections and is active."); } else { return false; } } // Check if we should use each account's username and password // for the SMTP if ($pref['smtp_per_account_auth']) { $pref['smtpauth_username'] = $atmail->auth->get_username(); $pref['smtpauth_password'] = $atmail->auth->get_password(); } // Optionally authenticate with the SMTP server if ($pref['smtpauth_username'] && $pref['smtpauth_password']) { if ($smtp->auth($pref['smtpauth_username'], $pref['smtpauth_password']) !== true) { if ($catch_smtp_error) { $this->smtperror("Error authenticating to {$pref['smtphost']} - Check the SMTP authentication is correct"); } else { $this->smtp_error_msg = "Error authenticating to {$pref['smtphost']} - Check the SMTP authentication is correct"; return false; } } } $smtp->mailFrom($this->EmailFrom); $fails = array(); //$smtp->setDebug(true); foreach ($mail as $v) { $res = $smtp->rcptTo($v); if (PEAR::isError($res)) { if ($catch_smtp_error) { $output = $res->getMessage(); $this->smtperror("Could not send message to SMTP server. Check you have access to send messages via the server and that all To/CC/BCC addresses are valid\\nError: {$output}"); break; } else { $fails[] = $v; continue; } } } $res = $smtp->data($this->headers . "\r\n\r\n" . $this->body); if (PEAR::isError($res)) { if ($catch_smtp_error) { $this->smtperror("Message rejected by SMTP server. Check message content and attachments\\nServer Responded: " . $res->getMessage()); } else { $this->smtp_error_msg = "Message rejected by SMTP server. Check message content and attachments\nServer Responded: " . $res->getMessage(); return false; } } elseif ($pref['install_type'] == 'standalone') { // Only log sent msgs if running only as webmail client // as Exim already does this when in server mode foreach ($mail as $addr) { $addr = preg_replace('/\\s+/', '', $addr); $this->log->write_log("SendMail", "WebMail:{$_SERVER['REMOTE_ADDR']}:{$addr}"); } } $smtp->disconnect(); // Reset some fields back to the raw UTF8 copy, since it's sent to the browser $this->EmailSubject = $this->RawEmailSubject; return $fails; }
/** * Sends and e-mail using Net_SMTP. * * @param Email $email * @return string Error message, or null upon success. */ public static function smtp(&$email) { /* if ( isset($GLOBALS['phpmail']) && $GLOBALS['phpmail'] ) { $rfc = new \Mail_RFC822(); $pureAddresses = $rfc->parseAddressList($email->to()); foreach( $pureAddresses as $address ) { $to = $address->mailbox. "@". $address->host; mail( $to, $email->subject(), $email->body(), $email->headers() ); } return; } */ Log::debug("Mailer: subject:[" . $email->subject() . "], from:[" . $email->from() . "], to:" . print_r($email->recipients(), true)); if (!Config::$smtpHost) { $error = "Mailer: no SMTP host supplied"; Log::debug($error); return $error; } if (!($smtp = new \Net_SMTP(Config::$smtpHost, Config::$smtpPort, $_SERVER['SERVER_NAME']))) { $error = "Mailer: unable to instantiate Net_SMTP object"; Log::debug($error); return $error; } // $smtp->setDebug(true); $pear = new \PEAR(); if ($pear->isError($e = $smtp->connect())) { $error = "Mailer: connect error: " . $e->getMessage(); Log::debug($error); return $error; } if (Config::$smtpUser && $pear->isError($e = $smtp->auth(Config::$smtpUser, Config::$smtpPass, Config::$smtpAuthType))) { $error = "Mailer: authentication error: " . $e->getMessage(); Log::debug($error); return $error; } $rfc = new \Mail_RFC822(); $pureAddresses = $rfc->parseAddressList($email->from()); $from = $pureAddresses[0]->mailbox . "@" . $pureAddresses[0]->host; if ($pear->isError($e = $smtp->mailFrom($from))) { $error = "Unable to set sender to [{$from}]: " . $e->getMessage(); Log::debug($error); return $error; } foreach ($email->recipients() as $toAddress) { $pureAddresses = $rfc->parseAddressList($toAddress); foreach ($pureAddresses as $address) { $to = $address->mailbox . "@" . $address->host; if ($pear->isError($e = $smtp->rcptTo($to))) { $error = "Unable to set recipient to [{$to}]: " . $e->getMessage(); Log::debug($error); return $error; } } } if ($pear->isError($e = $smtp->data($email->data()))) { $error = "Unable to set data: " . $e->getMessage(); Log::debug($error); return $error; } Log::debug("Mailer: sent: " . $smtp->_arguments[0]); $smtp->disconnect(); return null; }