I am using PHPMailer and smtp.gmail.com to send emails to my users.
Emails are sent, no problem with that, but on the client side, in the sender email address, there is showing my servers host email address, not my email address that is set with PHPMailer->SetFrom(). I want to set my email address as sender email.
I'm using php 5.4 & PHPMailer 5.2.4
My code is given below :
<?php
require_once('class.phpmailer.php');
$mail = new PHPMailer(true);
try {
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->Username = "myemail#gmail.com";
$mail->Password = "PASSWORD";
$mail->AddAddress('receiver#email.com', 'John Doe');
$mail->SetFrom('myemail#email.com', 'My Name');
$mail->addReplyTo('myemail#gmail.com', 'My Name');
$mail->Subject = 'PHPMailer Subject';
$mail->MsgHTML('This is the body');
$mail->Send();
echo "Message Sent";
} catch (phpmailerException $e) {
echo $e->errorMessage();
} catch (Exception $e) {
echo $e->getMessage();
}
?>
This is a gmail limitation - it does not let you set arbitrary from addresses, though you can set fixed aliases in your gmail preferences. This is covered in the PHPMailer documentation. It's also a reasonable restriction - otherwise you're probably forging the from address. You can always set a reply-to address if it's reply routing you're concerned about.
Related
So i thought that if you send email to hotmail it would at least arrive in spam but in this case it does not. So have i done something wrong in this code below? I tested the same code with Gmail and it works Perfectly. Sure i think its related to Hotmail Filters.
Things to note, I have setup the email address on my domain and i have an SPF Record setup.
What could be wrong? And how can i check it?
Current PHP:
<?php
require 'PHPmailer/PHPMailerAutoload.php';
$mail = new PHPMailer(true);
try {
$mail->SMTPDebug = true;
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "smtpout.secureserver.net"; // sets the SMTP server
$mail->Port = 26; // set the SMTP port
$mail->Username = "username"; // SMTP account username
$mail->Password = "password"; // SMTP account password
$mail->setFrom('email', 'Immortal Insurance');
$mail->AddAddress('sendtoemail');
$mail->AddAddress('sendtoemail');
$mail->Body = 'This is a test Email';
$mail->AddAttachment('tosend/policy.pdf');
$mail->Send();
echo "Message Sent OK<p></p>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage();
} catch (Exception $e) {
echo $e->getMessage();
}
?>
I have been developing a page that will automatically send emails from a Gmail account to mail accounts of various origin for specific users of my site. I have this working perfectly on Localhost. The mails are received by the users and also deposited in the "Sent Items" of the Gmail account.
However, as soon as I migrate this to the server we are using it no longer works. The code is as follows :-
$mail = new PHPMailer();
try
{
$mail->CharSet = "UTF-8";
// telling the class to use SMTP
$mail->IsSMTP();
// enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPDebug = 0;
// enable SMTP authentication
$mail->SMTPAuth = true;
// sets the prefix to the servier
$mail->SMTPSecure = "tls";
// sets GMAIL as the SMTP server
$mail->Host = "smtp.gmail.com";
// set the SMTP port for the GMAIL server
$mail->Port = 587;
// GMAIL username
$mail->Username = "xxxxxxx#gmail.com";
// GMAIL password
$mail->Password = "P4ssw0rd";
//Set reply-to email this is your own email, not the gmail account
//used for sending emails
$mail->SetFrom($from);
$mail->FromName = "XXX";
$mail->AddReplyTo('xxxxxxx#gmail.com' , 'XXX');
// Mail Subject
$mail->Subject = $subject;
//Main message
$mail->MsgHTML($message);
$mail->AddAddress($to, "");
$mail->Send();
}
catch (phpmailerException $e)
{
echo $e->errorMessage(); //Pretty error messages from PHPMailer
}
catch (Exception $e)
{
echo $e->getMessage(); //Boring error messages from anything else!
}
Setting the Debug to 1 on localhost produces the expected errors if something goes wrong. However, nothing seems to happen on the Server. Please help.
I have a problem with Replying mails with SMTP through PHPMailer. When I try to send the mail I get
"You must provide at least one recipient email address."
The following PHP Code I use is:
require("smtp/class.phpmailer.php");
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 0; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username = '****#gmail.com';
$mail->Password = '***';
$mail->SetFrom('***#gmail.com', '***#gmail.com');
$mail->Subject = 'RE: Hello World';
$mail->Body = 'Hello World';
$mail->AddReplyTo('****#gmail.com');
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
return false;
} else {
$error = 'Message sent!';
return true;
}
I would like to know what else I'm lacking in the configuration.
You're missing a To address. You can add one like so:
$mail->AddAddress('josh#example.net', 'Josh Adams');
See a full example here: https://github.com/PHPMailer/PHPMailer#a-simple-example
The Reply-To header designates the default/recommended address to use when the recipient clicks "reply."
AddReplyTo is used to add a Reply-To address. Responses to messages you send with a reply-to address are delivered to that address.
Say, you send an email to one of your visitors with the reply-to address set to support#example.com. When they reply to that email, it will be sent to the email you specified as AddReplyTo.
If you're trying to send an email to yourself, you can just use AddAddress instead.
$mail->AddAddress('someone#example.net', 'JohnDoe');
Hope this helps!
Documentation: phpMailer methods, phpMailer examples.
Use $mail->AddAddress() instead of $mail->AddReplyTo().
I have been testing the following code for hours. The email will send to the addresses added through $mail->AddAddress() and in the received email it states the cc but the person cced does not receive the email. I have looked everywhere and can not find a solution to why this is happening. I have run tests and all variables are being submitted to this code properly.
My server is running Linux Red Hat
My Code:
require_once('../smtp/class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
$mail->IsSMTP(); // telling the class to use SMTP
try {
$mail->SMTPDebug = 0; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the server
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = $port; // set the SMTP port for the GMAIL server 465 or 587
$mail->Username = $username; // GMAIL username
$mail->Password = $password; // GMAIL password
// Add each email address
foreach($emailTo as $email){ $mail->AddAddress(trim($email)); }
if($cc!=''){ foreach($cc as $email){ $mail->AddCC(trim($email)); } }
if($bcc!=''){ foreach($bcc as $email){ $mail->AddBCC(trim($email)); } }
$mail->SetFrom($emailFrom, $emailName);
$mail->AddReplyTo($emailFrom, $emailName);
$mail->Subject = $subject;
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML($content);
// $mail->AddAttachment('images/phpmailer.gif'); // attachment
// $mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
$mail->Send();
echo'1';exit();
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
Old question, but I ended up here looking for an answer. Just learned elsewhere that those functions AddCC and AddBCC only work with win32 SMTP
Try using:
$mail->addCustomHeader("BCC: mybccaddress#mydomain.com");
See http://phpmailer.worxware.com/?pg=methods
Hope this helps someone, cheers!
$address = "xxxxx#gmail.com";
$mail->AddAddress($address, "technical support");
$address = "yyyyyy#gmail.com";
$mail->AddAddress($address, "other");
$addressCC = "zzzzzz#gmail.com";
$mail->AddCC($addressCC, 'cc account');
$addressCC = "bcc#gmail.com";
$mail->AddBCC($addressCC, 'bcc account');
I'm running fedora core 9 with php 5.2.6. I want to send a simple email from a form using the php mail command.
Do I need to configure my system to be an smtp server? If so do I have to load something like pear?
I've read that pear is installed by default. however when I go to /usr/lib/php/pear , the folder is empty.
When I go to install pear with yum install php-pear none of the mirrors are available.
Does anyone have any suggesions on what mail service to use? If it's installed by default and where I can figure out how to configure.
Thanks!
Joe
This the code I'm using:
<?php
require_once('../class.phpmailer.php');
$mail = new PHPMailer(true);
$mail->IsSMTP();
try {
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = "587"; // set the SMTP port for the GMAIL server
$mail->Username = "joestestemail#gmail.com"; // GMAIL username
$mail->Password = "joeiscool"; // GMAIL password
//This is the "Mail From:" field
$mail->SetFrom('joestestemail#gmail.com', 'First Last');
//This is the "Mail To:" field
$mail->AddAddress('joe12345#mailcatch.com', 'John Doe');
$mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
$mail->Body = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
$mail->Send();
echo "Message Sent OK<p></p>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
?>
I would strongly recommend using a library like PHPMailer to send emails.
You can use it with the server's own mail service or with any other server on the internet (your ISPs, Gmail, etc).
For a better idea, check this example from their website:
require_once('../class.phpmailer.php');
$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
$mail->IsSMTP(); // telling the class to use SMTP
try {
$mail->Host = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "mail.yourdomain.com"; // sets the SMTP server
$mail->Port = 26; // set the SMTP port for the GMAIL server
$mail->Username = "yourname#yourdomain"; // SMTP account username
$mail->Password = "yourpassword"; // SMTP account password
$mail->AddReplyTo('name#yourdomain.com', 'First Last');
$mail->AddAddress('whoto#otherdomain.com', 'John Doe');
$mail->SetFrom('name#yourdomain.com', 'First Last');
$mail->AddReplyTo('name#yourdomain.com', 'First Last');
$mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML(file_get_contents('contents.html'));
$mail->AddAttachment('images/phpmailer.gif'); // attachment
$mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
$mail->Send();
echo "Message Sent OK<p></p>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
Edit:
Following up on your comment..
For a simple GMail example, try this:
require_once('../class.phpmailer.php');
$mail = new PHPMailer(true);
$mail->IsSMTP();
try {
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail->Username = "yourusername#gmail.com"; // GMAIL username
$mail->Password = "yourpassword"; // GMAIL password
//This is the "Mail From:" field
$mail->SetFrom('name#yourdomain.com', 'First Last');
//This is the "Mail To:" field
$mail->AddAddress('whoto#otherdomain.com', 'John Doe');
$mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
$mail->Body = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
$mail->Send();
echo "Message Sent OK<p></p>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
Edit2:
The reason why you're getting this error is because you have an extra ' on your string:
Replace
$mail->Host = "'smtp.gmail.com";
with:
$mail->Host = "smtp.gmail.com";
The easiest way to get mail sending up and running is to install and configure postfix. You can use:
yum install postfix
And check out the documentation here:
http://www.postfix.org/docs.html
You can use the LAMPStack from BitNami. It comes with an easy installer that installs everything you need and you'll be running in minutes.