0

I am trying to send a mail using PHP.
But it is just not working. I contacted my IT teacher, a few IT nerds I know, colleges at work, but they all just couldn't help me. I know there are a lot of questions on stackoverflow about this already, that's why I waited so long to post this. But i looked at all the questions about PHP's mail() function I could find, and none solved my problem. I am pretty desperate.

The thing is that it doesn't work using my code to create the mail, but it also doesn't work when just using the function with plain text. So that got me thinking the problem must be server-side. I contacted one.com (which is where my website is hoasted), but they couldn't help me. They said the mail() function should just work. They did give me this:
Sushant: - Mail Host: mailout.one.com
Sushant: - Port: 25
Sushant: - Authentication: None/False
Sushant: - Security: None/False
Sushant: - Username: email account of your domain e.g. info@mydomain.com
Sushant: - Password: password of your email account

I don't know what to do with this information. I did try to edit some of the things in the php.ini file (mail.add_x_header, mail.log, SMTP, smtp_port, sendmail_from, sendmail_path) using the ini_set() function. also that didn't solve my problem.

My full code:

(sorry for the dutch language)

$onderwerp = "E-mail adres controlleren";
$bericht ="
U bent succesvol geregistreerd bij theovisser.com.<br>
Nu kunt u alle verstuurde nieuwsbrieven lezen. Ook kunt u<br>
nieuwe nieuwsbrieven meteen zien, nog voordat ze bij op op de mat liggen!<br><br>

Om uw account te activeren moet u alleen nog wel de onderstaande code<br>
invoeren.<br>
E-mail code: " .$_SESSION['controle_code']. "<br><br>

Groeten,<br>
Samuël Visser
";
// Regels in berichten mogen nooit langer zijn als 70 caracters (PHP regel).
// Hiermee worden alle regels die langer als 70 caracters zijn ingekort.
    $bericht = wordwrap($bericht, 70);
// Verstuur de mail
if(!isset($_SESSION['mail_verstuurd']))
{
    if(mail($email,$onderwerp,$bericht)){
        echo 'Er is een mail verstuurd naar uw adres.<BR><BR>';
        $_SESSION['mail_verstuurd']='true';
    }else{
        echo 'Het versturen van de mail is mislukt.<BR><BR>';
    }

}

This doesn't do anything at all.
And the weirdest this is that this doesn't work too:

mail('samuvisser@gmail.com','test','dit is een test');

Can you guys solve my problem? I really don't know what I am doing wrong.

EDIT:
Using the suggestion from @ojovirtual to use the PHPMailer libary, I got the following error:

2014-11-04 13:27:48 Connection: opening to mailout.one.com:25, t=300, opt=array ( ) 2014-11-04 13:28:10 SMTP ERROR: Failed to connect to server: A connection attempt failed because the connected party did not properly respond after a period of time , or established connection failed because connected host has not replied. (10060) 2014-11-04 13:28:10 SMTP connect() failed. Message could not be sent.Mailer Error: SMTP connect() failed

The italic style text in the error was Dutch, this is translated to English. Might be a bit different as the original English translation.

This is the setup I used, following the data i got from one.com:

require($main .'PHPMailer-master/PHPMailerAutoload.php');

$mail = new PHPMailer;

$mail->SMTPDebug=3;
$mail->isSMTP();                                        // Set mailer to use SMTP
$mail->Host = 'mailout.one.com';                        // Specify main and backup SMTP servers
$mail->SMTPAuth = false;                                // Enable SMTP authentication
$mail->Username = 'noreply@theovisser.com';             // SMTP username
$mail->Password = 'won't show that here :)';            // SMTP password
$mail->SMTPSecure = 'tls';                              // Enable TLS encryption, `ssl` also accepted
$mail->Port = 25;                                       // TCP port to connect to

$mail->From = 'noreply@theovisser.com';
$mail->FromName = 'Samuël Visser';
$mail->addAddress('samuvisser@gmail.com', 'Sammie gast');       // 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';
}

I used the code recommended on the PHPMailer page.
Thanks for your help!

Samuël Visser
  • 452
  • 2
  • 5
  • 14
  • You'll probably need to log into the SMTP server before sending mail. I recommend using [PHPMailer](https://github.com/PHPMailer/PHPMailer). – ojovirtual Nov 04 '14 at 12:31
  • add `headers` in your mail – Prashant Vardhan Singh Nov 04 '14 at 12:31
  • headers are optional, right? I did not do anything to log-in to the SMTP server, everybody told me you don't need that. But i will try this libary, thanks – Samuël Visser Nov 04 '14 at 12:48
  • Using the libary SMTP connection keeps failing – Samuël Visser Nov 04 '14 at 13:15
  • Getting this error: 014-11-04 13:16:58 Connection: opening to mailout.one.com:25, t=300, opt=array ( ) 2014-11-04 13:17:20 SMTP ERROR: Failed to connect to server: Een verbindingspoging is mislukt omdat de verbonden party niet correct heeft geantwoord na een bepaalde tijd, of de gemaakte verbinding is mislukt omdat de verbonden host niet heeft geantwoord. (10060) 2014-11-04 13:17:20 SMTP connect() failed. Message could not be sent.Mailer Error: SMTP connect() failed. – Samuël Visser Nov 04 '14 at 13:18

1 Answers1

3

Various hosts have different limitations or requirements when an email is sent from a PHP-script, thus all PHPMailer library features can't always be used. In this answer I have information about One.com since the question stated One.com was used and I also use that host.

One.com possible mail settings

Host, username and password need to be provided as stated in the question.

The allowed authentication and encryption settings depend on which port that is used, these are all alternatives at One.com but the technical support have verified that all the settings can not currently be used in a PHP-script:

  • Port 25: authentication false or STARTTLS
  • Port 2525: authentication false or STARTTLS
  • Port 465: TLS eller SSL
  • Port 587: authentication false or STARTTLS

Within a PHP-script only the following can be used:

  • Port 25: authentication false

Example code verified at One.com

Below you can see code that is working at One.com servers if you replace the username and password with your own.

/*** Prepare email information ***/
require_once "Mail.php";
$from = "USERNAME@YOUR.DOMAIN";
$to = "SOMEONES.MAIL@gmail.com";
$subject = "Testing PHP SMTP mail";
$body = "Hi,\n\nit seems to be working don't you think?";
$headers = array ('From' => $from,   'To' => $to,   'Subject' => $subject,   'Reply-To' => $from);

/*** Send email ***/
$SendMail = new Mail();
$smtp = $SendMail->factory('smtp',array(
'host' => "mailout.one.com",
'port' => 25,
'auth' => false,
'username' => "YOUR_MAIL_USERNAME",
'password' => "YOUR_MAIL_PASSWORD"));
$mail = $smtp->send($to,$headers,$body);

/*** Check for errors ***/
$ErrorCheck = new PEAR;
if ($ErrorCheck->isError($mail)){
    echo("<p>".$mail->getMessage()."</p>");
}else{
    echo("<p>Message successfully sent!</p>");
}
?>
  • Thanks for your answer! To be honest, I have not checked if your solution is working because this website is no longer online :). But i fully trust you, will use your answer if i ever need it again (still with one.com for another website) – Samuël Visser Apr 18 '16 at 12:57