Sending Email With PHP from an Smtp Server

How to send email with SMTP in php

Take a look at PHP Mailer:

https://github.com/PHPMailer/PHPMailer

Example from that page:

<?php
require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

//$mail->SMTPDebug = 3; // Enable verbose debug output

$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user@example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to

$mail->From = 'from@example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('joe@example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen@example.com'); // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');

$mail->WordWrap = 50; // Set word wrap to 50 characters
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$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;
} else {
echo 'Message has been sent';
}

Send mail reliably with PHP?

From my experience, PHPMailer is the way to go - https://github.com/PHPMailer/PHPMailer

You need to get access to a SMTP server you will use to send your mail. This could either be the Google SMTP server (a @gmail.com email address) or your company's email server.

A quick example to do this using Google SMTP server is:

1) Install PHPMailer using composer from a terminal / shell:

composer require phpmailer/phpmailer

2) Include the composer generated autoload file and PHPMailer in your php script:

// Load Composer's autoloader
require 'vendor/autoload.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

3) Send email using:

$mail = new PHPMailer(true);
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output
$mail->isSMTP(); // Send using SMTP
$mail->Host = 'smtp.gmail.com'; // Set the SMTP server to send through
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'YOUREMAIL@gmail.com'; // SMTP username
$mail->Password = 'YOUREMAILPASSWORD'; // SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted
$mail->Port = 587; // TCP port to connect to

//Recipients
$mail->setFrom('YOUREMAIL@gmail.com', 'Mailer');
$mail->addAddress('TOADDRESS'); // Add a recipient

// Content
$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->send();

Read the PHPMailer documentation at: https://github.com/PHPMailer/PHPMailer

Sending email through local smtp server

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.

How do I specify to PHP that mail() should be sent using an external mail server?

mail() is intended to hand off to a local SMTP server, and does a poor job of it. For proper mail support, use Swiftmailer or PHPMailer, both of which fully support external SMTP servers and are far easier to use (plus letting you do things like mixed text/html mails, attachments, etc...)

Use default PHP mail function with SMTP server on Linux

You standard php mail function will just send to whatever is defined as the sendmail _path in php.ini

This is typically sendmail -t -i

You would need to configure sendmail to use smtp.

FWIW, most developer who do a lot of mail sending from PHP apps revile the mail() and instead use one of many mailing libraries (or services) which provide better configurability/reliability.

You could for example pipe the mail function to your own PHP script and use whatever library you wanted to in that script in order to do mail sending (and thus preserving the use of mail() function across applications).

Send email using live server SMTP server through PHP Mailer not working

As the error says it clearly you're trying to do email spoofing.

CLIENT: 250 Message denied for spoofing attempt via SMTP Auth

More information on email spoofing:

Email spoofing is the forgery of an email header so that the message appears to have originated from someone or somewhere other than the actual source. Email spoofing is a tactic used in phishing and spam campaigns because people are more likely to open an email when they think it has been sent by a legitimate source.

Solution:
As I can see in your code you have set up $mail->From to a gmail account. which is not owned by your domain and will end up failing.

Change $mail->SetFrom = "abc@gmail.com"; to $mail->SetFrom = "info@gitanjaliadvertising.com";

You can add replyTo to "abc@gmail.com" if you want replied to go to "abc@gmail.com"

Thanks.



Related Topics



Leave a reply



Submit