Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Wysyłanie maila oraz sprawdzenie czy nie zwrócił błedu.
Forum PHP.pl > Forum > PHP
casperii
Panowie zrobiłem potwierdzenie aktywacji konta, weryfikacją adresu email. Wymyśliłem sobie, że fajnie by było zrobić coś takiego, że z chwilą gdy użytkownik wpiszę swój adres email otrzymałby od razu informację , że np był problem z doręczeniem do niego wiadomości. No i tu nasuwa się kilka pytań , czy np po wysłaniu requesta z wysyłką maila zrobić np 60 sekundowy loader gdzie w tym czasie czytam sobie skrzynkę, czy otrzymałem zwrotkę o nie dostarczeniu wiadomości, czy istnieje jakiś inny sposób ?
Bo co w sytuacji jak ktoś mi JS wyłączy. Aha całość robię w frameworku Laravel być może macie jakieś sprawdzone sposobu ?

Czyli podsumowując, formularz -> wyślij -> czytam czy w skrzynke mam informację o nie dostarczeniu - >
TAK - wyświetla informację o błędzie
NIE - weryfikacja potwierdzona
vokiel
Na 100% nie sprawdzisz czy klient dostał wiadomość, bo może trafić do spamu i nie dostaniesz zwrotki.

Zrób walidację adresu i jeśli będzie poprawna to zwracaj info czy udało się wysłać maila czy nie. To czy dotrze już od Ciebie nie zależy (problemy sieci, serwera odbiorcy, konfiguracja konta etc - rzeczy na które nie masz wpływu).

Przy walidacji sprawdź np czy domena ma rekordy MX - będzie większa pewność czy jest tam serwer pocztowy.
casperii
@trueblue po wnikliwej analizie widzę, że ów sposób zaproponowany przez Ciebie (link) nie zdaje egzaminu np z pocztą na o2.pl sad.gif

Znalazłem jeszcze inny analizator maila, już nawet oparty o Laravel : link tu

Niestety Laravela dopiero się uczę i nie wiem do końca jak to rozgryźć, ponieważ mam już własną walidację , teoretycznie wiem jak to ma wyglądać ale w praktyce gorzej.

chodzi o fragment kodu:

  1. $validateemail = new Validateemail;
  2. try {
  3. if (!$validateemail->test($request->input("email"))) {
  4. return redirect()->back()->withErrors(["email" => "Invalid Email Address"]);
  5. }
  6. }
  7. catch (\ServiceTo\ValidateEmailException $vee) {
  8. return redirect()->back()->withErrors(["email" => "Invalid Email Address (" . $vee->getMessage() . ")"]);
  9. }



który powinien być (chyba) w metodzie validator (?)
  1. protected function validator(array $data)
  2. {
  3. return Validator::make($data, [
  4. 'user' => 'required|string|min:3|max:255|unique:users',
  5. 'email' => 'required|string|min:5|email|max:255|unique:users',
  6. 'password' => 'required|string|min:6||regex:/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{6,}$/',
  7. 'password_confirmation' => 'required|same:password',
  8. 'type' => 'required|in:0,1',
  9. ]);
  10. }
  11.  
  12.  
  13. protected function create(array $data)
  14. {
  15. $user = User::create(
  16. [
  17. 'user' => $data['user'],
  18. 'email' => $data['email'],
  19. 'password' => bcrypt($data['password']),
  20. 'ip' => request()->ip(),
  21. ]
  22. );
  23. return $user;
  24. }
  25.  
  26.  
  27. public function register(Request $request)
  28. {
  29. $this->validator($request->all())->validate();
  30. $user = $this->create($request->all());
  31.  
  32. event(new Registered($user));
  33. $this->guard()->login($user);
  34. UserVerification::generate($user);
  35. UserVerification::send($user, 'Potwierdzenie rejestracji konta.', env('MAIL_FROM_ADDRESS'), env('APP_NAME'));
  36.  
  37.  
  38. $user = Event::create([
  39. 'user_id' => $user->id,
  40. 'event' => 1, // 1 - Rejestracja konta
  41. 'description' => 'Rejestracja konta',
  42. 'ip' => request()->ip(),
  43. ]);
  44.  
  45. return $this->registered($request, $user)
  46. ?: redirect($this->redirectPath());
  47. }
viking
Przecież validator LV ma też opcję sprawdzania DNS, czytałeś dokumentację? https://laravel.com/docs/8.x/validation#rule-email
Poza tym https://laravel.com/docs/8.x/validation#cre...g-form-requests
casperii
Tak doczytałem, ale to już od wyższej wersji, nie chce updatować do wyższej wersji by coś się nie posypało.

@viking czy ze chciałbyś spojrzeć w mój kod, nie rozumiem dlaczego wywala mi , że brak klasy.

  1. protected function validator(array $data)
  2. {
  3. return Validator::make($data, [
  4. 'email' => new MyOwnEmail()
  5. ]);
  6. }
  7.  
  8. public function register(Request $request)
  9. $this->validator($request->all())->validate();
  10. {


czy też tak:

  1. public function register(Request $request)
  2. {
  3. $request->validate([
  4. 'email' => [new MyOwnEmail],
  5. ]);



plik MyOwnEmail.php
  1. <?php
  2.  
  3. namespace App\Rules;
  4.  
  5. use Illuminate\Contracts\Validation\Rule;
  6. use ServiceTo\ValidateEmail;
  7.  
  8. class MyOwnEmail implements Rule
  9. {
  10. /**
  11.   * Create a new rule instance.
  12.   *
  13.   * @return void
  14.   */
  15. public function __construct()
  16. {
  17. //
  18. }
  19.  
  20. public function passes($attribute, $value)
  21. {
  22. //
  23. $validateEmail = new ValidateEmail();
  24. return $validateEmail->test($value);
  25. }
  26.  
  27. public function message()
  28. {
  29. return 'error message.';
  30. }
  31. }


plik ValidateEmail.php

  1. <?php
  2. namespace ServiceTo;
  3.  
  4. use Exception;
  5.  
  6. class ValidateEmail {
  7. /**
  8.   * The address to supply in our MAIL FROM connection to the SMTP servers we're talking to.
  9.   *
  10.   * @var string
  11.   */
  12. public $testaddress = "localhost";
  13.  
  14. /**
  15.   * Alias to lookup
  16.   *
  17.   * @param string $emailaddress email address to test
  18.   * @return boolean
  19.   */
  20. function test($emailaddress) {
  21. return $this->lookup($emailaddress);
  22. }
  23.  
  24. /**
  25.   * Lookup all MX records and check each until we have a success
  26.   *
  27.   * @param string $emailaddress email address to look up
  28.   * @return boolean
  29.   */
  30. function lookup($emailaddress) {
  31. $user = "";
  32. $domain = "";
  33. if (strpos($emailaddress, "@")) {
  34. list($user, $domain) = preg_split("/@/", trim($emailaddress));
  35. }
  36. else {
  37. throw new ValidateEmailException("No at sign to work with");
  38. }
  39.  
  40. if ($user == "") {
  41. throw new ValidateEmailException("Blank user name");
  42. }
  43. if ($domain == "") {
  44. throw new ValidateEmailException("Blank domain name");
  45. }
  46.  
  47. $mxhosts = array();
  48. $weight = array();
  49. if (getmxrr($domain, $mxhosts, $weight)) {
  50. // pick first one and check it.
  51.  
  52. array_multisort($weight, $mxhosts);
  53.  
  54. foreach ($mxhosts as $id => $mxhost) {
  55. if ($this->verify($emailaddress, $mxhost)) {
  56. return true;
  57. }
  58. }
  59. }
  60. else {
  61. throw new ValidateEmailException("No MX records");
  62. }
  63. }
  64.  
  65. /**
  66.   * Connect to the mail server on port 25 and see if it allows mail for the users' supplied email address.
  67.   *
  68.   * @param string $emailaddress email address to test
  69.   * @param string $mxhost mail server host name to connect to and test
  70.   * @return boolean
  71.   */
  72. function verify($emailaddress, $mxhost) {
  73. try {
  74. $validated = false;
  75.  
  76. $opts = array('socket' => array('bindto' => '0:0'));
  77. $ctx = stream_context_create($opts);
  78. $socket = stream_socket_client("tcp://" . $mxhost . ":25", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $ctx);
  79. stream_set_blocking($socket, true);
  80.  
  81. $buffer = null;
  82. // server will say hi...
  83. $buffer .= fgets($socket, 2048);
  84.  
  85. $response = trim($buffer, "\r\n.");
  86. $buffer = null;
  87.  
  88. list($code, $message) = preg_split("/\s+/", $response, 2);
  89. if (substr($code, 3, 1) == "-") {
  90. // they're still talking...
  91.  
  92. $buffer .= fgets($socket, 2048);
  93.  
  94. $response = trim($buffer, "\r\n.");
  95. list($code, $message) = preg_split("/\s+/", $response, 2);
  96. }
  97.  
  98. if ($code == 220) {
  99. // say hello.
  100. $message = "EHLO ValidateEmail.service.to\r\n";
  101. fwrite($socket, $message);
  102.  
  103. while($buf = fgets($socket)) {
  104. $buffer .= $buf;
  105. list($code, $message) = preg_split("/\s+/", $buf, 2);
  106. if ($code == "250") {
  107. break;
  108. }
  109. }
  110. $response = trim($buffer, "\r\n");
  111. $buffer = null;
  112.  
  113. $lines = preg_split("/\n/", $response);
  114. $last = count($lines) - 1;
  115. list($code, $message) = preg_split("/\s+/", $lines[$last], 2);
  116. if ($code == 250) {
  117. // give them my from address
  118. $message = "MAIL FROM:<" . $this->testaddress . ">\r\n";
  119. fwrite($socket, $message);
  120.  
  121. $buffer .= fgets($socket, 2048);
  122.  
  123. $response = trim($buffer, "\r\n");
  124. $buffer = null;
  125.  
  126. list($code, $message) = preg_split("/\s+/", $response, 2);
  127.  
  128. if ($code == 250) {
  129. // give them the user's address.
  130. $message = "RCPT TO:<" . $emailaddress . ">\r\n";
  131. fwrite($socket, $message);
  132.  
  133. $buffer .= fgets($socket, 2048);
  134.  
  135. $response = trim($buffer, "\r\n");
  136. $buffer = null;
  137.  
  138. list($code, $message) = preg_split("/\s+/", $response, 2);
  139. if ($code == 250) {
  140. $validated = true;
  141. }
  142.  
  143. // say goodbye regardless
  144. $message = "QUIT\r\n";
  145. fwrite($socket, $message);
  146.  
  147. $buffer .= fgets($socket, 2048);
  148.  
  149. $response = trim($buffer, "\r\n");
  150. $buffer = null;
  151.  
  152. list($code, $message) = preg_split("/\s+/", $response, 2);
  153.  
  154. return $validated;
  155. }
  156. }
  157. }
  158. }
  159. catch (Exception $e) {
  160. throw new ValidateEmailException($e->getMessage());
  161. }
  162. }
  163. }
  164.  
  165. class ValidateEmailException extends Exception {}


viking
Zaimportowałeś ją? Jest dostępna gdzieś wewnątrz autoloada?
casperii
pytasz o composera ?

  1. {
  2. "name": "laravel/laravel",
  3. "description": "The Laravel Framework.",
  4. "keywords": ["framework", "laravel"],
  5. "license": "MIT",
  6. "type": "project",
  7. "require": {
  8. "php": ">=7.0.0",
  9. "laravel/framework": "5.5.*",
  10. "service-to/validate-email": "^1.0"
  11. },
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.