简体   繁体   中英

ZEND send with SMTP authentication

My application is currently sending emails but they are not SMTP authenticated so the emails are going in junk. Is there a way to add SMTP options without changing the main config?

 /* Mail Sender */
            $mail = new \Zend_Mail('utf-8');
            $mail->setFrom("XXX@XXX.com");
            $mail->addTo($adminMail);
            $mail->setSubject('My Subject');
            $mail->setBodyHtml('email content');
            $content = file_get_contents($filePath);
            $attachment = new \Zend_Mime_Part($content);
            $attachment->type = 'image/png'; // attachment's mime type
            $attachment->disposition = \Zend_Mime::DISPOSITION_ATTACHMENT;
            $attachment->encoding = \Zend_Mime::ENCODING_BASE64;
            $attachment->filename = 'image_' . $customerId . '.png';
            $mail->addAttachment($attachment);
            $mail->send();

Try to modify your code like this:

$mail = new \Zend_Mail('utf-8');
$mail->setFrom("XXX@XXX.com");
$mail->addTo($adminMail);
$mail->setSubject('My Subject');
$mail->setBodyHtml('email content');
$content = file_get_contents($filePath);
$attachment = new \Zend_Mime_Part($content);
$attachment->type = 'image/png'; // attachment's mime type
$attachment->disposition = \Zend_Mime::DISPOSITION_ATTACHMENT;
$attachment->encoding = \Zend_Mime::ENCODING_BASE64;
$attachment->filename = 'image_' . $customerId . '.png';
$mail->addAttachment($attachment);

// create mail transport
$transport = new \Zend_Mail_Transport_Smtp(
    'smtp.yourserver.com',
    [
        'auth' => 'login',
        'port' => 'yoursmtpport',
        'username' => 'yourusername',
        'password' => 'yourpassword',
    ]
);

$mail->send($transport);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM