I have a question for all you out there.
I've noticed that my php mail isn't sent anymore. So I looked into it to determine what the problem is but I can't seem to find it.
This is the case:
In powershell 3 (server 2012)I use the following command:
send-MailMessage -SmtpServer smtp.versatel.nl
and after giving the to e-mail address, a subject and a from address it sends the mail without any problem.
The code in the PHP file is:
INI_SET("SMTP", "smtp.versatel.nl");
$success = mail($to, $subject, $body, $header);
preg_match("/\d+/", $error["message"], $error);
if(!$success){$this->sFoutmelding ='Email is niet verstuurd.'.print_r($error);}
In the php.ini I disabled the default ini settings:
mail function]
; For Win32 only.
; SMTP = Smtp.versatel.nl
; smtp_port = 25
But still I get an error that the mail isn't sent:
Array ( )
Email is niet verstuurd.1
Why doesn't php send the mail when powershell does this perfectly?
Any ideas are welcome, I have searched on and off the whole week over the internet, but i can't find an answer.
*edit
I tried the var_dump and it gives, as expected, only a 'FALSE'.
Also the change in 'localhost' didn't do the trick. Still searching...
Its working, but still no explonation why :-) So if someone knows why, I like to know it too :-)
It still uses the same smtp server as without the phpmailer class. So the only thing I can think off is the fact that PHPmailer creates a different, more widly excepted, mail header than the default one of PHP.
I added the following code and class PHP mailer:
$success = $this->mail_attachment($to, $subject, $body);
protected function mail_attachment($mailto, $subject, $message) {
require_once("../PHPMailer/class.phpmailer.php");
//$file = $path.$filename;
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.versatel.nl"; // SMTP server
$mail->From = "noreply#provider.foo";
$mail->FromName = "Sendername";
$mail->AddAddress($mailto);
$mail->Subject = $subject;
$mail->Body = $message;
$mail->WordWrap = 75;
//$mail->AddAttachment($file);
if(!$mail->Send()) {
echo 'Bevestigings e-mail is niet verzonden.';
echo 'Mailer error is: ' . $mail->ErrorInfo;
}
}
Related
How to connect a php testing server like https://mailtrap.io/ .
If i have defined variables like
$host = 'mail.sample.com';
$port=25;
$user="username";
$pass="secret";
maybe also $Auth = "PLAIN, LOGIN and CRAM-MD5" $TLS="Optional (STARTTLS on all ports)";
How to use these variables, nowhere i can find samples.
I want to use simple php mail function.
mail($to, $subject, $message, $headers);
Thank you
Mailtrap is a fake SMTP server. However, the built-in PHP mail() function is simple and lacks SMTP authentication support ā which is required by mailtrap.
So, move on an and use a PHP mailer package like PHPMailer, Swift Mailer, PearMail, etc as shown here https://blog.mailtrap.io/php-email-sending/
E.g. with PHPMailer
use PHPMailer\PHPMailer\PHPMailer;
require_once './vendor/autoload.php';
$mail = new PHPMailer();
// configure an SMTP
$mail->isSMTP();
$mail->Host = 'smtp.mailtrap.io';
$mail->SMTPAuth = true;
$mail->Username = '1a2b3c4d5e6f7g';
$mail->Password = '1a2b3c4d5e6f7gā;
$mail->SMTPSecure = 'tls';
$mail->Port = 2525;
$mail->setFrom('confirmation#hotel.com', 'Your Hotel');
$mail->addAddress('me#gmail.com', 'Me');
$mail->Subject = 'Thanks for choosing Our Hotel!';
$mail->isHTML(TRUE);
$mail->Body = '<html>Hi there, we are happy to <br>confirm your booking.</br> Please check the document in the attachment.</html>';
$mail->AltBody = 'Hi there, we are happy to confirm your booking. Please check the document in the attachment.';
if(!$mail->send()){
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
There are other, haphazard options but I bet they are not working in your case. One of them is changing mail directives in php.ini and sendmail.ini (if you have access to it and feel adventurous).
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
<?php
$to = "karthik.hansi#infysky.com";
$subject = "First php test script";
$message = "Hi This message is for leave application ";
$headers = "From:karthik.hansi#infysky.com";
if(mail($to, $subject, $message, $headers))
{
echo "failed";
}else{
echo "sent";
}
?>
This is My code I have tried this sending mail from PHP not able to send mail anything I have to configure in xampp.
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->IsHTML(true);
$mail->Username = "yourname#gmail.com";
$mail->Password = "your_pass";
$mail->SetFrom("your_email");
$mail->Subject = "your_title";
$mail->Body = "your_body";
$mail->AddAddress("receiver_email");
if (!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";
}
You have to include phpmailer library. You can download it from here phpmailer
After that you go to your gmail account that you want to use as sender and you need to set 2-step verification ---> off and Allow less secure apps ---> ON.
This guide may help you set up gmail for send emails with php
I am using this script and works fine just make sure to set up all the changes. Maybe you have to add some changes in your xampp ( i did not but i don't know your php.ini file so keep that in mind).
You can use libmail: http://lwest.free.fr/doc/php/lib/index.php3?page=mail&lang=en
include "libmail.php";
$m = new Mail(); // create the mail
$m->From( "leo#isp.com" );
$m->To( "destination#somewhere.fr" );
$m->Subject( "the subject of the mail" );
$m->Body( "Hello\nThis is a test of the Mail component" );
$m->Cc( "someone#somewhere.fr");
$m->Priority(4);
// attach a file of type image/gif to be displayed in the message if possible
$m->Attach( "/home/leo/toto.gif", "image/gif", "inline" );
$m->Send(); // send the mail
echo "Mail was sent:"
echo $m->Get(); // show the mail source
I'm trying to send email to my gmail account from my website using mail() function. This is working fine but the problem is, when I receive the mail in my gmail account inbox ( not in spam ), there is a '?' mark instead of a regular blank profile picture. When I hover over the '?' mark, a message pops up saying,"Gmail couldn't verify that mywebsite.com actually sent this message (and not a spammer)". I have added two pictures of this issue. I contacted to my hosting provider and they told me that it was a script related issue. They analyzed the file which I got by clicking on "Show original" in my gmail inbox. They said that, the problem occurred because the 'from' email address that gmail got was "cpanel-username#server-hostname.com" instead of "myemail#website.com". I have to replace "cpanel-username#server-hostname.com" with "myemail#website.com". I tried a lot but was unable to change the "from" header. My code is something like this
$Email = "myemail#gmail.com";
$message = "Hello!!";
$headers = "From: Mywebsite <noreply#mywebsite.com>";
$sentmail = mail("$Email",'Something Important',"$message",$headers);
Note that, "noreply#mywebsite.com" is a real email address which I created in my cpanel. I also tried phpmailer to solve this issue but I did not succeed. My phpmailer code is like this
require 'mailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isMail(); // I also tried $mail->isSMTP(), but it does not work. Just keeps loading the page but never completes.
$mail->Host = 'localhost';
$mail->SMTPAuth = true;
$mail->Username = 'noreply#mywebsite.com';
$mail->Password = '****************';
$mail->SMTPSecure = 'tls';
$mail->Port = 365;
$mail->setFrom('noreply#mywebsite.com', 'Mywebsite');
$mail->addAddress('myemail#gmail.com');
$mail->isHTML(true);
$mail->Subject = 'Something Important';
$mail->Body = 'Hello!!';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
But this also does not solve my problem. The from header is still "cpanel-username#server-hostname.com" when the mail is received in gmail. Please someone help me with this issue.
you can add the -f user#domain.tld to a mail.force_extra_parameters line in your php.ini.
See the solution provided here. It helped me.
https://forums.cpanel.net/threads/email-sent-as-username-hostname.601655/
i use phpmailer for sending emails. in one script, the code works fine, in other exaple, result is true, but no mail is delivered. error log is empty, what would you recommend to check?
thanks
include_once '/var/www/xxxxxx.cz/web/php/phpmailer.php';
$to = 'dubcznic#gmail.com';
$to_name = '';
$from = 'robot#xxxxxx.cz';
$from_name = 'Robot';
$mail = new phpmailer();
$mail->CharSet = 'UTF-8';
$mail->From = $from;
$mail->FromName = $from_name;
$mail->AddAddress($to, $to_name); // Add a recipient
$mail->AddCC('nabytek-safr#xxxx.cz');
$mail->WordWrap = 50; // Set word wrap to 50 characters
$mail->IsHTML(true); // Set email format to HTML
$mail->Subject = 'Import Robot Autronic';
$mail->Body = 'xxx';
$mail->AltBody = str_replace("<br />", "\n", 'xxx');
if (!$mail->Send())
{
echo 'Mail Error: ' . $mail->ErrorInfo;
exit;
}
else
{
echo 'OK';
}
die();
Check what text you are sending. It could be that your results are being filtered out and put in a Spam folder. Sometimes a mail with 'test' in the title could be sent right to Spam.
It is impossible to verify that an email was delivered with PHP.
Check your mail server log (usually /var/log/mail) to see if the email was sent.
Send a BCC to an email address that you know has no spam filters.
You can validate real emails with Telnet and MX records. See this answers
https://stackoverflow.com/a/17332773/468891
First, check your spam, if you haven't already.
Second, change your SMTP settings to that of gmails, put your gmail log in and password and try.
If a mail fails to deliver you'll get an delivery- failure notification with possible causes of failure, which helps a lot. A delivery failure can also happen when no error is displayed in PHP.
This always helps me.
I am tying to send an email with an attachment using phpmailer.
include_once('/home/site/PHPMailer/class.phpmailer.php');
$mail = new PHPMailer();
$body = $mail->getFile('contents.html');
$body = eregi_replace("[\]",'',$body);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.free.fr"; // SMTP server
$mail->IsSendmail(); // telling the class to use SendMail transport
$mail->From = "name#sub.fr";
$mail->FromName = "name";
$mail->Subject = "subject";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$mail->AddAddress("sub#sub.net", "name");
$mail->AddAttachment("mylist.csv"); // attachment
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
I receive a "Message sent !" on execution, but no email is ever received.
It is very possible the user you sent the email to did not recieve it due to the fact a lot of mail providers such as AOL and Yahoo block massive amounts of Ip addresses related to email spam. So if the server you are running this php script from is on their blacklist the user will not even receive it to the spam folder.
Check your php email logs.
http://help.yahoo.com/l/us/yahoo/smallbusiness/webhosting/php/php-03.html
Also, if this is not the case, try making sure the file your trying to include exists, and you are passing the correct path to the file. Like #Waygood said, try seeing if it sends without the attachment.