Posted By

cerxx on 09/11/19


Tagged

HTML-FORMS


Versions (?)

Send by SMTP newly


 / Published in: PHP
 

URL: https://unihost.com/help/ru/php-script-to-send-email-using-smtp/

Despite the fact that php has a ready-made function for sending mail mail (), the SMTP mail sending script has great capabilities. Most often, mail is sent via SMTP in order to bypass anti-spam filters - many of them are accustomed to consider emails sent directly from PHP as suspicious.

  1. <?php
  2. $domain = $_SERVER['HTTP_HOST'];
  3. $subject = html_entity_decode(sprintf($this->language->get('email_subject'), $this->request->post['name']), ENT_QUOTES, 'UTF-8');
  4. $text = 'from E-mail: ' . $this->request->post['email'] . ' Message: ' . $this->request->post['enquiry'];
  5. $__smtp = array(
  6. "host" => $this->config->get('config_mail_smtp_hostname'),
  7. "debug" => 0,
  8. "auth" => true,
  9. "port" => $this->config->get('config_mail_smtp_port'),
  10. "username" => $this->config->get('config_mail_smtp_username'),
  11. "recipient" => 'To admin',
  12. "password" => html_entity_decode($this->config->get('config_mail_smtp_password'), ENT_QUOTES, 'UTF-8'),
  13. "reply-to" => $this->request->post['email'],
  14. "to" => $this->config->get('config_email'),
  15. //"to" => "[email protected]",
  16. "debug" => 0
  17. );
  18. function get_data($smtp_conn){
  19. $data="";
  20. while($str = fgets($smtp_conn,515)){
  21. $data .= $str;
  22. if(substr($str,3,1) == " ") { break; }
  23. }
  24. return $data;
  25. }
  26. $header.="X-Priority: 3 (Normal)\r\n";
  27. $header.="Message-ID: <172562218.".date("YmjHis")."@" .$domain .">\r\n";
  28. $header.="To: =?UTF-8?Q?".str_replace("+","_",str_replace("%","=",urlencode($__smtp['recipient'])))."?= <" . $__smtp['to'] . ">\r\n";
  29. $header.="Subject: =?UTF-8?Q?".str_replace("+","_",str_replace("%","=",urlencode($subject)))."?=\r\n";
  30. $header.="MIME-Version: 1.0\r\n";
  31. $header.="Content-Type: text/plain; charset=UTF-8\r\n";
  32. $header.="From: Aroterra <" . $__smtp['username'] . ">\r\n";
  33. $header.="Reply-To: " . $__smtp['reply-to'] . "\r\n";
  34. $header.="Content-Transfer-Encoding: 8bit\r\n";
  35. $smtp_conn = fsockopen($__smtp['host'], $__smtp['port'],$errno, $errstr, 10);
  36. $smtp_log = get_data($smtp_conn) . "\r\n";
  37. fputs($smtp_conn,"EHLO aroterra\r\n");
  38. $smtp_log .= get_data($smtp_conn) . "\r\n";
  39. fputs($smtp_conn,"AUTH LOGIN\r\n");
  40. $smtp_log .= get_data($smtp_conn);
  41. fputs($smtp_conn,base64_encode($__smtp['username'])."\r\n");
  42. $smtp_log .= get_data($smtp_conn);
  43. fputs($smtp_conn,base64_encode($__smtp['password'])."\r\n");
  44. $smtp_log .= get_data($smtp_conn);
  45. $size_msg=strlen($header."\r\n".$text);
  46. fputs($smtp_conn,"MAIL FROM: <" . $__smtp['username'] . "> SIZE=".$size_msg."\r\n");
  47. $smtp_log .= get_data($smtp_conn);
  48. fputs($smtp_conn,"RCPT TO:<" . $__smtp['to'] . ">\r\n");
  49. $smtp_log .= get_data($smtp_conn);
  50. fputs($smtp_conn,"DATA\r\n");
  51. $smtp_log .= get_data($smtp_conn);
  52. fputs($smtp_conn,$header."\r\n".$text."\r\n.\r\n");
  53. $smtp_log .= get_data($smtp_conn);
  54. fputs($smtp_conn,"QUIT\r\n");
  55. $smtp_log .= get_data($smtp_conn);
  56. if($__smtp["debug"]){
  57. $this->log->write($smtp_log);
  58. }
  59. ?>

Report this snippet  

You need to login to post a comment.