I am using the last version of PHPMailer to send emails from my web. I can send some emails without SMTP parameters, but i need to give the SMTP configuration to my object PHPMailer(). The functions that sends this emails are in the same file.
Why some emails could need SMTP configurations and not others?
Regards
function sendEmailWithoutSMTP(){
$bodyIntern = file_get_contents('emails/myemail.html');
$mailIntern = new PHPMailer(true);
$mailIntern->CharSet = "UTF-8";
try {
//Recipients
$mailIntern->AddAddress('myemail#myemail.com', 'My address');
$mailIntern->SetFrom('myemail#myemail.com', 'My address');
$mailIntern->AddReplyTo('myemail#myemail.com', 'My address');
//Content
$mailIntern->Subject = 'My subject';
$mailIntern->AltBody = '';
$mailIntern->MsgHTML($bodyIntern);
$mailIntern->Send();
} catch (phpmailerException $e) {
//echo $e->errorMessage();
} catch (Exception $e) {
//echo $e->getMessage();
}
}
function sendEmailWithSMTP($email){
require_once 'smtp/getsmtp.php';
$smtp= get_smtp($email);
$bodyClient = file_get_contents('emails/myotheremail.html');
$mailClient = new PHPMailer(true);
$mailClient->CharSet = "UTF-8";
try {
//Server settings
$mailClient->IsSMTP();
$mailClient->SMTPDebug = SMTP::DEBUG_OFF;
$mailClient->SMTPAuth = true;
$mailClient->SMTPSecure = $smtp["secure"];
$mailClient->Host = $smtp["host"];
$mailClient->Username = $smtp["username"];
$mailClient->Password = $smtp["pass"];
$mailClient->Port = $smtp["port"];
$mailClient->Timeout= 30;
//Recipients
$mailClient->AddAddress('myemail#myemail.com', 'My address');
$mailClient->SetFrom('myemail#myemail.com', 'My address');
$mailClient->AddReplyTo('myemail#myemail.com', 'My address');
//Content
$mailClient->Subject = 'My subject';
$mailClient->AltBody = '';
$mailClient->MsgHTML($bodyIntern);
$mailClient->Send();
} catch (phpmailerException $e) {
//echo $e->errorMessage();
} catch (Exception $e) {
//echo $e->getMessage();
}
$this->sendEmailWithoutSMTP();
}
PHPMailer has several different ways of sending messages.
First of all there is the default, which uses PHP's built-in mail() function behind the scenes. This does not use SMTP, but hands the message to a sendmail program using a path set in your php.ini file. This sending method does not require any SMTP setup, and SMTP-related params (e.g. Username, Host) will just be ignored, but it does require that you have a local mail server.
isSendmail() is like a manually-constructed equivalent to PHP's built-in function, but it gives you a bit more control if you are using some unusual local mail server. Generally you should not use this.
If you call isSMTP(), it will try to use SMTP directly, and all of the SMTP parameters will be used. Even if you do have a local mail server, this is the recommended route because it's faster, safer, and easier to debug. If you need to send through a remote mail server (such as gmail), this is your only option.
So in your code, the first function is sending using mail(), and the second uses SMTP.
Related
So I have a contact form that uses phpmailer. It sends an email from one Gmail account to another. But I can't seem to get the receiving email to receive any emails.
The script is hosted on a cpanel (RivalHost) and the domain is on GoDaddy. I asked RivalHost if they were blocking SMTP connections or blocking ports 587 or 465, and they said they weren't. So I have no idea what's causing the issue. The script works perfectly fine on my localhost, just not on cpanel
Heres the mailing script:
<?php
$result="";
if(isset($_POST['submit'])){
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->Host='smtp.gmail.com';
$mail->Port=465;
$mail->SMTPAuth=true;
$mail->SMPTSecure='ssl';
$mail->Username='sendingemail#gmail.com';
$mail->Password='*********';
$mail->setFrom('sendingemail#gmail.com');
$mail->addAddress('receivingemail#gmail.com');
$mail->addReplyTo($_POST['email'],$_POST['name']);
$mail->isHTML(true);
$mail->Subject='Contact: '.$_POST['subject'];
$mail->Body='Message: '.$_POST['msg'].'</h1>';
if(!$mail->send()){
$result='something went wrong';
echo $result;
} else {
$result="thank you";
echo $result;
}
}
?>
I was also told to check my MX records, but wasn't really sure what to change them to, or if I needed to change them at all:
MX 0 ********.com 3599 RBL
Solution 1:
PHPMailer uses exeptions. You can put your code in try/catch block to get exceptions caused emails not to be sent.
$mail = new PHPMailer(true); //defaults to using php "mail()"; the true param means it will throw exceptions on errors, which we need to catch
try {
//Email information comes here
$mail->Send();
echo "Message Sent OK\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
}
Solotion 2:
Are you also using the CSF firewall? If so, check to see if the "SMTP_BLOCK" setting is enabled. If STMP_BLOCK is enable contact to hosting to disable it.
Add this to your settings:
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
if (!$mail->send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message sent!';
}
Can someone tell me why phpMailer not working on my webhost. I test my website locally with XAMMP and email sending with phpMailer is working correctly but when I upload my files to my webhost server it doesn't work. my script do not tell me any errors. neither my server error_log message is appended.
Is there any configuration that I may need to do on my server?
This is how am using phpMailer:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
//mailer function
function sendmail($mailFrom,$orgName,$toMail,$replyTo,$mailSugject,$mailBody){
//Load composer's autoloader
require 'mailer/vendor/autoload.php';
$mail = new PHPMailer(true);
try {
//Recipients
$mail->setFrom($mailFrom, $orgName);
$mail->addAddress($toMail);// Add a recipient
$mail->addReplyTo($replyTo, $orgName);
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $mailSugject;
$mail->Body = $mailBody;
$mail->send();
return array('Mailer' => 1,'msg' => 'Message has been sent');
} catch (Exception $e) {
return array('Mailer' => $mail->ErrorInfo ,'msg' => 'Message could not be sent');
}
}
So I am using phpmailer using smpt and it is going thru postfix to send emails. When I send a email from my email it goes thru without a problem when it comes to using DKIM and DMARC. But when I send using phpmailer Im not getting a DKIM.
<?php
function send_email($to, $from_email, $from_name, $subject, $body,
$is_html=false, $attachments=null) {
global $smtp_host, $smtp_port, $smtp_user, $smtp_password;
try {
$email = new PHPMailer(true);
if ($from_email === $smtp_user) {
$email->isSMTP();
$email->Host = $smtp_host;
$email->Port = $smtp_port;
$email->SMTPAuth = true;
$email->Username = $smtp_user;
$email->Password = $smtp_password;
$email->SMTPSecure = 'tls';
}
$email->CharSet = 'UTF-8';
$email->From = $from_email;
$email->FromName = $from_email;
$email->Subject = $subject;
$email->Body = $body;
$email->AddAddress($to);
if ($is_html == true) {
$email->IsHTML(true);
$email->Encoding = 'base64';
}
if ($attachments != null) {
foreach ($attachments as $attachment) {
$apath = $attachment["path"];
$aname = $attachment["name"];
$email->AddAttachment($apath , $aname);
}
}
$email->Send();
$status = "success";
}
catch (phpmailerException $e) {
$status = $e->errorMessage();
}
catch (Exception $e) {
$status = $e->getMessage();
}
return $status;
}
So I think I need to add this to my code but I'm not sure if I have to add this to the code. I was thinking that opendkim would just add the DKIM to the header. But its not.
$email->DKIM_domain = 'mydomain.com';
$email->DKIM_private = '/path/to/private_key';
$email->DKIM_selector = 'default';
$email->DKIM_passphrase = '1234567';
There are several ways you can implement DKIM signing.
With those properties in PHPMailer, where your client script needs
direct access to your private keys. Good when you have no control over the sending environment - e.g. on shared hosting, but it means each individual sending script is responsible for signing, which isn't ideal.
Getting your mail server to do the signing for you. Good when you have you own mail server and the ability to configure it - all mail that goes through it can be signed automatically, and you don't have to do anything at the client end.
Using a signing SMTP relay/proxy server in line
with your existing mail server, such as Hmailserver for Windows. Good when you have your own mail server, but don't have admin access to it, or it can't do DKIM.
The selector needs to match the key you're signing with, so if you have a selector called s1, you would expect the public key to be available in a TXT record called s1._domainkey in your domain's DNS. The matching private key just needs to be somewhere safe and web-inaccessible on the server.
The DNS and key arrangements are the same whichever signing mechanism you use. If you use PHPMailer's DKIM, you don't need openDKIM, but if you want to use OpenDKIM, you need to tell it which selector you want to use in its config. Some mail servers (like GreenArrow that I use) allow dynamic control of selectors via custom message headers, but I don't think OpenDKIM supports that. You may be able to set up virtual MTAs within postfix that allow something similar.
For a PHPMailer reference, look at the DKIM signing example provided, and the DKIM test in the test suite.
I´m having trouble sending a e-mail with the PHPMailer class, after submit form i have a message mail send ok but i don't recive any mail.
I guess the problem is with the SMTP authentication, but I couldn´t find the problem.the source application are stored in a distant server with ip adress:175.2.3.69 and i use outlook count to send mail
The code with problem is:
require_once('../libs/PHPMailer/class.phpmailer.php');
//Ensuite on débute l'envoi de mail
$mail = new PHPmailer();
$mail->IsSMTP(); // telling the class to use SMTP
try {
$mail->Host = "messagerie.abc.a.fr"; // SMTP server
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 25; // set the SMTP port for the GMAIL server
$mail->Username = "username.name#a-bc.fr"; // SMTP account username
$mail->Password = "password"; // SMTP account password
$mail->AddReplyTo('username.name#a-bc.fr', 'First Last');
$mail->AddAddress('username.name#a-bc.fr', 'John Doe');
$mail->SetFrom('username.name#a-bc.fr', 'First Last');
$mail->AddReplyTo('username.name#a-bc.fr', '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->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!
}
PHPMailer will not throw exceptions unless you pass true to the constructor, like $mail = new PHPmailer(true);, so your code will not cause any exceptions to catch, nor report any errors. I suggest you set $mail->SMTPDebug = 3; to get more feedback on the problem.
I'm trying to do a very simple mail form in PHP but get the error:
PHP Notice: Undefined index: in /var/www/html/sites/send_contact.php on line 10, referer: http://example.com/contact2.php
My PHP file looks like this:
<?php // send_contact.php
// Contact subject
$subject =$_POST["$subject"];
// Details
$message=$_POST["$detail"];
// Mail of sender
$mail_from=$_POST["$customer_mail"];
// From
$name2=$_POST["$name2"];
$header="From: $mail_from\r\n";
// Enter your email address
$to="joe#mail.com";
$send_contact=mail($to,$subject,$message,$header);
// Check, if message sent to your email
// display message "We've recived your information"
if($send_contact){
echo "We've recived your contact information";
}
else {
echo "ERROR";
}
?>
The easiest guess is that you're doing a mistake accessing your variables.
instead of:
$name2=$_POST["$name2"];
use this:
$name2=$_POST["name2"];
Or, if you know the difference and are doing this on purpose, make sure your $name2 variable is defined with the correct name of the HTML form field.
As an aside, I would strongly recommend using a library like PHPMailer to send emails.
Your example is quite simple and the mail() should work just fine, but for anything more elaborate (ie. having attachments or html) or needing to send using an external mail server by SMTP (with or without authentication), it will do a much better job and save you lots of time.
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!
}
The error is telling you exactly what is wrong:
Line 10:
$name2=$_POST["$name2"];
You are using '$name2' before it is defined.
PHP can substitute variables in strings:
$var1 = "bar";
echo "foo $var1"; // prints "foo bar"
In your HTML use something similar to the following:
<input type="...whatever..." name="name2" />
Then in PHP, assuming the data was POSTed, you would access it using:
$name2 = $_POST["name2"];
It seems that your code doesn't pass the $_POST data as you expect. I recommend you to check the keys of $_POST as follows:
<?php
print_r(array_keys($_POST));
?>
If it doesn't display the value of $name2, it means you should modify the web page sending form data to send_contact.php.