There are 2 ways to send email in ZF2, one is to send email through the system's mail program, and the other is to send email via SMTP protocol using the remote SMTP server.
The related classes are:
Use Zend\mail\message;//email message class uses zend\mail\transport\sendmail;//send class using the System mail application zend\mail\transport\smtp;// Send class using the remote SMTP server through the SMTP protocol use zend\mail\transport\smtpoptions;//to set the parameter class for the SMTP class
Use is also relatively simple, the following mail.qq.com SMTP server For example, a small number of code to achieve the ability to send email:
Use Zend\mail\message;use zend\mail\transport\sendmail;use zend\mail\transport\smtp;use Zend\Mail\Transport\ Smtpoptions;class TestController extends Abstractactioncontroller {//Send mail via SMTP public function sendsmtpmailaction ( {$msg = new Message (); $msg->setfrom ("[email protected]", "xxxxx")->setto ("[email protected]", "yyyyy") ->setsubject ("email Title")->setbody (' This was an Email! '); $smtpOpt = new Smtpoptions (Array (' name ' = ' smtp.qq.com ', ' host ' = ' smtp.qq.com ',//QQ's free mailbox service The ' Port ', ' connection_class ' = ' login ', ' connection_config ' = = Array ( ' username ' + ' xxxxx ',//QQ account, or the character mailbox name of the character mailbox name ' password ' = ' 1234567890 ',//Email login password ), )); $trans = new Smtp (); $trans->setoptions ($SMTPOPT); $trans->send ($msg); return new Jsonmodel (); }//Send mail via system mail program public Function sendmailaction () {$msg = new message (); $msg->setfrom ("[email protected]", "xxxxx")->setto ("[email protected]", "yyyyy") ->setsubject ("email Title")->setbody (' This was an Email! '); $trans = new Sendmail (); $trans->send ($msg); return new Jsonmodel (); }}
ZENDFRAMEWORK2 Learning Notes Send email