提问人:nibelungen 提问时间:9/30/2023 最后编辑:ADysonnibelungen 更新时间:9/30/2023 访问量:42
用于 pdf 转换和电子邮件的 Wordpress 插件构建
Wordpress plugin building for pdf conversion and email
问:
我正在尝试编写一个简单的插件,该插件将 WP 表单提交转换为 PDF 文件,并使用 Google Translate API 自动将它们翻译成三种不同的语言。 我的开发环境在本地主机上。我也在服务器上尝试了代码,问题仍然存在。
我面临的问题是代码甚至不发送电子邮件。我一直在尝试调试它,但没有成功。如果我使用 WordPress 的wp_mail功能发送测试电子邮件,它似乎无法按预期工作。电子邮件发送功能是我插件的关键部分,我将不胜感激在识别和解决此问题方面的任何帮助。
这是我目前为止的代码:
<?php
/*
PLUGIN DESCRIPTION HERE
*/
// Hook into the WPForms submission process
add_action('wpforms_process_complete', 'translate_send_pdf_wpforms_process_complete', 10, 4);
function echo_log( $what )
{
echo '<pre>'.print_r( $what, true ).'</pre>';
}
function translate_send_pdf_wpforms_process_complete($fields, $entry, $form_data, $entry_id) {
$form_data = wpforms()->form->get($form_data['id']);
echo_log($form_data);
$user_submitted_data = wpforms()->entry->get($entry_id);
// send_test_email($entry_id);
$name_field = 'Single Line Text';
$person_name = isset($user_submitted_data[$name_field]) ? $user_submitted_data[$name_field] : 'Unknown';
error_log('person_name: ' . print_r($person_name, true));
echo $person_name;
echo 'hello';
// Generate PDF for each language
$languages = array('de', 'fr', 'es');
$pdf_files = array();
foreach ($languages as $language) {
$translated_content = translate_content($user_submitted_data, $language);
$pdf_content = generate_pdf($translated_content);
$pdf_files[$language] = $pdf_content;
}
send_pdf_email(
$pdf_files,
$languages,
$person_name
);
}
// Function to translate content
function translate_content($content, $target_language) {
$api_key = 'API KEY HERE';
$url = 'https://translation.googleapis.com/language/translate/v2?key=' . $api_key;
$data = array(
'q' => $content,
'source' => 'en',
'target' => $target_language,
);
// Initialize cURL session
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
// Execute the translation request
$response = curl_exec($handle);
// Close the cURL session
curl_close($handle);
// Parse the translation response
$translation_data = json_decode($response, true);
// Check if translation was successful
if (isset($translation_data['data']['translations'][0]['translatedText'])) {
// Extract and return the translated content
$translated_content = $translation_data['data']['translations'][0]['translatedText'];
return $translated_content;
} else {
// Return the original content if translation failed
return $content;
}
return $content;
}
// Function to generate PDF from content using TCPDF
function generate_pdf($content) {
// Include the TCPDF library if not already included
if (!class_exists('TCPDF')) {
require_once('/tcpdf-wrapper/lib/tcpdf/tcpdf.php');
}
// Create a new TCPDF instance
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// Set document information
$pdf->SetCreator('Name');
$pdf->SetAuthor('Name');
$pdf->SetTitle('Translated PDF');
$pdf->SetSubject('Translated PDF');
$pdf->SetKeywords('PDF, translation');
// Set default header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
// Set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
// Set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
// Add a page
$pdf->AddPage();
// Set font
$pdf->SetFont('times', '', 12);
// Add content to the PDF
$pdf->MultiCell(0, 10, $content, 0, 'L');
// Output the PDF to a variable
$pdf_content = $pdf->Output('', 'S');
return $pdf_content;
}
function send_pdf_email($pdf_files, $languages, $person_name) {
$to = '[email protected]';
$subject = 'New Form Submission';
$message = 'Please find the attached PDFs for the following languages: ' . implode(', ', $languages);
$attachments = array();
$current_date = date('Y-m-d');
// Loop through PDF files and attach them
foreach ($pdf_files as $language => $pdf_content) {
$pdf_file_name = "New_Form_{$person_name}_{$current_date}_{$language}.pdf";
$attachments[$pdf_file_name] = $pdf_content;
}
// Send the email
wp_mail($to, $subject, $message, '', $attachments);
}
function send_test_email($person_name) {
$to = '[email protected]';
$subject = 'Test Email';
$message = "This is a test email sent from your WordPress plugin. {$person_name}";
$result = wp_mail($to, $subject, $message);
}
?>
我尝试在我的插件代码中使用 WordPress 的wp_mail功能发送测试电子邮件。我希望测试电子邮件能够成功发送到指定的收件人电子邮件地址,并看到消息“测试电子邮件已成功发送”。但是,当我在WP Forms的“设置/通知”中设置“发送电子邮件”选项时,我只能通过WP Forms功能接收电子邮件。我确实安装了WP Mail SMTP插件。WP Mail SMTP插件的描述是: 将 wp_mail() 函数重新配置为使用 Gmail/Mailgun/SendGrid/SMTP 而不是默认的 mail(),并创建一个选项页面来管理设置。
另一方面,当我的插件处于活动状态时,它会阻止显示确认消息。
答: 暂无答案
评论