• Hello, I’m doing my first WordPress template. I realized that the mail (via the contact form for example) is always in spam (with Outlook and Gmail) or is not even received (1and1). Here is the method I used:

    if ($form_valide){
    $sent = wp_mail($to, $subject, $body, $headers);
    }

    What advise you to send an email that would have more change to be received. How to send an SMTP mail with WordPress without using a Plugin?

    • This topic was modified 6 years, 9 months ago by anUser.
Viewing 11 replies - 1 through 11 (of 11 total)
  • Dion

    (@diondesigns)

    WordPress by default uses the internal PHP mail() function to send mail, and for some reason WP uses a sender address that does not exist on servers. While that caused no problems back in the WP1.x days, in this day and age it will result in your domain and/or IP address added to blacklists.

    IMO the current state of the wp_mail() function in WordPress is worse than having no email capabilities whatsoever. Adding a dedicated email plugin would be both better and easier than having to deal with getting removed from blacklists.

    To answer your question, unless you add your own code to configure PHPMailer to send mail via SMTP, you will need a plugin to send mail via SMTP.

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    Basically, yes, what Dion said. You’d have to write a plugin to do SMTP so why not use one that someone else is maintaining? ??

    Thread Starter anUser

    (@anuser)

    Thank you for your answers, the main reason that pushes me not to use a plugin already realize is that I’m doing a template and I do not want to put in my template plugins made by other people.

    • This reply was modified 6 years, 9 months ago by anUser.
    • This reply was modified 6 years, 9 months ago by anUser.
    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    “Doing a template”? If you mean making a theme, it would be inappropriate to include SMTP functionality in a theme. It belongs in a plugin.

    Moderator bcworkz

    (@bcworkz)

    FWIW, you can change the From: address/name that wp_mail() uses by default to something that is actually valid for your domain with the ‘wp_mail_from’ and ‘wp_mail_from_name’ filters. That should help some. One of the common anti-spam mail headers needs to be inserted somewhere along the way. I believe it’s normally done by your host’s mail server, but maybe only for valid From: addresses it knows about.

    You don’t want to manage your own SMTP, doing so correctly is far from easy. Some sort of well known library should be used to avoid even more problems with blacklists. Naturally, be sure your server is not actually spamming people. That’s a sure way to get blacklisted ??

    Dion

    (@diondesigns)

    It’s kinda technical, but by default, WordPress sets an incorrect “return path” when sending emails. The return path is checked by many/most mail servers, and if it’s not valid, the email is rejected and the IP/domain of the sending server could be added to a blacklist.

    The “return path” is set by using the sender option in PHPMailer, and there are no filters to set that option. It has to be set via the “phpmailer_init” action hook. Thing is, if you’re going to use that hook, you may as well configure PHPMailer to use SMTP to send the email. It’s a lot simpler than most realize since PHPMailer does all the heavy lifting…

    Thread Starter anUser

    (@anuser)

    @sterndata, this is a theme that also contains plugin that I created for the running of the theme.

    • This reply was modified 6 years, 9 months ago by anUser.
    • This reply was modified 6 years, 9 months ago by anUser.
    Thread Starter anUser

    (@anuser)

    @bcworkz, I used the filters ‘wp_mail_from’ and ‘wp_mail_from_name’, it’s cleaner, but it does not change the fact that the mail goes into the pams or is not received.

    Thread Starter anUser

    (@anuser)

    @diondesigns, the code below solves my problem :

    `function init_smtp( $phpmailer ) {
    $phpmailer->isSMTP();
    $phpmailer->Host = ‘smtp.example.com’;
    $phpmailer->SMTPAuth = true;
    $phpmailer->Port = 25; // May need to be changed
    $phpmailer->Username = ‘username’;
    $phpmailer->Password = ‘password’;
    }

    add_action( ‘phpmailer_init’, ‘init_smtp’ );

    However, here it is a template that I build. the user should easily find $ phpmailer-> Host, $ phpmailer-> Username and $ phpmailer-> Password ?

    Dion

    (@diondesigns)

    Almost no mail server these days allows auth on port 25, so I’m sure you will need to change the port, and you will probably need to set $phpmailer->SMTPSecure.

    Otherwise, what you have is pretty much all that’s needed to use SMTP in WordPress. (The most popular SMTP plugin uses almost the same code…albeit in a more user-friendly fashion, and with lots of ads and/or nag screens.)

    I’m not sure what you’re asking in your question. Users will not be able to see the SMTP credentials since they’re in a PHP file. If you want to increase security, store the SMTP login credentials in your database and load them into your script when needed.

    Thread Starter anUser

    (@anuser)

    @diondesigns, thank you for your answer which is very beneficial. myself experienced $ phpmailer-> Port and $ phpmailer-> SMTPSecure and what you wrote is correct.

    In the question that I asked in my previous message, I wanted to say that it is not obvious for a lambda user (a website designer with wordpress is not necessarily a professional) to find the coordinates STMP …

    ? I also looked to make a plugin ‘SMTP’, I did for gmail and 1and1 … I have more difficulty with outlook for example. But I did not spend a lot of time on ??

    • This reply was modified 6 years, 9 months ago by anUser.
Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Send mail with STMP’ is closed to new replies.