A web developer's blog. PHP, MySQL, CakePHP, Zend Framework, Wordpress, Code Igniter, Django, Python, CSS, Javascript, jQuery, Knockout.js, and other web development topics.

PHP: Various ways of sending email

This is quoted from the source:

Most newcomers to PHP were initially attracted to the language due to its low barrier of entry. After all, adding dynamic data to a web page by using PHP (the current date, for instance) can be done with as little as one line of code. Along these lines, one of PHP’s common selling points is in fact its mail() function. What’s not to like about a language that can send email via a single simple function? But, the reality is there’s a bit more involved when it comes to successfully sending email in this fashion, particularly when it comes to doing so on a platform-specific basis.

On the Linux- and Unix-based platforms, PHP’s mail() function will send email through Sendmail, the open source mail transfer software responsible for sending as much as 75% of the world’s email. Because Sendmail is typically installed by default on Linux- and Unix-based servers, chances are the mail() function is going to work out of the box.

On the Windows operating system, the process is a tad more involved. Because Windows doesn’t come with a mail transfer service installed, the mail() function must instead rely on a third-party email server, as defined by PHP’s SMTP configuration directive. When mail() is called, PHP will contact this SMTP server and attempt to transmit the email using it.

On the second page, the author discusses about sending mail using Zend Framework.

Sending Email Using the Zend Framework

The first two solutions provided in this tutorial progressively solve various obstacles you’ll encounter when sending email through a PHP script. However, many unresolved tasks remain. For instance, how can you send HTML-formatted email? Include attachments? If you’re looking for the total package when it comes to sending email, capable of doing everything you could possibly conceive in this regards, look no further than the Zend Framework.

The Zend Framework’s Zend_Mail component, when coupled with the Zend Framework’s approach to minimizing redundancy and maximizing portability, offers the ideal solution for configuring and transmitting email through PHP-driven webpages. In this concluding section, I’ll presume you’re familiar with the Zend Framework and therefore focus upon my particular approach to using the Zend_Mail component.

First, within the application’s config.ini file, I define various parameters used to configure the transmission solution:

; email

email.smtpserver = smtp.gmail.com
email.username   = wj@wjgilmore.com
email.password   = supersecret
email.support    = support@wjgilmore.com

Within the bootstrap.php file, I configure Zend_Mail’s transport mechanism, drawing upon the parameters defined in config.ini:

$mailConfigs = array('auth' => 'login',
                     'username' => $config->email->username,
                     'password' => $config->email->password,
                     'ssl' => 'tls');
 
$tr = new Zend_Mail_Transport_Smtp($config->email->smtpserver, $mailConfigs);
Zend_Mail::setDefaultTransport($tr);

Although this executes with each request, the overhead required to do so is minimal and will not affect performance. Finally, I invoke code similar to the following from within the appropriate controllers:

try {
   // Create a new mail object
   $mail = new Zend_Mail();
 
   $mail->setFrom($this->config->email->from_admin);
   $mail->addTo("jason@example.com");
   $mail->setSubject("Your account has been created");
 
   $email = "Thank you for registering!";
 
   $mail->setBodyText($email);
   $mail->send();
 
   $this->view->success = 1;
} catch (Exception $e) {
   $this->view->errors[] = "We were unable to send your
      confirmation email. Please contact
      {$this->config->email->support}.";
}

If you’d like to add an attachment, all you need to do is invoke the $mail object’s createAttachment() method:

$mail->createAttachment("brochure.pdf");

If you want to send HTML-formatted email, just use the setBodyHtml() method instead of setBodyText():

$mail->sendBodyHTML("Thank <b>you</b> for registering!");
This entry was posted in General and tagged , , , . Bookmark the permalink.

6 Responses to PHP: Various ways of sending email

  1. alyssa says:

    thanks for the information

  2. Eliezer says:

    what is the other ways of sending email the ways only can you send it to my email

  3. Eliezer says:

    thank you for the information too that i read but its not complete oh im filipino im a student i need it in my project

  4. Eliezer says:

    do you have free software download about your website so i could read it

  5. Eliezer says:

    AND WHAT INFORMATIONS DO YOU HAVE MAYBE SOME OF THEM THAT I NEED

  6. Eliezer says:

    WHAT DAY ARE YOU READING THE MESSAGES

Leave a Reply to Eliezer Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>