coder coder - 1 year ago 38
PHP Question

how send mail without changing hosting files (or without entering credential in hosting files static)

I searched a lot how to send email but what i found was for specific criteria.2 are given follow;

like how to mail form localhost (by provoding detail in sendmail.ini and other custom code)
how to sen mail form godaddy hosting (use PHP Form Mailer)

If i follow this them it becomes specific by changing hosting files.

while there are many scripts(developed sites for sale) which allow users to install those scripts on localhost or any hosting plan and enter smtp credential and those script able to send mail(contact-us form) without changing hosting files.

but what i know is to give credential in 'sendmail.ini' file (id, password, smtp server) to send mail or use PHP Form Mailer for godaddy hosting.

but actually i'm developing website and i will put it on 'codycanon' or other platform to sale this, so in this case user can host it on any hosting he likes. so can you guide me how i can send mail without changing hosting files?

Answer Source

use PHP MAILER script include it in your project and use it like this

require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

//$mail->SMTPDebug = 3;                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = ';';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = '';                 // SMTP username
$mail->Password = 'secret';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->setFrom('', 'Mailer');
$mail->addAddress('', 'Joe User');     // Add a recipient
$mail->addAddress('');               // Name is optional
$mail->addReplyTo('', 'Information');

$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';

All your user has to do is use the SMTP details provided by his hosting provider and enter it in the relevant variables, you can go a notch higher and make a interactive html page where your user enters details and your script automates the process of editing the config.

SwiftMailer is also a potential alternative.