Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I am using a class. It's much more easier. You can try this too. I took this class from somewhere and I improved it to match with my requirement.</p> <pre><code>&lt;?php class Mail { private $arrTo = array(); private $arrCC = array(); private $arrBCC = array(); private $strContent = ""; private $strSubject = ""; private $arrHeader = array(); private $arrAttachments = array(); private $numPriority = 3; private $strCharSet = "iso-8859-1"; private $strContentType = "text/plain"; private $strEncoding = "8bit"; /* * Mail::addUser() * * Adds a user to send the email to * * @param String $strAddress : Holds an email address * * @access public * * @return Integer */ public function addUser($strAddress){ if($this-&gt;emailCheck($strAddress)){ $this-&gt;arrTo[] = $strAddress; return 1; }else{ return 0; } } /* * Mail::removeUser() * * Removes an address from the array * * @param String $strAddress : Holds an email address * * @access public * * @return void */ public function removeUser($strAddress){ //create array or arrays to check $arrVars = array('arrTo', 'arrCC', 'arrBCC'); foreach($arrVars as $resVars){ //sees if the item exists in the array $numKey = array_search($strAddress, $this-&gt;{$resVars}); if(isset($numKey)){ //remove the email address $this-&gt;{$resVars}[$numKey] = ""; } unset($numKey); } } /* * Mail::addCC() * * Adds a CC user to send to * * @param String $strAddress : Holds an email address * * @access public * * @return Integer */ public function addCC($strAddress){ if($this-&gt;emailCheck($strAddress)){ $this-&gt;arrCC[] = $strAddress; return 1; }else{ return 0; } } /* * Mail::addBCC() * * Adds a BCC user to send to * * @param String $strAddress : Holds an email address * * @access public * * @return Integer */ public function addBCC($strAddress){ if($this-&gt;emailCheck($strAddress)){ $this-&gt;arrBCC[] = $strAddress; return 1; }else{ return 0; } } /* * Mail::addReplyTo() * * Adds a reply-to email address * * @param String $strEmail : Holds the reply-to email address * * @access public * * @return void */ public function addReplyTo($strEmail){ if($this-&gt;emailCheck($strEmail)){ $this-&gt;addHeader("Reply-to: $strEmail"); } } /* * Mail::setContent() * * Adds the content of the email * * @param String $strText : Holds the email content * * @access public * * @return void */ public function setContent($strText){ $this-&gt;strContent = $strText; } /* * Mail::setSubject() * * Adds the email subject * * @param String $strText : Holds the email subject * * @access public * * @return void */ public function setSubject($strText){ $this-&gt;strSubject = $strText; } /* * Mail::setFrom() * * Adds the sender info to the headers * * @param String $strText : Holds the details of who the email is from * * @access public * * @return void */ public function setFrom($strText){ $this-&gt;addHeader("From: $strText"); } /* * Mail::setHTML() * * Sets the email to allow html * * @access public * * @return void */ public function setHTML(){ $this-&gt;strContentType = "text/html"; } /* * Mail::setPlain() * * Sets the email to plain text * * @access public * * @return void */ public function setPlain(){ $this-&gt;strContentType = "text/plain"; } /* * Mail::setPriority() * * Sets the email priority from 1 – Urgent to 5 – not so * * @param Integer $numPriority : The email priority * * @access public * * @return void */ public function setPriority($numPriority){ $this-&gt;numPriority = $numPriority; } /* * Mail::addReadConfirmationEmail() * * Ensures that a confirmation email is sent when read * * @param String $strEmail : Email address to send read confirmation to * * @access public * * @return void */ public function addReadConfirmationEmail($strEmail){ if($this-&gt;emailCheck($strEmail)){ $this-&gt;addHeader("Disposition-Notification-To: &lt;$strEmail&gt;"); } } /* * Mail::addHeader() * * Adds the header details * * @param String $strText : Holds the header details * * @access public * * @return void */ public function addHeader($strText){ $this-&gt;arrHeader[] = $strText; } /* * Mail::addAttachment() * * Adds an attachment to be sent with the email * * @param String $strFile : The file that you want to attach * @param String $strName : The name of the attachment you want displayed * @param String $strType : The MIME type of the file * * @access public * * @return void */ public function addAttachment($strFile, $strName, $strType){ //check to make sure the file exists if(file_exists($strFile)){ $this-&gt;arrAttachments[] = array('path' =&gt; $strFile, 'name' =&gt; $strName, 'type' =&gt; $strType); } } /* * Mail::send() * * Sends the email to all the required people * * @access public * * @return void */ public function send(){ //get all the emails in a string to use $strTo = implode(", ", $this-&gt;arrTo); //add any CC addresses if needed if($this-&gt;arrCC){ $this-&gt;addHeader("Cc: ".implode(", ", $this-&gt;arrCC)); } //add any BCC addresses if needed if($this-&gt;arrBCC){ $this-&gt;addHeader("Bcc: ".implode(", ", $this-&gt;arrBCC)); } //append any attachments to the end of the content if(count($this-&gt;arrAttachments)){ $this-&gt;appendAttachments(); } //add the additional headers $this-&gt;addAdditionalHeaders(); //implode the headers as they need to be in a single string $strHeader = implode("\r\n", $this-&gt;arrHeader); // Set this if you have your own domain //ini_set('sendmail_from', 'yourdomain.com'); //send the email mail($strTo, $this-&gt;strSubject, $this-&gt;strContent, $strHeader); } /* * Mail::appendAttachments() * * Appends any attachments to the end of the email content * * @access public * * @return void */ public function appendAttachments(){ //create a unique boundary value $strBoundary = "H2O-".time(); //add the boundary to the headers $this-&gt;addHeader("Content-Type: multipart/alternitive; boundary=$strBoundary"); //if there is content to the email already we need to add the boundary and content type if($this-&gt;strContent){ $strContent = "–$strBoundary\r\n"; $strContent .= "Content-Transfer-Encoding: {$this-&gt;strEncoding}\r\n"; $strContent .= "Content-type: {$this-&gt;strContentType}; charset={$this-&gt;strCharSet}\r\n\r\n"; $this-&gt;strContent = $strContent.$this-&gt;strContent."\r\n\r\n\r\n"; } //loop through all the attachments foreach($this-&gt;arrAttachments as $arrFile){ //read the content of the file into a string and base64 encode and split into the correct chunk size $strData = chunk_split(base64_encode(implode("", file($arrFile['path'])))); //add the attachments $strAttachment = "–$strBoundary\r\n"; $strAttachment .= "Content-Transfer-Encoding: base64\r\n"; $strAttachment .= "Content-Type: {$arrFile['type']}; name={$arrFile['name']}\r\n"; $strAttachment .= "Content-Disposition: attachment; filename={$arrFile['name']}\r\n\r\n"; $strAttachment .= "$strData\r\n"; //add the attachment to the email content $this-&gt;strContent .= $strAttachment; unset($strAttachment); } //add the closing boundary $this-&gt;strContent .= "–$strBoundary–"; } /* * Mail::addAdditionalHeaders() * * Adds additional headers for content type and priority etc * * @access public * * @return void */ public function addAdditionalHeaders(){ $this-&gt;addHeader("MIME-Version: 1.0"); //add the content type and charset if there are no attachments if(!count($this-&gt;arrAttachments)){ $this-&gt;addHeader("Content-type: {$this-&gt;strContentType}; charset={$this-&gt;strCharSet}"); } //add the priority $this-&gt;addHeader("X-Priority: $this-&gt;numPriority"); } /* * Mail::emailCheck() * * Checks to ensure that the email address is valid * * @param String $strAddress : Holds an email address * * @access public * * @return Integer */ public function emailCheck($strAddress){ if(ereg("^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$", $strAddress)){ return 1; }else{ return 0; } } } ?&gt; </code></pre> <p>Implementation:</p> <pre><code>$email = new Mail(); $email-&gt;addUser($toAddress); $email-&gt;setFrom("Your Company Name&lt;" . $yourCompanyEMail. "&gt;"); $email-&gt;setHTML(); $email-&gt;setSubject("Subject goes here"); $email-&gt;setContent("Mail body goes here"); $email-&gt;send(); </code></pre> <p>Cheers !</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload