• I have successfully sent a lot of emails through wp_mail, but I have now ran into a weird issue concerning URL:s. The email is sent as a result of a scheduled single event, which is executed by a cron job on my server with WP cron disabled:

    cd /home/username/public_html; /usr/local/bin/php -q wp-cron.php

    A URL for a post is a part of the email content, and the problem is that it automatically gets re-written from

    https://mysite.com/assignments/2018/09/21/12371237/”

    to

    “https:/assignments/2018/09/21/12371237/”

    somewhere between wp_mail() and my inbox. So basically “/mysite.com/” gets stripped from the URL. This occurs even when skipping get_permalink and hard coding the URL as “https://mysite.com/assignments/2018/09/21/12371237/”, so it is not a problem with get_permalink. The email contains HTML, but even when just executing the following simple example this occurs. The rest of the HTML turns out fine though, so it’s just the URL that gets messed with.

    $postURL = get_permalink(15375225168281);
    $body = 'URL: ' . $postURL;
    $headers[] = 'From: Me <[email protected]>';
    $headers[] = 'Content-Type: text/html; charset=UTF-8';
    wp_mail('[email protected]', 'Titel', $body, $headers);

    When I send this email from anywhere else on my site however, which is not the result of a cron job, I don’t have this problem. Any ideas why this happens?

    • This topic was modified 6 years, 6 months ago by minisemi.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Sounds like a security measure to me, though I’ve no idea.

    I do have an idea for a workaround. Since you are sending HTML, you should be able to % encode the domain characters and everything should still appear and function normally.
    mysite => %6D%79%73%69%74%65

    Also encode the https:// so the security parser hopefully does not even recognize it as a link. Unverified, but perhaps worth a try?

    Thread Starter minisemi

    (@minisemi)

    Hi bcworkz,

    Thanks for the suggestion. I tried that now, but having this as input:

    <a href="%68%74%74%70%73%3A%2F%2F%78%2E%63%6F%6D">Link</a>

    genereates this as output:

    [%68%74%74%70%73%3A%2F%2F%78%2E%63%6F%6D]Link

    So it seems as if if it, apart from stripping the URL, also replaces the link element with “[]”… This is also the case with my initial scenario.

    • This reply was modified 6 years, 6 months ago by minisemi.
    Moderator bcworkz

    (@bcworkz)

    Well, poo, that’s disappointing. Other than disabling the security or whatever that is, I don’t see a solution. It’s quite strange indeed. Since this apparently happens post WP, using an alternative email library wouldn’t help. The out going email packets are going to appear the same no matter how they are constructed.

    All I can think of is to re-enable WP_Cron and use that instead of CRON job. I suppose you have a good reason for disabling in the first place, so that’s not really a viable solution.

    Dion

    (@diondesigns)

    PHP executed from the command line is not the same as PHP executed by a webserver. In addition to having different sets of super-globals, they are different executables. This means the command line version of PHP could have different extensions installed, and it could even be a different version than the one used by the webserver.

    Please go to the command line and capture the output of the php -i command. Then compare that output to what you see when phpinfo() is displayed from a web address. Check specifically for differences in the mail/sendmail and url_rewriter configurations — the CLI versions of these should match the webserver versions.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘wp_mail filtering a URL’ is closed to new replies.