Need to send a mail (bash or php script) using "sendmail" of a different linux server - linux

Any means of specifying the IP address we want to connect to send that mail.?

Use PHP`s PEAR library class Mail. It's really straightforward.
Example:
(it's using remote SMTP server with SMTP AUTH, you don't need to use it)
<?php
require_once "Mail.php";
// mail data
$from = "You <sender#example.com>";
$to = "Her <recipient#example.com>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
// SMTP server info
$host = "mail.example.com";
$username = "smtp_username";
$password = "smtp_password";
// create mail headers
$headers = array(
'From' => $from,
'To' => $to,
'Subject' => $subject);
// create PEAR Mail object passing SMTP server info
$smtp = Mail::factory('smtp',
array (
'host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
// send the email
$mail = $smtp->send($to, $headers, $body);
// check the result
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>

Related

PHPMailer not working when function is within and included file

Something is mighty odd. I created a basic test file with the code from PHPMailer.
# Import the PHPMailer class into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require 'includes/PHPMailer/PHPMailer.php';
require 'includes/PHPMailer/SMTP.php';
require 'includes/PHPMailer/Exception.php';
function sendCampaignEmail ($email, $firstname, $lastname)
{
$mail = new PHPMailer(true); // Create a new PHPMailer instance. Passing `true` enables exceptions.
try {
//Server settings
# $mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output. SMTP::DEBUG_SERVER = client and server messages
# $mail->SMTPDebug = SMTP::DEBUG_CLIENT; // SMTP::DEBUG_CLIENT = client messages
$mail->SMTPDebug = SMTP::DEBUG_OFF; // SMTP::DEBUG_OFF = off (for production use)
$mail->isSMTP(); // Send using SMTP
$mail->Host = 'myhostxxxx.co.uk'; // Set the SMTP server to send through
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'oobaclub#xxxxxxxxx.co.uk'; // SMTP username
$mail->Password = 'xxxxxmyEmailPasswordxxxxx'; // SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
$mail->Port = 465; // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
//Recipients
$mail->setFrom('myfromemail.co.uk', 'My Name');
$mail->addAddress($email, $firstname . ' ' . $lastname); // Add a recipient
$mail->addReplyTo('myfromemail.co.uk', 'My Name');
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'Dear ' . $firstname . ' ' . $lastname . '<br/><br/>This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo '<br/>Message has been sent to ' . $email . ' (' . $firstname . ' ' . $lastname . ')';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
}
$names= array();
$id=0;
$names[$id]['email'] = "atestemail#gmail.com";
$names[$id]['first_name'] = "Bob";
$names[$id]['last_name'] = "gMail"; $id++;
$names[$id]['email'] = "anothertest#testingmyemail.co.uk";
$names[$id]['first_name'] = "Sid";
$names[$id]['last_name'] = "Smith"; $id++;
$count=0;
while ($count < count($names))
{
sendCampaignEmail ($names[$count]['email'], $names[$count]['first_name'], $names[$count]['last_name']);
$count++;
}
THIS CODE ABOVE WORKS FINE.
So... Then, I took the function and put it into an Included file (where all my functions are: "globalfunctions.php").... And now it says "Fatal error: Uncaught Error: Class 'PHPMailer' not found"
So, now, at the top of my index.php, I have:
## PHP MAILER - # Import the PHPMailer class into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require_once 'includes/PHPMailer/PHPMailer.php';
require_once 'includes/PHPMailer/SMTP.php';
require_once 'includes/PHPMailer/Exception.php';
require_once 'includes/connect.php';
require_once 'includes/globalfunctions.php';
I am confused as everything else works. All my other functions work. I thought namespaces were global... But, I tried adding the "use" code into the function... but, as expected, that didn't work either....
I am stumped.
PHP use declarations are local aliases that apply only to the file they appear in. You have to add a use statement in every file that uses that class; you can't put all your declares in one file and then include it from somewhere else.
Now might be a good time to learn how to use composer as it takes care of a lot of this.

Mail sending using PHP Mailer in Laravel 7

I am using PHPMailer in Laravel 7. Mail works fine. Send Email successful. But after click 'send' button it shows some line about server after this, showing the success message.
This lines are show -
2021-01-04 19:27:49 SERVER -> CLIENT: xxxx
2021-01-04 19:27:49 CLIENT -> SERVER:
2021-01-04 19:27:49 SERVER -> CLIENT:
2021-01-04 19:27:49 SERVER -> CLIENT: xxxxx
2021-01-04 19:27:49 CLIENT -> SERVER: xxxx
2021-01-04 19:27:49 SERVER -> CLIENT: xxxx
My Controller Code :
public function jobApply(Request $request)
{
$resume = Resume::where('candidate_id', Auth::guard('candidate')->user()->id)->first();
$compEmail = $request->txt_compEmail;
$cadEmail = $request->txt_candEmail;
$subject = $request->txt_subject;
$body = $request->txt_candSalary;
$file = $resume->resume;
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
$mail->isSMTP();
// Send using SMTP
$mail->Host = env('MAIL_HOST');
// Set the SMTP server to send through
$mail->SMTPAuth = true;
// Enable SMTP authentication
$mail->Username = env('MAIL_USERNAME');
// SMTP username
$mail->Password = env('MAIL_PASSWORD');
// SMTP password
$mail->SMTPSecure = env('MAIL_ENCRYPTION');
// Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
$mail->Port = env('MAIL_PORT');
Log::info("Check:".$mail->Host. ' '.$mail->Username.' '.$mail->Password.' '.$mail->SMTPSecure.' '.$mail->Port);// TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
//Recipients
$mail->setFrom($cadEmail);
$mail->addAddress($compEmail);
// Add a recipient
$mail->AltBody ='';
$mail->addAttachment($file );
// Content
$mail->isHTML(true);
// Set email format to HTML
$mail->Subject = $subject;
$mail->Body = 'Expected Salary : ' . $body ;
$mail->send();
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
// dd($e);
}
if ($mail){
return redirect()->back()->with('success','Mail Send Successfully..!!');
}
else{
return redirect()->back()->with('error','Something Went Wrong')->withInput();
}
}
How Can I hide this ? Anyone suggest me any solution. Advanced thanks.
You've asked it to show debug output, so it is producing it. If you don't want that, don't turn it on – comment out this line:
$mail->SMTPDebug = SMTP::DEBUG_SERVER;

How to get SMTP host for my phpmailer

I own my domain .com and it is hosted in different provider of the domain.
And I want to use PhpMailer, but my webhosting doesn't provide me SMTP server but it allows me to create emails of my domain (e.g. sdfsdfsfd#mydomain.com)
So, I really can't use phpmailer because it never connects to SMTP server, what should I do?
I brought my domain in onlydomains and my webhost is 000webhost.
<?php
require './PHPMailer-master/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = ''; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'noreply#mydomain.com'; // SMTP username
$mail->Password = '********'; // SMTP password
$mail->SMTPSecure = 'tls';
$mail->From = 'noreply#mydomain.com';
$mail->FromName = 'Admin';
$mail->addAddress('sdfsdf#hotmail.com'); // Name is optional
mail->WordWrap = 50; // Set word wrap to 50 characters
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'swag';
$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';
}
?>
Initially download the php mailer files to your sever(if not downloaded before) & define as following.Avoid using net sources in your code.Once you apply this it will work fine and the mail would be sent as from your mail id.
require("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->SMTPDebug = true;
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true; // SMTP authentication
$mail->Host = "ssl://smtp.gmail.com"; // SMTP server
$mail->Port = 465; // SMTP Port
$mail->Username = "your mail id"; // SMTP account username
$mail->Password = "password"; // SMTP account password
//The following code allows you to send mail automatically once the code is run
$mail->SetFrom('to mail id', 'name'); // FROM
$mail->AddReplyTo('mail id', 'name'); // Reply TO
$mail->AddAddress('recipient id'); // recipient email
$mail->Subject = "First SMTP Message"; // email subject
$mail->Body = "Hi! \n\n some text.";
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
After doing the above code if you still not able to send mail means do the following:
In class.smtp.php file add the below given two lines before the line "Connect to the SMTP server" & it ll work fine.
$host = "ssl://smtp.gmail.com";
$port = 465;

Awardspace Email Setting

I got a Award Space Mail Hosting account.I created that account only for the free email sending feature.
Tried
PHP mailer
swiftmailer ect..
But no use.Can anyone give me a full working code of php and mail form,so that i can just test it in my server .I cound send emails when using this file :
http://www.html-form-guide.com/php-form/php-form-validation.html
But,i dont want that peace of code.I want a perfect code that can work on My server.
Sample code:(Has an html form submit form)
<?php
if(isset($_POST['submit'])) {
$myemail = “support#domain.dx.am”;
$subject = $_POST['subject'];
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$headers = “From:Contact Form <$myemail>\r\n”;
$headers .= “Reply-To: $name <$email>\r\n”;
echo “Your message has been sent successfully!”;
mail($myemail, $subject, $message, $headers);
} else {
echo “An error occurred during the submission of your message”;
}
?>
Or i tried doing the same with php mailer:
<?php
require 'filefolder/mailer/class.phpmailer.php';
$mail = new PHPMailer;
$mail->IsSMTP(); // Set mailer to use SMTP
$mail->Host = 'localhost'; // Specify main and backup server
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'support#whatever.dx.am'; // SMTP username
$mail->Password = 'mypassword'; // SMTP password
$mail->From = 'support#whatever.dx.am';
$mail->FromName = 'Support';
$mail->AddAddress('anything#gmail.com'… // Name is optional
$mail->WordWrap = 50; // Set word wrap to 50 characters
$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;
exit;
}
echo 'Message has been sent';
?>
I am doing something wrong. But cant figure out what. Can anyone help me out .
The 'from' header should be an existing email account inside your Email Manager of your Hosting Control Panel.
as their FAQ says:
Tip: Please note that the 'From' header should be an existing email account inside your Email Manager of your Hosting Control Panel.
FAQ

phpmailer not sending mail to gmail

I have a email program that runs from a cron that was working, and now it doesn't. Well, it still works, but it doesn't send any emails to gmail. I have filters setup in gmail's filters to send all emails from: 'my domain' to my inbox instead of the spam folder, but now they don't even go to the spam folder. It works with yahoo and textfree.us.
Here is the code:
$alerttarget = $usersemail;
$subject = "A bill is OVERDUE!";
$body = "You have an overdue bill, in the amount of $" . $amount . ".";
$from = $userId . date("YmdHis") . $billdetailId . "b#mydomain.com";
sendemail($alerttarget, $body, $from, $subject);
function sendemail($target, $body, $from, $subject)
{
$mail = new PHPMailer();
$email = $target;
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "localhost"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
$mail->SetFrom($from, "my domain");
$mail->AddReplyTo($from, "my domain");
$mail->Subject = $subject;
$mail->MsgHTML($body);
$address = $email;
$mail->AddAddress($address, "me");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
}
I recently bought a new domain, and set the host to point to my ip address. I did this with godaddy's DNS manager. But, I only set the host to point to my ip address; nothing else. It stopped working, however, before I did all of this.

Resources