PHP: Various ways of sending email

Posted on: Dec 17, 2008 by wenbert

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!");

Subscribe to comments Comment | Trackback |
Post Tags: , , ,

Browse Timeline


Comments ( 6 )

thanks for the information

alyssa added these pithy words on Jan 13 09 at 12:36 PM

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

Eliezer added these pithy words on Jan 14 09 at 7:04 PM

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

Eliezer added these pithy words on Jan 14 09 at 7:08 PM

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

Eliezer added these pithy words on Jan 14 09 at 7:10 PM

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

Eliezer added these pithy words on Jan 14 09 at 7:17 PM

WHAT DAY ARE YOU READING THE MESSAGES

Eliezer added these pithy words on Jan 14 09 at 7:34 PM

Add a Comment


XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">


© Copyright 2007 eKini Web Developer Blog . Thanks for visiting!