Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mail() with php

Tags:

php

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

like image 921
nikky Avatar asked Dec 10 '22 19:12

nikky


2 Answers

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

like image 171
Pascal MARTIN Avatar answered Dec 13 '22 22:12

Pascal MARTIN


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      = '[email protected]';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: [email protected]' . "\r\n" .
    'Reply-To: [email protected]' . "\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 = "[email protected]";
$mail->FromName = "Your Name";
$mail->AddAddress("[email protected]"); // 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).

like image 23
Sam Avatar answered Dec 13 '22 21:12

Sam