How to setup php on webhost without php.ini file - php

I'm close to finishing my first real website, this website uses php mail function to send mail from my website. My script works because I'm running mercury which routes the mail through gmail smtp. Now my question is if someone can tell me how to connect to this smtp server from php because my webhost doesn't allow me acces to the php.ini file.
Thanks in advance

This PHP class will do it for you. Most real webapps don't rely on PHP's mail(), they use this class instead (or swiftmailer, but his one's much easier).
PHPMailer
Sample code (from the linked page)
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.com'; // 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->From = 'from#example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen#example.com'); // Name is optional
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
$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';
}

Related

my phpMailer not working

hi my phpmailer is producing the following result when i execute my mailtest.php
Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
i have gone through many questions about phpmailer in this forum but none has benefit me.
my mailtest.php
<?php
require ("/phpmailer/PHPMailerAutoload.php");
require ("/phpmailer/class.phpmailer.php");
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'thethih#gmail.com'; // SMTP username
$mail->Password = 'password.'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('from#example.com', 'Mailer');
$mail->addAddress('thethih#gmail.com', 'Joe User'); // Add a recipient
$mail->addAddress('thethih#yahoo.com'); // Name is optional
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
$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';
}
?>
any help will be appreciated .. and it is in localhost.
On GitHub, PHPMailer says "Compatible with PHP 5.0 and later", but that may not be the case.
Try changing your version of PHP to 5.5 and see if that fixes the problem. You can change your version of PHP by going to control panel and clicking on PHP Configuration.

PHP Mailer doesn't send mail and not loading errors on my server

I am using PHP MAiler but the mail is not being sent,In fact the website on my server just keeps on loading and loading w/o showing any error.I even changed my gmail settings to allow less secure app but still the page doesnt load nor does it show any errors.
Code:
<?php
error_reporting(E_ALL);
require_once 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'mohd.gadiwala#techmatters.com'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->setFrom('mohd.gadiwala#techmatters.com', 'Mailer');
$mail->addAddress('mohd.gadiwala#techmatters.com', 'Joe User'); // Add a recipient
$mail->addAddress('ellen#example.com'); // Name is optional
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
// 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';
}
?>
I have Php mailer folder in the same folder as this so I guess loading the file isn't the problem

How to rectify SMTP connection failure error?

<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.comcast.net'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'mass.mari06#gmail.com'; // SMTP username
$mail->Password = '8489328117'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->Port = $SmtpPort;
$mail->From = 'from#example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('mass.mari06#gmail.com', 'Joe User'); // Add a recipient
$mail->addAddress('deeparavisankar93#gmail.com'); // Name is optional
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
$mail->WordWrap = 50; // Set word wrap to 50 characters
$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';
}
?>
****ERROR:**** Message could not be sent.Mailer Error: SMTP connect() failed.
So what can I do? Please help me.. this code I downloaded from GitHub.
SMTP is mail sending server.
You should use the one you are trying to send mail "from" is assigned. I assumed that you try to send from mass.mari06#gmail.com so configuration should look like following:
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'mass.mari06#gmail.com';

php mail is not working on my server

My code is this.
<html>
<head>
<title>Sending email using PHP</title>
</head>
<body>
<?php
$to = "myemail#gmail.com";
$subject = "This is subject";
$message = "This is simple text message.";
mail($to,$subject,$message);
echo "<br>Message sent successfully...";
?>
</body>
</html>
I know this is very simple but I am not able to solve it my try to help me
Is there a mailserver running on the machine that hosts this page? Most likely not because the code you show should work.
Now there are two options: Install a mailserver or use an additional PHP library to overcome this problem. I'm not gonna post a tutorial on how to install a mailserver so just Google it!
For the PHP library part, u could use PHPMailer. This class holds additional settings for sending the mail via specific servers. First download the needed files from GitHub (on the right side, click "Download zip") https://github.com/PHPMailer/PHPMailer . Unzip the package and copy the files to your working directory. The code could be like:
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.com'; // 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->From = 'from#example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen#example.com'); // Name is optional
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
$mail->WordWrap = 50; // Set word wrap to 50 characters
$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';
}
?>
The easiest way to send mail is to use a gmail account or something. The settings can be found literally everywhere. For example: http://phpmailer.worxware.com/?pg=examplebgmail

is mail() function of PHP encrypted already?

Please anybody tell me is mail() function of PHP is already encrypted or i have to make it encrypted my self, i want to make secure travel of email.
to use SSL or TLS with mail(), you will have to configure the "php.ini" (Require SSL on your server):
http://www.php.net/manual/en/mail.configuration.php
SMTP = YOUR_SERVER
smtp_port = SMTP_SECURE_PORT
sendmail_from = SEND_MAIL_FROM
sendmail_path = SEND_MAIL_APP
But if you change your server, you have to do it again, so I recommend instead of using mail() use SMTP, there are ready-made classes, see:
PHPMailer download: https://github.com/PHPMailer/PHPMailer
Example PHPMailer with TLS:
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->From = 'from#example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen#example.com'); // Name is optional
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
$mail->WordWrap = 50; // Set word wrap to 50 characters
$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';
}
The mail() function will either use the local mailer (Linux) or will connect to standard port 25 (Windows). There's absolutely no way to:
Make it use another port an encrypted channel
Provide authentication data
If security (or reliability) is a concern, avoid it at all cost.

Categories