4

i wanna write a script sending e-mail to my client automatically using php

How do i send it automatically, for example, if they enter their email. and click submit

i wanna send this e-mail automatically

And, second do i need smtp server on my host? can i just this in any free hosting?

Thanks you guys and im so sorry for my language

Nikky

nikky
  • 1,835
  • 3
  • 18
  • 19
  • 1
    Be careful. As soon as some enterprising spammer finds that you have a mailer script on your website, they'll think nothing of trying to hijack it to use it for spam (and then all their spam will come from your servers!) – Jeremy Powell Oct 17 '09 at 19:10

3 Answers3

6

I probably wouldn't go with using the mail function directly : too many things you have to care about...

Instead, I would recommend using some mail-related library, which will deal with a lot of things for you.

One of those (which seems to have some success nowadays -- it's being integrated in the Symfony framework, for instance) is Swift Mailer.

Of course, it might be a bit overkill for just a simple mail... But investing some time in learning how to use such a library is always worth it ;-)

d-_-b
  • 21,536
  • 40
  • 150
  • 256
Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • 1
    Composing and delivering valid emails is harder than it looks at first glance. Granted, maybe it's not rocket science. But in almost all cases it's something of the category "already solved, no need to fail at the first five/six/seven attempts reinventing the wheel". And Swiftmailer really is a nifty library. – VolkerK Oct 17 '09 at 19:19
  • Not only is it difficult to format and compose the bits of the email like charset correctly, it's really hard to prevent "mail header injection" with the mail function alone, so I highly recommend swiftmailer as a bare minimum (if you want to make it less verbose, for common cases, just create a custom function wrapper for simple cases. – Kzqai Jul 28 '11 at 15:34
6

PHP doesn't implement SMTP protocol (RFC 5321) or IMF (RFC 5322), or MIME like Python for example. Instead - all PHP has is a simple C wrapper around sendmail MTA.

However - with all it's drawbacks - one can still create mime messages(multipart/alternative, multipart/mixed etc) and send html and text messages and also attach files using default PHP's mail() function. The problem is - it's not straightforward. You'll end up handcrafting entire message by using "headers" mail() argument, while setting "message" argument to ''. Also - sending emails in a loop through PHP's mail() will be a performance waste since mail() opens new smtp connection for each new email.

/**sending email via PHP's Mail() example:**/
$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);

Because of these limitations most folks end up using third party libraries like:

  1. PHPmailer (download)
  2. Swiftmailer
  3. Zend_Mail

Using these libraries one can construct text or html messages with ease. Adding files also becomes an easy thing to do.

/*Sending email using PHPmailer example:*/
require("class.phpmailer.php");
$mail = new PHPMailer();

$mail->From = "from@example.com";
$mail->FromName = "Your Name";
$mail->AddAddress("myfriend@example.net"); // This is the adress to witch the email has to be send. 
$mail->Subject = "An HTML Message";
$mail->IsHTML(true); // This tell's the PhPMailer that the messages uses HTML.
$mail->Body = "Hello, <b>my friend</b>! \n\n This message uses HTML !";
$mail->AltBody = "Hello, my friend! \n\n This message uses HTML, but your email client did not support it !";

if(!$mail->Send()) // Now we send the email and check if it was send or not.
{
   echo 'Message was not sent.';
   echo 'Mailer error: ' . $mail->ErrorInfo;
}
else
{
   echo 'Message has been sent.';
}

ALSO: Q: do i need smtp server on my host? can i just this in any free hosting? A: any shared hosting has SMTP server nowadays (sendmail/postfix).

Sam
  • 61
  • 1
  • 4
2

Using built-in mail() function is not a good idea in most cases. So yes, either use the SwiftMailer or:

http://phpmailer.worxware.com/ - The PhpMailer which is in many senses is a similar implementation.

SPDenver
  • 442
  • 5
  • 9