programing

JQuery / JavaScript와 연결할 메일 호출 / 클릭

minimums 2023. 9. 25. 22:29
반응형

JQuery / JavaScript와 연결할 메일 호출 / 클릭

자바스크립트에서 링크할 메일을 호출하고 싶습니다. 즉, 사용자 PC에서 마치 일반 메일을 클릭하여 링크할 수 있는 것처럼 이메일 클라이언트를 열 수 있는 방법을 원합니다.

이거 어떻게 해요?

사용가능window.location.href여기, 다음과 같이:

window.location.href = "mailto:address@dmail.com";

대신 페이지에 링크가 있는 .click()을 사용하여 위에서 설명한 빈 페이지 문제를 방지할 수 있습니다.

document.getElementById('mymailto').click();
...
<a href="mailto:...." id="mymailto" style="display:none"></a>

크롬, IE, 파이어폭스 등에서 테스트를 받은 나를 위한 작업 답변은 이것이었습니다.

window.location.href = 'mailto:address@dmail.com?subject=Hello there&body=This is the body';

%0d%0a링크할 메일에 있는 이메일 본문의 새 줄 기호입니다.

%20사용해야 할 공간 기호이지만, 일반적인 공간에서도 사용할 수 있었습니다.

사용하기 좋음

window.open('mailto:address@mail.com?subject=sub&body=this is body')

사용하면window.location.href네트워크 탭에서 크롬 브라우저에 오류가 발생하고 임시 헤더가 Upgrade-Insecure-Requests: 1로 표시됨

사실 빈 페이지를 피할 수 있는 가능성이 있습니다.

알았어요, 그냥 메일로 아이프레임을 삽입해서 돔에 연결하면 됩니다.현재 Firefox/Chrome 및 IE에서 작동합니다(IE에서 짧은 확인 대화 상자가 표시됨).

jQuery를 사용하면 다음을 얻을 수 있습니다.

var initMailtoButton = function()
{
    var iframe = $('<iframe id="mailtoFrame" src="mailto:name@domain.com" width="1" height="1" border="0" frameborder="0"></iframe>');
    var button = $('#mailtoMessageSend');    
    if (button.length > 0) {            
        button.click(function(){
            // create the iframe
            $('body').append(iframe);
            //remove the iframe, we don't need it any more
            window.setTimeout(function(){
                iframe.remove();    
            }, 500);

        });
    }
}

이것은 오래된 질문이지만, 다음과 같은 기능을 생각해 내기 위해 여러 스택 오버플로를 결합했습니다.

//this.MailTo is an array of Email addresses
//this.MailSubject is a free text (unsafe) Subject text input
//this.MailBody is a free text (unsafe) Body input (textarea)
//SpawnMail will URL encode /n, ", and ', append an anchor element with the mailto, and click it to spawn the mail in the users default mail program
SpawnMail: function(){
    $("#MyMailTo").remove();
    var MailList="";
    for(i in this.MailTo)
        MailList+=this.MailTo[i]+";";
    var NewSubject=this.MailSubject.replace(/\n/g, "%0d%0a");
    NewSubject=NewSubject.replace(/"/g, "%22");
    NewSubject=NewSubject.replace(/'/g, "%27");
    var NewBody=this.MailBody.replace(/\n/g, "%0d%0a");
    NewBody=NewBody.replace(/"/g, "%22");
    NewBody=NewBody.replace(/'/g, "%27");
    $("#mainNavBar").after("<a id='MyMailTo' style='display:none;' href='mailto:"+MailList+"?subject="+NewSubject+"&body="+NewBody+"'>&nbsp;</a>");
    document.getElementById('MyMailTo').click();
}

이와 관련하여 멋진 점은 (그리고 어떻게 사용할 계획인가) 이것을 루프에 넣고 어레이에 있는 모든 사람들에게 개별 메시지를 분할하거나 모든 사람들이 함께 있도록 할 수 있다는 것입니다(이것이 현재 이 기능이 하는 일입니다).어쨌든 팁 고마워요 @Toskan

후속 조치 - 새로운 HTML5 표준은 "필요한 사용자 제스처" 없이 (또는 기타 팝업 관련 js)로 메일을 보내는 것을 허용하지 않습니다.멋진 기사: https://github.com/WICG/interventions/issues/12 따라서 개별 이메일을 대량으로 생성할 수는 없지만, 현재 디자인에서 많은 사람들에게 보내는 것은 잘 작동합니다.

언급URL : https://stackoverflow.com/questions/3868315/invoke-click-a-mailto-link-with-jquery-javascript

반응형