Home > Blockchain >  Send emails via SMTP in wordpress without any plugin
Send emails via SMTP in wordpress without any plugin

Time:09-16

I have been trying for several days to send emails in wordpress without using plugins. Using the wp_mail function I get error 500, before I used the mail function and it worked, but I have been recommended to use this, I have also read on the official wordpress page that it is the recommended one.

The mail.php file is located in the root directory of wordpress. This is my code:

Form->

<form method="post" action="<?php echo get_template_directory_uri(); ?>/php/processor.php" id="formulario">
    <div >
        <input type="text"  name="nombre" placeholder="Nombre" required>
    </div>
    <div >
        <input type="email"  name="email" placeholder="Email" required>
    </div>
    <div >
        <input type="text"  name="asunto" placeholder="Asunto" required>
    </div>
    <div >
        <textarea  name="mensaje" rows="3" placeholder="Mensaje" required></textarea>
    </div>
    <div >
        <input type="checkbox"  name="privacidad" required>
        <label >He leido y acepto la <a href="/politica-de-privacidad">política de privacidad</a></label>
    </div>
    <div >
        <button type="submit" name="submitted" id="sendForm" >Enviar mensaje</button>
    </div>
</form>

mail.php->

<?php

require_once('../../../../wp-load.php');

if (isset($_POST['submitted'])) {

   $nombre = $_POST['nombre'];
   $email = $_POST['email'];
   $asunto = $_POST['asunto'];
   $mensaje = $_POST['mensaje'];

   $from = '[email protected]';

   $headers = array(
      "Content-Type: text/html; charset=UTF-8",
      "Sender: $email",
      "From: Tuwebeconomica.es <$from>",
      "Reply-To: $nombre <$email>"
   );

   // wp_mail($emailTo, $asunto, $body);
   wp_mail($from, 'Nuevo mensaje de contacto desde tuweb.es', $body, $headers);

   // Se redirecciona a la página de contacto
   header('Location:' . getenv('HTTP_REFERER') . '?success=true');
}

As I said before I have tried the wp_mail function, mail, and phpmailer. I think I'm doing something wrong. Any help would be greatly appreciated. Thank you very much.

CodePudding user response:

When looking for an answer like this, always look at the package docs first, which could have saved the time you wasted.

Though you can take on all of this yourself, this isn't the best way to work with PHPMailer in Wordpress. Wordpress bundles PHPMailer, and you can make use of it instead of starting from scratch.

You need to write a hook function that intercepts the setup function that WordPress alley to configure its mailer. This lets you configure it as you like:

add_action('phpmailer_init', function ($mailer){
  $mailer->isSMTP();
  $mailer->Host = "mail.example.com"; // your SMTP server
  $mailer->Port = 25;
  $mailer->SMTPDebug = 2;
  $mailer->CharSet  = "utf-8";
  //etc
});

Once that is done, you can call the wp_mail function, and it will use your config instead of WordPress' default settings.

CodePudding user response:

The problem wasn't the wp_mail() function, it was using it correctly. I missed importing the following line:

require_once('../../../../wp-load.php');

The question is corrected, maybe it can help someone else.

  • Related