Code:
<?php
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->CharSet="utf-8";
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = '172.17.224.12'; // Specify main and backup SMTP servers
$mail->SMTPAuth = false; // Enable SMTP authentication
//$mail->Username = '***'; // SMTP username
//$mail->Password = '***'; // SMTP password
$mail->SMTPSecure = 'TLS'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 25; // TCP port to connect to
$mail->setFrom('itslt#example.com', 'Company.com');
$mail->addReplyTo('itslt#example.com');
$mail->addAddress('dichitslt#example.com'); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$bodyContent = '<h1>How to Send Email using PHP in Localhost by CodexWorld</h1>';
$bodyContent .= '<p>This is the HTML email sent from localhost using PHP script by <b>CodexWorld</b></p>';
$mail->SMTPDebug = 2;
$mail->Subject = 'Email from Localhost by CodexWorld';
$mail->Body = $bodyContent;
if(!$mail->send()) {
echo 'Message could not be sent.<br>';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>
When I run this code, it says message has been sent. However, when I look at my email, there's a notification saying that the email sent through my code was bounced back.
What could be the possible problem? was it the configuration in my code or was it the server?
screencap of the bounce back email
For those who encounter the same problem, what resolved to this problem is smozgur's answer.
I think the server you are trying to send email through php - your
local server - is not authorized to send email and considered as SPAM
(like someone else is trying to send email by using your credentials).
When I need to make my scripts sends email from the server, then I add
my server IP as the SPF record in the DNS records. That way, my email
server knows that IP is sending email in my knowledge and allows. In
your case, I think you can likely add your IP to temporarily solve the
problem. But you need to do that for the production server IP later as
well. If this is the case of course..
I contacted supervisor and told him the status of the email function, which is emails are being sent, but the mail server returns the emails to the sender. Also, the possible cause of the problem, which is the local server is not authorized to send email and considered as SPAM. Lastly, the possible solution, which is add the server IP as the SPF record in DNS record.
Related
This code is work, email is sent
$mail = new PHPMailer();
$mail->setFrom("name1#gmail.com", "Name");
$mail->addAddress($to); //Recipient name is optional
//Address to which recipient will reply
$mail->addReplyTo("name1#yahoo.com", "Reply");
//Send HTML or Plain Text email
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $body;
if(!$mail->send())
{
echo 'Mailer Error: ' . $mail->ErrorInfo . "\n";
}
But for some reason, if I change From email to name2#yahoo.com
$mail->setFrom("name2#yahoo.com", "Name");
emails does not sends anymore. Phpmailer does not report any error messages.
name2#yahoo.com is valid working email address related to this web server.
Thanks.
This is covered in the PHPMailer troubleshooting guide.
Most service providers now have strict SPF and DMARC configurations (Yahoo especially, since they invented DMARC) that mean that you cannot send from addresses in their domains except through their own mail servers, or any others included in their SPF records.
Your code is sending through your own local server, which is not a Yahoo server, and thus won't work.
The solution is to send through Yahoo's own servers using authentication for your email account, something like:
$mail->isSMTP();
$mail->Host = 'smtp.mail.yahoo.com';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->Username = 'me#yahoo.com';
$mail->Password = 'password';
Yahoo's DMARC config won't let you spoof the from address, so you can only use a from address that matches your username - this is probably the cause of the symptom you're seeing.
I'm using godaddy for hosting my site and using default godaddy mail service.
Now i want to sent email using php mail function to other email address from my 1 of my 15 email address of my godaddy's email accounts
How can i fix that from which email address email will be sent and how to place the username and password for the email address ?
Thanks
The PHP mail function uses the mailserver configured for that webhost. You can't change that. Since godaddy controls the mailserver they control what headers it sends. You could try inserting a custom From header but I doubt that will work. It will either get modified, flagged as spam, or rejected.
If you have 15 accounts at godaddy, perhaps it's time to look for a more serious hosting solution?
Instead of using the mail() function, which just calls the OS mail function (i.e. sendmail), try something like SwiftMail (free PHP mail library). It support many different ways of sending mail, including logging into a mail account and sending email, just like you would do from your own computer. You could even send email from a gmail account if you wanted.
http://swiftmailer.org/
I am using godaddy hosting . Just keep some fields blank and send mail it will work .
please see below code its working for me.
<?php
include("class.phpmailer.php");
function sendMail($address,$username,$body){
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
//$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
// $mail->SMTPAuth = true; // enable SMTP authentication
// $mail->SMTPSecure = "ssl"; // sets the prefix to the servier
// $mail->Host = "smtp.gmail.com"; // sets as the SMTP server
// $mail->Port = 465; // set the SMTP port for the server
// $mail->Username = "xyz#gmail.com"; // username
// $mail->Password = "test121232"; // password
$mail->SetFrom('contact#example.co.in', 'Contact');
$mail->Subject = "Enquiry for tour and travels package";
$mail->MsgHTML($body);
$address = $address;
$mail->AddAddress($address, $username);
$mail->AddCC('contact#example.co.in');
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
}
?>
just changed from email address, so you can send mail through this email id.
$mail->SetFrom('youremail#example.co.in', 'Contact');
I've had this issue on a couple of client accounts.
After MUCH playing around, I found that it didn't matter what email app or code you use.
I finally compared two accounts. One where I know email sends from PHP, and one where it doesn't. The difference I found was:
Working account had these 2 dns entries on the domain:
CNAME / mail / #
MX / # / mail.yourdomainname.com
And the MX Entry set within CPanel Zone Editor of:
Destination: yourdomainname.com
The account that was NOT working was the same, except it did not have the CNAME entry mentioned above. I added it, waited a few minutes and tested my code again and I finally started receiving the emails from my code!
This one worked for me.
I asked Godaddy and support they told me to use relays. So Instead of using mail() function, I used PHPMailer. Kindly check below the configuration for PHP Mailer.
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Host = "relay-hosting.secureserver.net";
$mail->Port = 25;
$mail->setFrom('from#mail.com', 'From Mail');
$mail->addAddress('to#mail.com', 'To Mail');
$mail->Subject = 'PHPMailer GMail SMTP test';
$mail->msgHTML("test body 123");
$mail->AltBody = 'HTML messaging not supported';
if(!$mail->send()){
echo "Mailer Error: " . $mail->ErrorInfo;
}else{
echo "Message sent!";
}
Note: Here from#mail.com is my mail ID which is in cPanel mail accounts.
I do not know if what I want to do is possible (but finding out that it isn't would be useful in itself).
I cannot use my company's gmail account "real.business#gmail.com" directly with PHPMailer. I can, however, use an intermediary gmail account "fake.12345.account#gmail.com" which can have "less secure apps" enabled, which permits SMTP verification.
However I do not want to have the emails be sent from this fake.12345.account#gmail.com account (wouldn't look particularly professional) - but rather the company's gmail account.
I can send the emails from the intermediary account to real.business#gmail.com; either through the editing of the PHPMailer parameters, or by automatically forwarding emails from fake.12345.account#gmail.com to the company account.
The problem lies in how real.business#gmail.com can then successfully email the email (or at least appear to be the sender), as originally intended.
The code so far
$Mail = new PHPMailer();
$Mail->IsSMTP(); // Use SMTP
$Mail->Host = "smtp.gmail.com"; // Sets SMTP server for gmail
$Mail->SMTPDebug = 0; // 2 to enable SMTP debug information
$Mail->SMTPAuth = TRUE; // enable SMTP authentication
$Mail->SMTPSecure = "tls"; //Secure conection
$Mail->Port = 587; // set the SMTP port to gmail's port
$Mail->Username = 'fake.12345.account#gmail.com'; // gmail account username
$Mail->Password = 'a_password'; // gmail account password
$Mail->Priority = 1; // Highest priority - Email priority (1 = High, 3 = Normal, 5 = low)
$Mail->CharSet = 'UTF-8';
$Mail->Encoding = '8bit';
$Mail->Subject = 'Mail test';
$Mail->ContentType = 'text/html; charset=utf-8\r\n';
$Mail->From = 'testing.num.101#gmail.com'; //Your email adress (Gmail overwrites it anyway)
$Mail->FromName = 'Testing Again';
$Mail->WordWrap = 900; // RFC 2822 Compliant for Max 998 characters per line
$Mail->addAddress($personEmail); // To: the PERSON WE WANT TO EMAIL
$Mail->isHTML( TRUE );
$Mail->Body = ' Good news '.$personName.'! The email sent correctly!';
$Mail->AltBody = 'This is a test mail';
$Mail->Send();
$Mail->SmtpClose();
if(!$Mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $Mail->ErrorInfo;
exit;
}
So the issue is: not having the email sent to $personEmail from fake.12345.account#gmail.com (that's trivial) but rather how to send the email from fake.12345.account#gmail.com to real.business#gmail.com such that real.business#gmail.com forwards the message to $personEmail
What you're describing is really relaying, which is usually configured in the mail server config (not the messages), but you don't have access to anything like that in gmail.
You can set allowed aliases in gmail, but I would guess that these are not allowed to overlap with existing gmail account names as that would be a major security hole. Why not enable "less secure apps" on the main account? It's not as if it is actually any less secure - if anything it's better, because the setup to use OAuth2 is so deeply complex and unpleasant...
That said, rather than trying to do all this forgery, you may be interested in this PR and associated docs. It's fairly likely the xoauth branch will get merged into master and released without any further changes as PHPMailer 5.2.11, and it would very helpful if you could give it a try.
PHPMailer is made for sending.
What you want to do is forward an email. This implies receiving the email and then sending it through.
What you need is some kind of IMAP client in php, that will allow you to read the emails on fake.12345.account#gmail.com (and maybe real.business#gmail.com). Then save their body and title and pass it to PHPMailer. You can then use PHPMailer to send the emails with real.business#gmail.com.
Hello I am trying to get a website up and running. It is currently hosted on AWS, so I do not have my own smtp server running at this moment. So after reading a few articles, I have understood that we could used gmail as a smtp server.
I wanted to double check if what I read was right, I am going to use smart job board software, can I plug in the values provided by gmail and use that as an smtp server??
Yes, Google allows connections through their SMTP and allows you to send emails from your GMail account.
There are a lot of PHP mail scripts that you can use. Some of the most popular SMTP senders are: PHPMailer (with an useful tutorial) and SWIFTMailer (and their tutorial).
The data you need to connect and send emails from their servers are your GMail account, your password, their SMTP server (in this case smtp.gmail.com) and port (in this case 465) also you have to make sure that emails are being sent over SSL.
A quick example of sending an email like that with PHPMailer:
<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true; // SMTP authentication
$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->Port = 465; // SMTP Port
$mail->Username = "john.doe#gmail.com"; // SMTP account username
$mail->Password = "your.password"; // SMTP account password
$mail->SetFrom('john.doe#gmail.com', 'John Doe'); // FROM
$mail->AddReplyTo('john.doe#gmail.com', 'John Doe'); // Reply TO
$mail->AddAddress('jane.doe#gmail.com', 'Jane Doe'); // recipient email
$mail->Subject = "First SMTP Message"; // email subject
$mail->Body = "Hi! \n\n This is my first e-mail sent through Google SMTP using PHPMailer.";
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
?>
I successfully use the GMail SMTP server.
We do have a corporate GMail account, though I don't think that matters. A personal GMail account should suffice.
I do not have a PHP sample however the following configuration for ASP.Net should provide adequate guidance:
<mailSettings>
<smtp from="me#gmail.com">
<network enableSsl="true" host="smtp.gmail.com" port="587" userName="me#gmail.com" password="mypassword" />
</smtp>
</mailSettings>
If someone has a suitable PHP sample, feel free to edit my answer or post your own.
Authentication is required I believe, but I don't see why not. I won't do the research for you, but there are a couple things to look into:
Their SMTP server requires TLS encryption and is hosted on a non-standard port (995). You will need to ensure that AWS supports both of these options for SMTP outbound.
There is probably a cap on emails you can send before being marked as spam. You should research this and ensure it is not beyond your requirements.
You can user PHPMailer class for this job. And you can easily configure the smtp.
An example of configuration
if (class_exists(#PHPMailer)) {
$smtp_mail = new PHPMailer();
$smtp_mail->isSMTP();
$smtp_mail->SMTPAuth = true; // enable SMTP authentication
$smtp_mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$smtp_mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$smtp_mail->Port = 465; // set the SMTP port
$smtp_mail->Username = "info#example.com"; // GMAIL username
$smtp_mail->Password = "password"; // GMAIL password
$smtp_mail->From = "info#example.com";
$smtp_mail->FromName = "Name";
$smtp_mail->AltBody = "This is the body when user views in plain text format"; //Text Body
$smtp_mail->WordWrap = 50; // set word wrap
$smtp_mail->AddReplyTo("info#example.com","Name");
$smtp_mail->isHTML(true); // send as HTML
}
Although you can technically use Gmail as SMTP server, it is not recommended for larger websites. By time you may receive issues like "421 4.7.0 Temporary System Problem" or similar, is it was not designed to be used by an application, rather a single person.
Related issue for the error message above:
Gmail SMTP error - Temporary block?
I'm using godaddy for hosting my site and using default godaddy mail service.
Now i want to sent email using php mail function to other email address from my 1 of my 15 email address of my godaddy's email accounts
How can i fix that from which email address email will be sent and how to place the username and password for the email address ?
Thanks
The PHP mail function uses the mailserver configured for that webhost. You can't change that. Since godaddy controls the mailserver they control what headers it sends. You could try inserting a custom From header but I doubt that will work. It will either get modified, flagged as spam, or rejected.
If you have 15 accounts at godaddy, perhaps it's time to look for a more serious hosting solution?
Instead of using the mail() function, which just calls the OS mail function (i.e. sendmail), try something like SwiftMail (free PHP mail library). It support many different ways of sending mail, including logging into a mail account and sending email, just like you would do from your own computer. You could even send email from a gmail account if you wanted.
http://swiftmailer.org/
I am using godaddy hosting . Just keep some fields blank and send mail it will work .
please see below code its working for me.
<?php
include("class.phpmailer.php");
function sendMail($address,$username,$body){
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
//$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
// $mail->SMTPAuth = true; // enable SMTP authentication
// $mail->SMTPSecure = "ssl"; // sets the prefix to the servier
// $mail->Host = "smtp.gmail.com"; // sets as the SMTP server
// $mail->Port = 465; // set the SMTP port for the server
// $mail->Username = "xyz#gmail.com"; // username
// $mail->Password = "test121232"; // password
$mail->SetFrom('contact#example.co.in', 'Contact');
$mail->Subject = "Enquiry for tour and travels package";
$mail->MsgHTML($body);
$address = $address;
$mail->AddAddress($address, $username);
$mail->AddCC('contact#example.co.in');
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
}
?>
just changed from email address, so you can send mail through this email id.
$mail->SetFrom('youremail#example.co.in', 'Contact');
I've had this issue on a couple of client accounts.
After MUCH playing around, I found that it didn't matter what email app or code you use.
I finally compared two accounts. One where I know email sends from PHP, and one where it doesn't. The difference I found was:
Working account had these 2 dns entries on the domain:
CNAME / mail / #
MX / # / mail.yourdomainname.com
And the MX Entry set within CPanel Zone Editor of:
Destination: yourdomainname.com
The account that was NOT working was the same, except it did not have the CNAME entry mentioned above. I added it, waited a few minutes and tested my code again and I finally started receiving the emails from my code!
This one worked for me.
I asked Godaddy and support they told me to use relays. So Instead of using mail() function, I used PHPMailer. Kindly check below the configuration for PHP Mailer.
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Host = "relay-hosting.secureserver.net";
$mail->Port = 25;
$mail->setFrom('from#mail.com', 'From Mail');
$mail->addAddress('to#mail.com', 'To Mail');
$mail->Subject = 'PHPMailer GMail SMTP test';
$mail->msgHTML("test body 123");
$mail->AltBody = 'HTML messaging not supported';
if(!$mail->send()){
echo "Mailer Error: " . $mail->ErrorInfo;
}else{
echo "Message sent!";
}
Note: Here from#mail.com is my mail ID which is in cPanel mail accounts.