I’m trying to run MailerSend, an outbound email platform. I’ve loaded the necessary code onto my server and MailerSend works fine if I place the following code in a single PHP file and execute that file – let’s call it ‘SendAnEmail.php” :
require '/home/xxxxx/vendor/autoload.php';
use MailerSendMailerSend;
use MailerSendHelpersBuilderRecipient;
use MailerSendHelpersBuilderEmailParams;
use MailerSendHelpersBuilderAttachment;
$mailersend = new MailerSend(['api_key' => $apiKey]);
$recipients[] = new Recipient($email, '');
$emailParams = (new EmailParams())
->setFrom('fromemail.com')
->setFromName('My System')
->setRecipients($recipients)
->setSubject($subject)
->setHtml('This is the HTML content')
->setText('This is the text content')
->setReplyTo('[email protected]')
->setReplyToName('My System')
->setAttachments($attachments);
// send the email
$response = $mailersend->email->send($emailParams);
The problem is that I want this code to be executed inside a function, so I can call the function from multiple php scripts within the system, send the function the basic info (to address, subject line, body text, etc) and have the function send the email. I can’t get this to work – when I put the first five lines of the code above into the calling PHP script I get an error “Class ‘Recipient’ not found”. If I put the first five lines of code above into the function I get a syntax error “unexpected ‘use’ ” pointing to the first ‘use’ statement.
I am not an expert at PHP. I know how to expose regular variables from the calling script to the function using a global statement inside the function, but I don’t know how to expose the Receipient class, or the other classes I need, inside the function.
I hope this question makes sese – I’m not exactly sure I’m using the correct terminology. Any help is appreciated.
Regards.
Jonathan Spector is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1