2

I am using a send email with php. When a user opens my email message, there i send some text and the link. I wan't, that if a user using a iphone - there will be a link on the istore, or if he is using android - there will be a link to play market, or if he is using a web browser (PC) - it will show a link to site.

How can i create these cases? I had a idea to use js, but i can't send a js code with email message.

  • 3
    As you can't embed PHP in e-mails either, are you detecting the users platform before sending the e-mail? Why not just include all links just in case he browses your site on their phone but gets e-mails on their desktop? – Hex Jul 18 '18 at 09:26
  • When you have sent a mail with php how you would know which browser the client will use in future to open this mail? You must need JS otherwise no way to do it. – asifsaho Jul 18 '18 at 10:00

2 Answers2

1

You can use user-agent and detect the users's device in JavaScript.

/**
 * Determine the mobile operating system.
 * This function returns one of 'iOS', 'Android', 'Windows Phone', or 'unknown'.
 *
 * @returns {String}
 */
function getMobileOperatingSystem() {
  var userAgent = navigator.userAgent || navigator.vendor || window.opera;

      // Windows Phone must come first because its UA also contains "Android"
    if (/windows phone/i.test(userAgent)) {
        return "Windows Phone";
    }else if (/android/i.test(userAgent)) {
        return "Android";
    }else if (/iPad|iPhone|iPod/.test(userAgent) && !window.MSStream) {
        return "iOS";
    }else if (screen.width >= 480) {
        return "PC"
    }

    return "unknown";
}

However, in your case it is not necessary to detect the user's device or operating system. As it was mentioned in the comments, why don't you just put all the links there and let the user choose their prefered platform?

One the other hand, this can be easily played out by sending and email on a certain device and check the same email on another one, for example.

Nonetheless, JavaScript can be manipulated by the users since it's running on the client-side, so for security reasons you may want to use a server-side detection with PHP, or a prebuilt mobile-checking libary.

Reference

How to detect mobile or PC users?
How to detect operating system?

squancy
  • 565
  • 1
  • 7
  • 25
  • That could at most work up-front though, at the moment where the user is on the web site and triggers the sending out of this e-mail. _Inside_ an actual e-mail, you can forget about executing your own JavaScript, that will be blocked nearly everywhere. – CBroe Jul 18 '18 at 09:39
  • And what if the right link is chosen before actually sending the email? – squancy Jul 18 '18 at 09:41
  • Well then the question would be whether it still _is_ the “right link” at the time I check my e-mails … which might be on a totally different device. – CBroe Jul 18 '18 at 09:49
  • That's right and that's why I suggested to put every link there and let the user choose the proper one. – squancy Jul 18 '18 at 09:51
0

You could use the get_browser()
or get the $_SERVER['HTTP_USER_AGENT'] for OS and browser.
Keep in mind, that this info can be spoofed.

If you have this info you can cross check the user agent with what link you want to send. Here is a list of possible useragents

veritaS
  • 511
  • 1
  • 5
  • 23
  • I think this is not possible, he would't know all the time which email client the user will use to open the mail. It has to be done with JS, Thank you – asifsaho Jul 18 '18 at 09:43
  • I actually ment before sending the email. He obvoisly cannot(should not) send a script in mail. He could however also send a mail to a simple html landing page where he checks this info and prints out the correct link. – veritaS Jul 18 '18 at 09:45
  • JS works in email client, not guaranteed though. I think it should be with JS otherwise no other option because user can open mail from iPhone and android both devices which will cause wrong version of email as its static. – asifsaho Jul 18 '18 at 09:49
  • I did not consider this. I would however not send scripts in mails for fear that the mail server is complaining and putting me to spam or worse... get listed on official filter lists cause of potential exploit mails. Like our admin so heroically achived... I would simply check this before sending the mail, if that is possible in his usecase. He also said he cannot send js script in mail no? – veritaS Jul 18 '18 at 09:56