0

I want to send an email using my custom PHP file in Magento root with an attachment using object manager. Please suggest to me what I do about it.

2
0

We use Mageplaza Magento 2 SMTP Module Click here to download and install.

$emailSendStaus = 0;
try {
    /* Email and Password */
    $senderEmail = $username = "test@gamil.com";
    $password = "PASSWORD";
    $senderName = "TESTING";

    /* Sender Detils */

    /* Subject Name */
    $subject = "Test Email";

    /* HTML Body */
     $htmlBody = "<h1>TEST EMAIL</h1>";

    // SMTP Configuration
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORES;
    $smtpHost = $objectManager->get('Magento\Framework\App\Config\ScopeConfigInterface')->getValue('smtp/configuration_option/host', $storeScope);
    $auth = strtolower($objectManager->get('Magento\Framework\App\Config\ScopeConfigInterface')->getValue('smtp/configuration_option/authentication', $storeScope));
    $smtpConf = [];
    $smtpConf['auth'] = $auth;
    $smtpConf['username'] = $senderEmail;
    $smtpConf['password'] = $password;
    $smtpConf['port'] = $objectManager->get('Magento\Framework\App\Config\ScopeConfigInterface')->getValue('smtp/configuration_option/port', $storeScope);
    $ssl = $objectManager->get('Magento\Framework\App\Config\ScopeConfigInterface')->getValue('smtp/configuration_option/protocol', $storeScope);
    if ($ssl != 'none') {
        $smtpConf['ssl'] = $ssl;
    }
    $smtpConfig = new \Zend_Mail_Transport_Smtp($smtpHost, $smtpConf);

    /* Email Start code */
    $mail = new \Zend_Mail();
    $mail->setFrom($senderEmail, $senderName);
    $mail->addTo("sendeemailto@gamil.com", "Test");
    $mail->setSubject($subject);
    $mail->setBodyHtml($htmlBody);
    $fileContent = file_get_contents("http://www.orimi.com/pdf-test.pdf");
    $fileName = "pdf-test.pdf";
    $mail->createAttachment(
        $fileContent,
        \Zend_Mime::TYPE_OCTETSTREAM,
        \Zend_Mime::DISPOSITION_ATTACHMENT,
        \Zend_Mime::ENCODING_BASE64,
        $fileName
    );
    $mail->send($smtpConfig);
    /* Email Over code */
    $emailSendStaus = 1;
    echo "EMAIL SEND";
} catch (\Magento\Framework\Exception\LocalizedException $e) {
    echo "ERROR";
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.