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
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 ;-)
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:
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With