[英]How can I use smtpJS to add a self-generated PDF as a mail attachment?
I can have a mail sent on my website with smtpJS .我可以使用smtpJS在我的网站上发送邮件。 But now I want to attach a newly generated PDF that was created with jsPDF , I tried different things but it doesn't work for me.
但是现在我想附加一个新生成的 PDF ,它是用jsPDF创建的,我尝试了不同的方法,但它对我不起作用。 Does anyone recognize my problem?
有人认出我的问题吗?
I have added this link to WordPress in the functions.php file.我已在 functions.php 文件中将此链接添加到 WordPress。
wp_enqueue_script( 'pdfJS-js', get_stylesheet_directory_uri(). 'https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.min.js', array( 'jquery' ),'',true );
But in my browser console always comes the following error:但是在我的浏览器控制台中总是出现以下错误:
ReferenceError: Can't find variable: jsPDF
ReferenceError:找不到变量:jsPDF
( var doc = new jsPDF('p', 'mm', "A4"); )( var doc = new jsPDF('p', 'mm', "A4"); )
My Code:我的代码:
function sendMail() {
/* SmtpJS.com - v3.0.0 */
var Email = { send: function (a) { return new Promise(function (n, e) { a.nocache = Math.floor(1e6 * Math.random() + 1), a.Action = "Send"; var t = JSON.stringify(a); Email.ajaxPost("https://smtpjs.com/v3/smtpjs.aspx?", t, function (e) { n(e) }) }) }, ajaxPost: function (e, n, t) { var a = Email.createCORSRequest("POST", e); a.setRequestHeader("Content-type", "application/x-www-form-urlencoded"), a.onload = function () { var e = a.responseText; null != t && t(e) }, a.send(n) }, ajax: function (e, n) { var t = Email.createCORSRequest("GET", e); t.onload = function () { var e = t.responseText; null != n && n(e) }, t.send() }, createCORSRequest: function (e, n) { var t = new XMLHttpRequest; return "withCredentials" in t ? t.open(e, n, !0) : "undefined" != typeof XDomainRequest ? (t = new XDomainRequest).open(e, n) : t = null, t } };
var receiverMail = document.getElementById('email').value;
var doc = new jsPDF('p', 'mm', "A4");
var elementHandler = {
'#ignorePDF': function (element, renderer) {
return true;
}
};
var source = window.document.getElementById("A4")[0];
doc.fromHTML(
source,
297,
210,
{
'width': 210,'elementHandlers': elementHandler
});
var base64 = doc.output("datauristring");
Email.send({
SecureToken : "C973D7AD-F097-4B95-91F4-40ABC5567812",
To : receiverMail,
From : "name@example.com",
Subject : "Offerte von Name",
Body : "tst",
Attachments : [
{
name : "offerte.pdf",
data : base64
}]
}).then(
message => alert(message)
);
} }
First I verified that jsPDF
is loaded as a global variable if the script is on the page.首先,如果脚本在页面上,我验证了
jsPDF
作为全局变量加载。 It is.这是。 So your issue is that the script is not getting loaded to the page.
所以你的问题是脚本没有加载到页面上。 It's a PHP issue rather than a JS one.
这是 PHP 问题,而不是 JS 问题。
The problem is the URL in your wp_enqueue_script
.问题是 wp_enqueue_script 中的
wp_enqueue_script
。 You are loading a URL of a remote site so it is already a complete URL.您正在加载远程站点的 URL,因此它已经是完整的 URL。 You do not need to add
get_stylesheet_directory_uri()
to it.您不需要将
get_stylesheet_directory_uri()
添加到它。 The code that you have here will look for the script on a URL like https://mywordpresssite.com/wp-content/themes/my-theme/https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.min.js
which is obviously not the path that you want.您在此处的代码将在 URL 上查找脚本,例如
https://mywordpresssite.com/wp-content/themes/my-theme/https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.min.js
这显然不是你想要的路径。
wp_enqueue_script(
'pdfJS-js',
get_stylesheet_directory_uri() . 'https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.min.js',
array( 'jquery' ),
'',
true
);
You can just delete the get_stylesheet_directory_uri().
您可以删除
get_stylesheet_directory_uri().
and you should be fine.你应该没事。 I'm also adding in a version number as a best practice.
我还添加了一个版本号作为最佳实践。
wp_enqueue_script(
'pdfJS-js', // handle
'https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.min.js', // src
array( 'jquery' ), // dependecies
'1.3.2', // version
true // in footer
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.