-1

I am a bit confused with setting up Sparkpost SMTP. I need to send automatic email from my website to clients, for eg. a welcome message when they register, a reminder email for consultation etc. The website will automatically generate the email.

On Sparkpost they show:

define('PHPMAILERHOST', 'smtp.sparkpostmail.com');
$phpmailer_smtpuser = 'SMTP_Injection';
$phpmailer_smtppassword = '<API_KEY>';
define('PHPMAILERPORT', 587);

However when I searched for more information on Stackoverflow, I found this:

$config['mailtype'] = "html";
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'smtp.sparkpostmail.com';
$config['smtp_user'] = 'user';
$config['smtp_pass'] = 'password';
$config['smtp_crypto'] = 'tls';
$config['smtp_port'] = '587';
$condig['crlf'] = "\r\n";
$config['newline'] = "\r\n";

In light of the above, will the following work:

//Sparkpost configuration
$config['mailtype'] = "html";
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'smtp.sparkpostmail.com';
$config['smtp_user'] = 'user';
$config['smtp_pass'] = 'password';
$config['smtp_crypto'] = 'tls';
$config['smtp_port'] = '587';
$condig['crlf'] = "\r\n";
$config['newline'] = "\r\n";

//My email code
$to_email = "$Email";
$from_email = "me@mydomain";
$subject = "Email Subject";
$comment =  "<html>Email message</html>";

// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

// More headers
$headers .= "From: Info <" . $from_email . ">\r\n";
//send email
mail($to_email, "$subject", $comment, $headers);

The reason I was asking, I don't want to setup, test and emails go through and while I am under the impression that my Sparkpost setup is working, the email goes through my host.

  • A random array doesn't automatically become the settings for sending e-mail. As it stands, it looks likely the e-mail just gets sent via the default system MTA rather than via SparkPost. – Jonnix Feb 18 '19 at 10:17
  • @JonStirling. Thanks. Can you help me with the correct method on how to use Sparkpost SMTP? – CharlesWashington Feb 18 '19 at 10:46
  • My suggestion is to use a library like PHPMailer, and follow the instructions on how to use, with the details you've been provided for connecting to SparkPost. A quick google search should provide you everything you need. – Jonnix Feb 18 '19 at 10:52

1 Answers1

1

If you want to use SparkPost you can either use their library or, as suggested by Jon Stirling, use PHPMailer with SparkPost configurations.

Use SparkPost PHP Library

Use PHPMailer

  • Include phpMailer class in your script

  • Configure it to use SparkPost like below

$mail = new PHPMailer;
$mail->SMTPDebug = 3;
$mail->isSMTP();
$mail->Host = 'smtp.sparkpostmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = 'SMTP_Injection';
$mail->Password = '<API_KEY>'; //make sure you add SMTP permission to API Key
$mail->setFrom('testing@sparkpostbox.com'); //you can use w/o adding your sending domain like ~50 messages. Once you add your sending domain, use that. 
$mail->addAddress('recipient@domain.com');
$mail->Subject = 'Test subject';
$mail->Body    = 'Hello World!';
$mail->send();
HungryCoder
  • 7,506
  • 1
  • 38
  • 51