尝试在没有composer的情况下安装SwiftMailer时出现问题.尝试通过php发送带有pdf的smtp电子邮件



我在尝试安装另一个没有composer和其他东西的php库时遇到了一些困难。我做了什么:

  1. 在可执行脚本(index.php(所在的同一文件夹中,我创建了文件夹fpdf
  2. fpdf中创建了文件夹lib,并从中解压https://github.com/swiftmailer/swiftmailer/tree/master/lib
  3. lib中创建了文件夹DoctrineEmailValidator,并从https://github.com/doctrine/lexer和https://github.com/egulias/EmailValidator分别
  4. swift_required.php文件的末尾,我添加了这个答案的代码https://stackoverflow.com/a/50105900/15749307.

获取错误:

Parse error: syntax error, unexpected '?' in /home/virtwww/w_imaimachi_0f858928/http/fpdf/lib/EmailValidator/Validation/MessageIDValidation.php on line 47
line 47:     public function getError() : ?InvalidEmail;

我想我有PHP 7。

如果这场噩梦能够结束,请告诉我如何结束。如果有其他用于发送电子邮件的库(我需要将pdf作为字符串而不是文件发送(,可以在没有composer或其他框架的情况下轻松安装,或者如果有其他非第三方解决方案,请告诉我。

这不是确切问题的答案,但这是通过smtp发送pdf的代码,对我有效

  1. 下载"mailer";(只有几个php文件(并提取到服务器上的某个文件夹https://github.com/PHPMailer/PHPMailer.
  2. 在可执行的php文件(index.php或其他文件(中添加代码:

代码(如果由于某种原因,我将其粘贴在语法突出显示中断的列表之后(:

use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerException;
require 'path/to/src/Exception.php';
require 'path/tp/src/PHPMailer.php';
require 'path/to/src/SMTP.php';

将pdf作为二进制字符串发送的代码(不是base64!(:

function mail_attachment($pdf) 
{
$mail = new PHPMailer();
// Settings
$mail->IsSMTP();
$mail->CharSet = 'UTF-8';
$mail->Host       = "smtp.server.ua"; // SMTP server example
$mail->SMTPDebug  = 0;                     // enables SMTP debug information (for testing)
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->Port       = 25;                    // set the SMTP port for the GMAIL server
$mail->Username   = "login"; // SMTP account username example
$mail->Password   = "passwrod";        // SMTP account password example
$mail->setFrom('aga@ugu.com', 'Mailer');
//$mail->addAddress('joe@example.net', 'Joe User');     //Add a recipient
$mail->addAddress('okumaima@outlook.com');        
// Content
$mail->isHTML(true);                                  // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->AddStringAttachment($pdf, 'file.pdf'); // not base64 !
echo $mail->send();
}

应该有效,希望它能帮助到别人。

最新更新