program tip

자바 스크립트로 이메일 보내기

radiobox 2020. 8. 15. 09:00
반응형

자바 스크립트로 이메일 보내기


설명하기가 좀 헷갈 리니 참고 해주세요 ...

사용자가 내 웹 사이트를 통해 템플릿 이메일을 보낼 수있는 시스템을 설정하고 싶습니다. 단, 실제로 내 서버를 사용하여 전송되지는 않습니다. 대신 이메일이 준비된 상태로 자체 로컬 메일 클라이언트를 엽니 다. 응용 프로그램은 미리 정의 된 변수로 전자 메일 본문을 작성하여 사용자가 직접 입력해야하는 것을 저장합니다. 그런 다음 목적에 정확히 맞지 않는 경우 원하는대로 메시지를 편집 할 수 있습니다.

사용자의 로컬 메일 클라이언트를 통해 전송하려는 여러 가지 이유가 있으므로 서버가 이메일을 보내도록하는 것은 옵션이 아닙니다. 100 % 클라이언트 측이어야합니다.

이미 대부분 작동하는 솔루션이 실행 중이며 이에 대한 자세한 내용을 답변으로 게시 할 것입니다. 더 좋은 방법이 있는지 궁금합니다.


지금 내가하는 방식은 기본적으로 다음과 같습니다.

HTML :

<textarea id="myText">
    Lorem ipsum...
</textarea>
<button onclick="sendMail(); return false">Send</button>

자바 스크립트 :

function sendMail() {
    var link = "mailto:me@example.com"
             + "?cc=myCCaddress@example.com"
             + "&subject=" + escape("This is my subject")
             + "&body=" + escape(document.getElementById('myText').value)
    ;

    window.location.href = link;
}

이것은 놀랍게도 잘 작동합니다. 유일한 문제는 본문이 특히 길면 (2000 자 이상) 새 이메일을 열지 만 그 안에 정보가 없다는 것입니다. URL의 최대 길이를 초과하는 것과 관련이 있다고 생각합니다.


다음은 jQuery와 "요소"를 사용하여 클릭하는 방법입니다.

$('#element').click(function(){
    $(location).attr('href', 'mailto:?subject='
                             + encodeURIComponent("This is my subject")
                             + "&body=" 
                             + encodeURIComponent("This is my body")
    );
});

그런 다음 입력 필드 (예 :를 사용하여 $('#input1').val()또는 서버 측 스크립트를 사용하여 $.get('...').


자바 스크립트가 필요하지 않습니다. href가 다음과 같이 코딩되어야합니다.

<a href="mailto:me@me.com">email me here!</a>

텍스트 상자에 라이브 유효성 검사를 가지고 무엇에 대해, 그리고 (또는 최대 임계 값이 무엇이든) 그 다음 ',이 이메일은 너무 오래 브라우저에 완료 될 것입니다하시기 바랍니다 표시 2000 넘어 일단 <span class="launchEmailClientLink">launch what you have in your email client</span>'

내가 가지고있을

.launchEmailClientLink {
cursor: pointer;
color: #00F;
}

onDomReady에 jQuery

$('.launchEmailClientLink').bind('click',sendMail);

이 무료 서비스를 사용할 수 있습니다 : https://www.smtpjs.com

  1. 스크립트 포함 :

<script src="https://smtpjs.com/v2/smtp.js"> </script>

  1. 다음을 사용하여 이메일을 보냅니다.

    Email.send("from@you.com", "to@them.com", "This is a subject", "this is the body", "smtp.yourisp.com", "username", "password");


이것이 이메일을 보내기 위해 사용자의 클라이언트를 열게된다면, 거기에서도 작성하게하는 것은 어떨까요? 당신은 그들이 보내는 것을 추적 할 수있는 능력을 잃게됩니다. 그러나 그것이 중요하지 않다면 주소와 제목을 수집하고 사용자가 본문을 채울 수 있도록 클라이언트를 팝업하십시오.


The problem with the very idea is that the user has to have an email client, which is not the case if he rely on webmails, which is the case for many users. (at least there was no turn-around to redirect to this webmail when I investigated the issue a dozen years ago).

That's why the normal solution is to rely on php mail() for sending emails (server-side, then).

But if nowadays "email client" is always set, automatically, potentially to a webmail client, I'll be happy to know.


Send request to mandrillapp.com:

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
        console.log(xhttp.responseText);
    }
}
xhttp.open('GET', 'https://mandrillapp.com/api/1.0/messages/send.json?message[from_email]=mail@7995.by&message[to][0][email]=zdanevich.vitaly@yaa.ru&message[subject]=Заявка%20с%207995.by&message[html]=xxxxxx&key=oxddROOvCpKCp6InvVDqiGw', true);
xhttp.send();

참고URL : https://stackoverflow.com/questions/271171/sending-emails-with-javascript

반응형